diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index bfd2c3422..db3e42877 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -16,6 +16,7 @@ package proxy import ( "context" + "errors" "fmt" "io" "net" @@ -25,6 +26,7 @@ import ( "strings" "sync" "sync/atomic" + "syscall" "time" "cloud.google.com/go/cloudsqlconn" @@ -998,6 +1000,10 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi lc := net.ListenConfig{KeepAlive: 30 * time.Second} ln, err := lc.Listen(ctx, network, address) + if network == "unix" && errors.Is(err, syscall.EADDRINUSE) && removeStaleUnixSocket(address) { + c.logger.Infof("[%v] Removed stale Unix socket %v", inst.Name, address) + ln, err = lc.Listen(ctx, network, address) + } if err != nil { c.logger.Errorf("[%v] could not listen to address %v: %v", inst.Name, address, err) return nil, err @@ -1013,6 +1019,27 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi return m, nil } +// removeStaleUnixSocket removes the Unix socket at address when nothing is +// listening on it, e.g., because a previous Proxy process was killed before it +// could close its listener. It reports whether the socket was removed. A path +// that is not a socket, or a socket that still accepts connections, is left +// alone. +func removeStaleUnixSocket(address string) bool { + fi, err := os.Lstat(address) + if err != nil || fi.Mode()&os.ModeSocket == 0 { + return false + } + conn, err := net.DialTimeout("unix", address, time.Second) + if err == nil { + _ = conn.Close() + return false + } + if !errors.Is(err, syscall.ECONNREFUSED) { + return false + } + return os.Remove(address) == nil +} + // newUnixSocketMount parses the configuration and returns the path to the unix // socket, or an error if that path is not valid. func newUnixSocketMount(inst InstanceConnConfig, unixSocketDir string, postgres bool) (string, error) { diff --git a/internal/proxy/proxy_other_test.go b/internal/proxy/proxy_other_test.go index 77d7c21bb..426b09787 100644 --- a/internal/proxy/proxy_other_test.go +++ b/internal/proxy/proxy_other_test.go @@ -18,7 +18,9 @@ package proxy_test import ( "context" + "net" "os" + "path/filepath" "testing" "github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/internal/proxy" @@ -60,3 +62,90 @@ func TestFuseClosesGracefully(t *testing.T) { } } + +// createStaleUnixSocket leaves a Unix socket file at addr with nothing +// listening on it, as a Proxy process that was killed (e.g., SIGKILL or OOM) +// before closing its listener would. +func createStaleUnixSocket(t *testing.T, addr string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(addr), 0777); err != nil { + t.Fatal(err) + } + ln, err := net.Listen("unix", addr) + if err != nil { + t.Fatal(err) + } + ln.(*net.UnixListener).SetUnlinkOnClose(false) + if err := ln.Close(); err != nil { + t.Fatal(err) + } +} + +func TestClientInitializationWithStaleUnixSocket(t *testing.T) { + tcs := []struct { + desc string + inst string + sockPath func(dir string) string + }{ + { + desc: "MySQL", + inst: mysql, + sockPath: func(dir string) string { return filepath.Join(dir, mysql) }, + }, + { + desc: "Postgres", + inst: pg, + sockPath: func(dir string) string { return filepath.Join(dir, pg, ".s.PGSQL.5432") }, + }, + } + for _, tc := range tcs { + t.Run(tc.desc, func(t *testing.T) { + testDir, cleanup := createTempDir(t) + defer cleanup() + addr := tc.sockPath(testDir) + createStaleUnixSocket(t, addr) + + c, err := proxy.NewClient(context.Background(), &fakeDialer{}, testLogger, &proxy.Config{ + UnixSocket: testDir, + Instances: []proxy.InstanceConnConfig{{Name: tc.inst}}, + }, nil) + if err != nil { + t.Fatalf("want error = nil, got = %v", err) + } + defer c.Close() + + conn, err := net.Dial("unix", addr) + if err != nil { + t.Fatalf("want error = nil, got = %v", err) + } + conn.Close() + }) + } +} + +func TestClientInitializationWithUnixSocketInUse(t *testing.T) { + testDir, cleanup := createTempDir(t) + defer cleanup() + addr := filepath.Join(testDir, mysql) + ln, err := net.Listen("unix", addr) + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + c, err := proxy.NewClient(context.Background(), &fakeDialer{}, testLogger, &proxy.Config{ + UnixSocket: testDir, + Instances: []proxy.InstanceConnConfig{{Name: mysql}}, + }, nil) + if err == nil { + c.Close() + t.Fatal("want error when another process is listening on the socket, got nil") + } + + // The socket of the other process must be left in place. + conn, err := net.Dial("unix", addr) + if err != nil { + t.Fatalf("want error = nil, got = %v", err) + } + conn.Close() +}