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

@@ -101,6 +101,7 @@ func DownloadFileWithProgress(destpath, url string, kill_if_signaled bool) (err
}
do_download := func() {
lp.RecoverFromPanicInGoRoutine()
dl_data.mutex.Lock()
dl_data.download_started = true
dl_data.mutex.Unlock()

View File

@@ -136,14 +136,16 @@ func (self *ImageCollection) ResizeForPageSize(width, height int) {
ctx := images.Context{}
keys := utils.Keys(self.images)
ctx.Parallel(0, len(keys), func(nums <-chan int) {
if err := ctx.SafeParallel(0, len(keys), func(nums <-chan int) {
for i := range nums {
img := self.images[keys[i]]
if img.src.loaded && img.err == nil {
img.ResizeForPageSize(width, height)
}
}
})
}); err != nil {
panic(err)
}
}
func (self *ImageCollection) DeleteAllVisiblePlacements(lp *loop.Loop) {
@@ -294,7 +296,7 @@ func (self *ImageCollection) LoadAll() {
defer self.mutex.Unlock()
ctx := images.Context{}
all := utils.Values(self.images)
ctx.Parallel(0, len(self.images), func(nums <-chan int) {
if err := ctx.SafeParallel(0, len(self.images), func(nums <-chan int) {
for i := range nums {
img := all[i]
if !img.src.loaded {
@@ -305,7 +307,9 @@ func (self *ImageCollection) LoadAll() {
img.src.loaded = true
}
}
})
}); err != nil {
panic(err)
}
}
func NewImageCollection(paths ...string) *ImageCollection {

View File

@@ -44,6 +44,12 @@ func read_ignoring_temporary_errors(f *tty.Term, buf []byte) (int, error) {
}
func read_from_tty(pipe_r *os.File, term *tty.Term, results_channel chan<- []byte, err_channel chan<- error, quit_channel <-chan byte, leftover_channel chan<- []byte) {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
err_channel <- fmt.Errorf("%s", text)
}
}()
keep_going := true
pipe_fd := int(pipe_r.Fd())
tty_fd := term.Fd()
@@ -115,6 +121,12 @@ func read_until_primary_device_attributes_response(term *tty.Term, initial_bytes
}
received := make(chan error)
go func() {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
received <- fmt.Errorf("%s", text)
}
}()
buf := make([]byte, 1024)
n, err := read_ignoring_temporary_errors(term, buf)
if n > 0 {

View File

@@ -168,6 +168,12 @@ func write_to_tty(
pipe_r *os.File, term *tty.Term,
job_channel <-chan write_msg, err_channel chan<- error, write_done_channel chan<- IdType,
) {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
err_channel <- fmt.Errorf("%s", text)
}
}()
keep_going := true
defer func() {
pipe_r.Close()