From ecc241d8917d95a24085be6fe23bcbbc84036118 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 25 Apr 2026 13:40:58 +0530 Subject: [PATCH] For for background copy go routine to exit --- tools/utils/file_at_fd.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/utils/file_at_fd.go b/tools/utils/file_at_fd.go index 7b5b34edc..2004b7196 100644 --- a/tools/utils/file_at_fd.go +++ b/tools/utils/file_at_fd.go @@ -13,6 +13,7 @@ import ( "syscall" "time" + "github.com/kovidgoyal/go-parallel" "golang.org/x/sys/unix" ) @@ -402,6 +403,11 @@ func copy_file_and_close(ctx context.Context, src *os.File, dest *os.File) (err dest.Close() }() go func() { + defer func() { + if r := recover(); r != nil { + err_chan <- parallel.Format_stacktrace_on_panic(r, 1) + } + }() // this go routine will automatically exit when src/dest are closed // even if copying is not complete. io.Copy() automatically use // sendfile() or similar mechanisms for efficiency. @@ -411,6 +417,10 @@ func copy_file_and_close(ctx context.Context, src *os.File, dest *os.File) (err select { case <-ctx.Done(): + // wait for go routine to exit + src.Close() + dest.Close() + <-err_chan return ctx.Err() case err := <-err_chan: return err