diff --git a/pkgs/mcpx/internal/sdnotify/sdnotify.go b/pkgs/mcpx/internal/sdnotify/sdnotify.go index fdd3271b74..939f83b419 100644 --- a/pkgs/mcpx/internal/sdnotify/sdnotify.go +++ b/pkgs/mcpx/internal/sdnotify/sdnotify.go @@ -4,11 +4,19 @@ package sdnotify import ( + "context" "fmt" "net" "os" + "time" ) +const notifyTimeout = time.Second + +type unixgramDialer interface { + DialContext(context.Context, string, string) (net.Conn, error) +} + // Send writes one state assignment, such as "READY=1", to $NOTIFY_SOCKET. // Outside systemd the variable is unset and Send does nothing. func Send(state string) error { @@ -16,13 +24,22 @@ func Send(state string) error { if path == "" { return nil } - // A leading '@' names an abstract socket. Go's net package takes the same - // convention, so the name is passed through unchanged. - conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: path, Net: "unixgram"}) + // DialContext accepts Go's leading-@ convention for abstract sockets and + // bounds connection setup as well as the write below. + return sendWithDialer(path, state, &net.Dialer{}, notifyTimeout) +} + +func sendWithDialer(path, state string, dialer unixgramDialer, timeout time.Duration) error { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + conn, err := dialer.DialContext(ctx, "unixgram", path) if err != nil { return fmt.Errorf("notify %s: %w", path, err) } defer func() { _ = conn.Close() }() + if err := conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil { + return fmt.Errorf("notify %s: set write deadline: %w", path, err) + } if _, err := conn.Write([]byte(state)); err != nil { return fmt.Errorf("notify %s: %w", path, err) } diff --git a/pkgs/mcpx/internal/sdnotify/sdnotify_test.go b/pkgs/mcpx/internal/sdnotify/sdnotify_test.go index db88f0aac0..4430a561b0 100644 --- a/pkgs/mcpx/internal/sdnotify/sdnotify_test.go +++ b/pkgs/mcpx/internal/sdnotify/sdnotify_test.go @@ -1,10 +1,14 @@ package sdnotify import ( + "context" + "errors" "fmt" "net" "os" "path/filepath" + "runtime" + "syscall" "testing" "time" @@ -61,11 +65,25 @@ func TestReadyAndMainPIDReachTheSocket(t *testing.T) { } func TestAbstractSocketName(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("abstract Unix sockets are Linux-specific") + } + name := fmt.Sprintf("@mcpx-sdnotify-%d-%d", os.Getpid(), time.Now().UnixNano()) - conn := listen(t, name) + conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: name, Net: "unixgram"}) + if err != nil { + if abstractSocketUnavailable(err) { + t.Skipf("abstract Unix datagram sockets are unavailable here: %v", err) + } + t.Fatalf("listen on abstract socket: %v", err) + } + t.Cleanup(func() { _ = conn.Close() }) t.Setenv("NOTIFY_SOCKET", name) if err := Ready(); err != nil { + if abstractSocketUnavailable(err) { + t.Skipf("abstract Unix datagram sockets are unavailable here: %v", err) + } t.Fatal(err) } if got := next(t, conn); got != "READY=1" { @@ -73,6 +91,109 @@ func TestAbstractSocketName(t *testing.T) { } } +func abstractSocketUnavailable(err error) bool { + var networkErr net.Error + if errors.As(err, &networkErr) && networkErr.Timeout() { + return true + } + return errors.Is(err, syscall.EAFNOSUPPORT) || + errors.Is(err, syscall.EPROTONOSUPPORT) || + errors.Is(err, syscall.EOPNOTSUPP) || + errors.Is(err, syscall.ENOTSUP) +} + +type timeoutError struct{} + +func (timeoutError) Error() string { return "timed out" } +func (timeoutError) Timeout() bool { return true } +func (timeoutError) Temporary() bool { return true } + +func TestAbstractSocketUnavailable(t *testing.T) { + tests := []struct { + name string + err error + want bool + }{ + {name: "timeout", err: timeoutError{}, want: true}, + {name: "address family unsupported", err: syscall.EAFNOSUPPORT, want: true}, + {name: "protocol unsupported", err: syscall.EPROTONOSUPPORT, want: true}, + {name: "operation unsupported", err: syscall.EOPNOTSUPP, want: true}, + {name: "not supported", err: syscall.ENOTSUP, want: true}, + {name: "invalid argument is unexpected", err: syscall.EINVAL}, + {name: "other error is unexpected", err: errors.New("unexpected socket error")}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := abstractSocketUnavailable(fmt.Errorf("wrapped: %w", test.err)); got != test.want { + t.Fatalf("abstractSocketUnavailable(%v) = %v, want %v", test.err, got, test.want) + } + }) + } +} + +type blockingDialer struct{} + +func (blockingDialer) DialContext(ctx context.Context, _, _ string) (net.Conn, error) { + <-ctx.Done() + return nil, ctx.Err() +} + +func TestSendDialHasDeadline(t *testing.T) { + const timeout = 20 * time.Millisecond + started := time.Now() + err := sendWithDialer("@unused", "READY=1", blockingDialer{}, timeout) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("sendWithDialer error = %v, want deadline exceeded", err) + } + if elapsed := time.Since(started); elapsed > 250*time.Millisecond { + t.Fatalf("dial timeout took %s, want it bounded by %s", elapsed, timeout) + } +} + +type deadlineRecordingConn struct { + net.Conn + writeDeadline time.Time + written string +} + +func (c *deadlineRecordingConn) SetWriteDeadline(deadline time.Time) error { + c.writeDeadline = deadline + return nil +} + +func (c *deadlineRecordingConn) Write(p []byte) (int, error) { + c.written = string(p) + return len(p), nil +} + +func (c *deadlineRecordingConn) Close() error { return nil } + +type fixedDialer struct{ conn net.Conn } + +func (d fixedDialer) DialContext(context.Context, string, string) (net.Conn, error) { + return d.conn, nil +} + +func TestSendWriteHasDeadline(t *testing.T) { + conn := &deadlineRecordingConn{} + started := time.Now() + const timeout = time.Second + if err := sendWithDialer("/unused", "READY=1", fixedDialer{conn: conn}, timeout); err != nil { + t.Fatal(err) + } + finished := time.Now() + // The deadline is set after dialing, at some instant between started + // and finished, so it must fall in [started+timeout, finished+timeout]. + // Bounding it by timestamps captured around the call avoids assuming + // the setup itself finishes within any fixed scheduling slack. + if conn.writeDeadline.Before(started.Add(timeout)) || conn.writeDeadline.After(finished.Add(timeout)) { + t.Fatalf("write deadline = %s, want between %s and %s", conn.writeDeadline, started.Add(timeout), finished.Add(timeout)) + } + if conn.written != "READY=1" { + t.Fatalf("write = %q, want READY=1", conn.written) + } +} + func TestMissingSocketIsReported(t *testing.T) { t.Setenv("NOTIFY_SOCKET", filepath.Join(t.TempDir(), "absent.sock")) if err := Ready(); err == nil {