Clean up runtime.SetFinalizer uses (#61008)

This commit is contained in:
Edoardo Spadolini
2025-11-05 21:15:08 +00:00
committed by GitHub
parent 4713ff1c89
commit 9c9981d622
6 changed files with 14 additions and 7 deletions
+3
View File
@@ -99,6 +99,9 @@ func RunForkAuthenticate(ctx context.Context, params ForkAuthenticateParams) err
disownR.Close()
}()
// disownW and killR are going to stay alive until the defer or until the
// close after a successful start, so we can pass them to
// configureReexecForOS here
signalFd, killFd := configureReexecForOS(cmd, disownW, killR)
cmd.Args = append(cmd.Args, params.GetArgs(signalFd, killFd)...)
cmd.Args[0] = os.Args[0]
+2 -1
View File
@@ -33,7 +33,8 @@ func getExecutable() (string, error) {
}
// configureReexecForOS configures the command with files to inherit and
// os-specific tweaks.
// os-specific tweaks. The signaling files should be kept alive until the
// command is started, then the caller should close them.
func configureReexecForOS(cmd *exec.Cmd, signal, kill *os.File) (signalFd, killFd uint64) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
+2 -1
View File
@@ -30,7 +30,8 @@ func getExecutable() (string, error) {
}
// configureReexecForOS configures the command with files to inherit and
// os-specific tweaks.
// os-specific tweaks. The signaling files should be kept alive until the
// command is started, then the caller should close them.
func configureReexecForOS(cmd *exec.Cmd, signal, kill *os.File) (signalFd, killFd uint64) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
+2 -4
View File
@@ -21,7 +21,6 @@ package reexec
import (
"os"
"os/exec"
"runtime"
"syscall"
"github.com/gravitational/trace"
@@ -34,10 +33,9 @@ func getExecutable() (string, error) {
}
// configureReexecForOS configures the command with files to inherit and
// os-specific tweaks.
// os-specific tweaks. The signaling files should be kept alive until the
// command is started, then the caller should close them.
func configureReexecForOS(cmd *exec.Cmd, signal, kill *os.File) (signalFd, killFd uint64) {
// Prevent handle from being closed when signal is garbage collected.
runtime.SetFinalizer(signal, nil)
cmd.SysProcAttr = &syscall.SysProcAttr{
AdditionalInheritedHandles: []syscall.Handle{
syscall.Handle(signal.Fd()),
+4
View File
@@ -396,6 +396,10 @@ func (c *Cursor[T]) closeLocked() {
if c.closed {
return
}
// reset the finalizer that was set in (*Buffer).NewCursor, if it's still
// set - i.e. if this call is not the result of finalizeCursor but it's an
// explicit Close
runtime.SetFinalizer(c, nil)
c.closed = true
+1 -1
View File
@@ -34,7 +34,7 @@ func NewWriterFinalizer[T io.WriteCloser](writer T) *WriterFinalizer[T] {
wr := &WriterFinalizer[T]{
writer: writer,
}
runtime.SetFinalizer(wr, (*WriterFinalizer[T]).Close)
runtime.AddCleanup(wr, func(writer T) { _ = writer.Close() }, writer)
return wr
}