Skip to content

Commit 5e0d908

Browse files
committed
socket: try removing is socket-location is a directory
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. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c833fcd commit 5e0d908

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sockets/unix_socket.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,14 @@ 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+
return nil, err
86+
}
87+
if err := syscall.Rmdir(path); err != nil {
88+
return nil, err
89+
}
8490
}
8591
mask := syscall.Umask(0777)
8692
defer syscall.Umask(mask)

0 commit comments

Comments
 (0)