Skip to content

Commit 35aa63e

Browse files
committed
never send procError after the socket closed
Signed-off-by: lifubang <[email protected]>
1 parent 313ec8b commit 35aa63e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

libcontainer/init_linux.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ func startInitialization() (retErr error) {
107107

108108
defer func() {
109109
// If this defer is ever called, this means initialization has failed.
110-
// Send the error back to the parent process in the form of an initError.
110+
// Send the error back to the parent process in the form of an initError
111+
// if the sync socket has not been closed.
112+
if syncPipe.isClosed() {
113+
return
114+
}
111115
ierr := initError{Message: retErr.Error()}
112116
if err := writeSyncArg(syncPipe, procError, ierr); err != nil {
113117
fmt.Fprintln(os.Stderr, err)

libcontainer/sync_unix.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"sync/atomic"
78

89
"golang.org/x/sys/unix"
910
)
@@ -14,7 +15,8 @@ import (
1415
// which ends up making things like json.Decoder hang forever if the packet is
1516
// bigger than the internal read buffer.
1617
type syncSocket struct {
17-
f *os.File
18+
f *os.File
19+
closed atomic.Bool
1820
}
1921

2022
func newSyncSocket(f *os.File) *syncSocket {
@@ -26,9 +28,15 @@ func (s *syncSocket) File() *os.File {
2628
}
2729

2830
func (s *syncSocket) Close() error {
31+
// Even with errors from Close(), we have to assume the pipe was closed.
32+
s.closed.Store(true)
2933
return s.f.Close()
3034
}
3135

36+
func (s *syncSocket) isClosed() bool {
37+
return s.closed.Load()
38+
}
39+
3240
func (s *syncSocket) WritePacket(b []byte) (int, error) {
3341
return s.f.Write(b)
3442
}

0 commit comments

Comments
 (0)