Make various goroutines panic-safe

This commit is contained in:
Kovid Goyal
2025-10-09 07:17:53 +05:30
parent 49d8b1a9d0
commit f067e9cd92
11 changed files with 69 additions and 22 deletions

View File

@@ -32,7 +32,7 @@ var highlighter = sync.OnceValue(func() highlight.Highlighter {
func highlight_all(paths []string, light bool) {
ctx := images.Context{}
srd := prefer_light_colors(light)
ctx.Parallel(0, len(paths), func(nums <-chan int) {
if err := ctx.SafeParallel(0, len(paths), func(nums <-chan int) {
for i := range nums {
path := paths[i]
raw, err := highlighter().HighlightFile(path, &srd)
@@ -45,5 +45,7 @@ func highlight_all(paths []string, light bool) {
dark_highlighted_lines_cache.Set(path, text_to_lines(raw))
}
}
})
}); err != nil {
panic(err)
}
}

View File

@@ -358,14 +358,16 @@ func diff(jobs []diff_job, context_count int) (ans map[string]*Patch, err error)
patch *Patch
}
results := make(chan result, len(jobs))
ctx.Parallel(0, len(jobs), func(nums <-chan int) {
if err := ctx.SafeParallel(0, len(jobs), func(nums <-chan int) {
for i := range nums {
job := jobs[i]
r := result{file1: job.file1, file2: job.file2}
r.patch, r.err = do_diff(job.file1, job.file2, context_count)
results <- r
}
})
}); err != nil {
panic(err)
}
close(results)
for r := range results {
if r.err != nil {

View File

@@ -106,7 +106,7 @@ func (self *Search) search(logical_lines *LogicalLines) {
self.matches = make(map[ScrollPos][]Span)
ctx := images.Context{}
mutex := sync.Mutex{}
ctx.Parallel(0, logical_lines.Len(), func(nums <-chan int) {
if err := ctx.SafeParallel(0, logical_lines.Len(), func(nums <-chan int) {
for i := range nums {
line := logical_lines.At(i)
if line.line_type == EMPTY_LINE || line.line_type == IMAGE_LINE {
@@ -121,7 +121,9 @@ func (self *Search) search(logical_lines *LogicalLines) {
}
})
}
})
}); err != nil {
panic(err)
}
for _, spans := range self.matches {
slices.SortFunc(spans, func(a, b Span) int { return a.start - b.start })
}