Skip to content

socket: try removing if socket-location is a directory on Linux #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion sockets/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ func WithChmod(mask os.FileMode) SockOption {
// this should only be for a short duration, it may affect other processes that
// create files/directories during that period.
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS X and Linux differ on whether unlink(dir)
returns EISDIR,

https://golang.org/src/os/file_unix.go#L289

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. I guess it's still ok to retry on Linux only then? Added a comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a test

// 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
}
}

// net.Listen does not allow for permissions to be set. As a result, when
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 @@ -4,8 +4,11 @@ package sockets

import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"runtime"
"syscall"
"testing"
)
Expand Down Expand Up @@ -75,3 +78,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 := path.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 := path.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")
})
}