Fixes #17131.
Change-Id: I60b381687746fadce12ef18a190cbe3f435172f2
Reviewed-on: https://go-review.googlesource.com/32098
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Quentin Smith <quentin@golang.org>
import (
"context"
"os"
+ "sync"
"syscall"
"time"
)
// typically use variables of type Listener instead of assuming Unix
// domain sockets.
type UnixListener struct {
- fd *netFD
- path string
- unlink bool
+ fd *netFD
+ path string
+ unlink bool
+ unlinkOnce sync.Once
}
func (ln *UnixListener) ok() bool { return ln != nil && ln.fd != nil }
// is at least compatible with the auto-remove
// sequence in ListenUnix. It's only non-Go
// programs that can mess us up.
- if ln.path[0] != '@' && ln.unlink {
- syscall.Unlink(ln.path)
- }
+ // Even if there are racy calls to Close, we want to unlink only for the first one.
+ ln.unlinkOnce.Do(func() {
+ if ln.path[0] != '@' && ln.unlink {
+ syscall.Unlink(ln.path)
+ }
+ })
return ln.fd.Close()
}
import (
"bytes"
"internal/testenv"
+ "io/ioutil"
"os"
"reflect"
"runtime"
if _, err := os.Stat(name); err == nil {
t.Fatal("closing unix listener did not remove unix socket")
}
+ if err := ioutil.WriteFile(name, []byte("hello world"), 0666); err != nil {
+ t.Fatalf("cannot recreate socket file: %v", err)
+ }
+ if _, err := os.Stat(name); err != nil {
+ t.Fatal("recreating unix listener as file failed: %v", err)
+ }
+ l.Close()
+ if _, err := os.Stat(name); err != nil {
+ t.Fatalf("second close of unix socket did second remove: %v", err)
+ }
+ os.Remove(name)
}