Skip to content
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
27 changes: 27 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package proxy

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand All @@ -25,6 +26,7 @@ import (
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

"cloud.google.com/go/cloudsqlconn"
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
89 changes: 89 additions & 0 deletions internal/proxy/proxy_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package proxy_test

import (
"context"
"net"
"os"
"path/filepath"
"testing"

"github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/internal/proxy"
Expand Down Expand Up @@ -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()
}
Loading