helper/seccomp: use sync.Once for closeWrite
All checks were successful
Test / Create distribution (push) Successful in 1m29s
Test / Run NixOS test (push) Successful in 4m13s

This makes the code much cleaner, and eliminates the intermittent ErrInvalid errors.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-13 22:49:16 +09:00
parent 1e6a059668
commit d1d20c06fb
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q

View File

@ -1,11 +1,9 @@
package seccomp
import (
"io/fs"
"os"
"runtime"
"sync"
"sync/atomic"
)
type exporter struct {
@ -14,7 +12,8 @@ type exporter struct {
prepareOnce sync.Once
prepareErr error
closeErr atomic.Pointer[error]
closeOnce sync.Once
closeErr error
exportErr <-chan error
}
@ -36,19 +35,17 @@ func (e *exporter) prepare() error {
}
func (e *exporter) closeWrite() error {
if !e.closeErr.CompareAndSwap(nil, &fs.ErrInvalid) {
return *e.closeErr.Load()
}
e.closeOnce.Do(func() {
if e.w == nil {
panic("closeWrite called on invalid exporter")
}
err := e.w.Close()
e.closeErr.Store(&err)
e.closeErr = e.w.Close()
// no need for a finalizer anymore
runtime.SetFinalizer(e, nil)
})
return err
return e.closeErr
}
func newExporter(opts SyscallOpts) *exporter {