Skip to content

Commit

Permalink
socket: try removing if socket-location is a directory on Linux
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
thaJeztah committed Mar 15, 2021
1 parent 851a7fc commit 05e56ab
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sockets/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@ func WithChmod(mask os.FileMode) SockOption {

// NewUnixSocketWithOpts creates a unix socket with the specified options
func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error) {
// Using syscall.Unlink(), not os.Remove() to prevent deleting the socket if it's in use
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
return nil, err
if err != syscall.EISDIR {
// On Linux, attempting to remove a directory returns syscall.EISDIR,
// in which case we try to remove the directory. MacOS does not return
// this error, so we'll return immediately, see:
// https://github.com/golang/go/blob/6b420169d798c7ebe733487b56ea5c3fa4aab5ce/src/os/file_unix.go#L300-L311
return nil, err
}
if err := syscall.Rmdir(path); err != nil {
return nil, err
}
}
mask := syscall.Umask(0777)
defer syscall.Umask(mask)
Expand Down
48 changes: 48 additions & 0 deletions sockets/unix_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package sockets

import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
)
Expand Down Expand Up @@ -70,3 +73,48 @@ func TestUnixSocketWithOpts(t *testing.T) {
}
runTest(t, path, l, echoStr)
}

func TestUnixSocketConflictDirectory(t *testing.T) {
tmpDir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

t.Run("conflicting directory", func(t *testing.T) {
if runtime.GOOS == "darwin" {
t.Skip("not supported on macOS")
}
path := filepath.Join(tmpDir, "test.sock")

// Create a conflicting directory at the socket location
err = os.MkdirAll(path, 0700)
if err != nil {
t.Fatal(err)
}

l, err := NewUnixSocketWithOpts(path)
if err != nil {
t.Fatal(err)
}
defer l.Close()
runTest(t, path, l, "hello")
})

t.Run("conflicting file", func(t *testing.T) {
// Create a conflicting file at the socket location
path := filepath.Join(tmpDir, "test2.sock")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
f.Close()

l, err := NewUnixSocketWithOpts(path)
if err != nil {
t.Fatal(err)
}
defer l.Close()
runTest(t, path, l, "hello")
})
}

0 comments on commit 05e56ab

Please sign in to comment.