Add atexit cleanup via worker process to kittens infrastructure

This commit is contained in:
Kovid Goyal
2025-09-30 11:52:13 +05:30
parent 4f9519d773
commit 190e3e5891
2 changed files with 72 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ import (
)
func KittenMain(args ...string) int {
defer utils.WaitForAtexitWorkerToFinish()
krm := os.Getenv("KITTY_KITTEN_RUN_MODULE")
os.Unsetenv("KITTY_KITTEN_RUN_MODULE")
switch krm {

71
tools/utils/atexit.go Normal file
View File

@@ -0,0 +1,71 @@
package utils
import (
"fmt"
"io"
"os"
"os/exec"
"sync"
"sync/atomic"
)
var _ = fmt.Print
type worker struct {
cmd *exec.Cmd
stdin_pipe io.WriteCloser
}
var worker_started atomic.Bool
var get_worker = sync.OnceValues(func() (*worker, error) {
exe, err := os.Executable()
if err != nil {
return nil, err
}
cmd := exec.Command(exe, "__atexit__")
cmd.Stdout = nil
cmd.Stderr = os.Stderr
ans := worker{cmd: cmd}
si, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
ans.stdin_pipe = si
if err = cmd.Run(); err != nil {
return nil, err
}
worker_started.Store(true)
return &ans, nil
})
func WaitForAtexitWorkerToFinish() {
if worker_started.Load() {
if w, err := get_worker(); err == nil {
w.stdin_pipe.Close()
_ = w.cmd.Wait()
}
}
}
func register(prefix, path string) error {
if w, err := get_worker(); err == nil {
_, err = fmt.Fprintln(w.stdin_pipe, prefix+" "+path)
return err
} else {
return err
}
}
func AtExitUnlink(path string) error {
return register("unlink", path)
}
func AtExitShmUnlink(path string) error {
return register("shm_unlink", path)
}
func AtExitRmtree(path string) error {
return register("rmtree", path)
}