From d1d20c06fb0c3ed5aaff15f284921a5e61c291e1 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 13 Feb 2025 22:49:16 +0900 Subject: [PATCH] helper/seccomp: use sync.Once for closeWrite This makes the code much cleaner, and eliminates the intermittent ErrInvalid errors. Signed-off-by: Ophestra --- helper/seccomp/export.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/helper/seccomp/export.go b/helper/seccomp/export.go index dd1aeda..52ec414 100644 --- a/helper/seccomp/export.go +++ b/helper/seccomp/export.go @@ -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() - } - if e.w == nil { - panic("closeWrite called on invalid exporter") - } - err := e.w.Close() - e.closeErr.Store(&err) + e.closeOnce.Do(func() { + if e.w == nil { + panic("closeWrite called on invalid exporter") + } + e.closeErr = e.w.Close() - // no need for a finalizer anymore - runtime.SetFinalizer(e, nil) + // no need for a finalizer anymore + runtime.SetFinalizer(e, nil) + }) - return err + return e.closeErr } func newExporter(opts SyscallOpts) *exporter {