Skip to content

Commit 19cabb7

Browse files
committed
socket: try removing if socket-location is a directory on Linux
Due to race-conditions between containers starting and the Docker remote API being up, containers bind-mounting the docker-socket may cause the socket-path to be created as a directory. This patch will attempt to remove the directory in such situations. Removing will fail if the directory is not empty. MacOS does not allow us to detect that the path is a directory, and we'll return immediately instead of retrying. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 09f4792 commit 19cabb7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

sockets/unix_socket.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@ func WithChmod(mask os.FileMode) SockOption {
7979

8080
// NewUnixSocketWithOpts creates a unix socket with the specified options
8181
func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error) {
82+
// Using syscall.Unlink(), not os.Remove() to prevent deleting the socket if it's in use
8283
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
83-
return nil, err
84+
if err != syscall.EISDIR {
85+
// On Linux, attempting to remove a directory returns syscall.EISDIR,
86+
// in which case we try to remove the directory. MacOS does not return
87+
// this error, so we'll return immediately, see:
88+
// https://github.com/golang/go/blob/6b420169d798c7ebe733487b56ea5c3fa4aab5ce/src/os/file_unix.go#L300-L311
89+
return nil, err
90+
}
91+
if err := syscall.Rmdir(path); err != nil {
92+
return nil, err
93+
}
8494
}
8595
mask := syscall.Umask(0777)
8696
defer syscall.Umask(mask)

0 commit comments

Comments
 (0)