Skip to content
Closed
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
15 changes: 14 additions & 1 deletion pkgs/mcpx/internal/sdnotify/sdnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ import (
"fmt"
"net"
"os"
"time"
)

// writeTimeout bounds one notify datagram. A notify is best-effort
// daemon bookkeeping: it must never wedge startup, so Send fails with
// an error after this long instead of blocking on a socket nobody is
// draining (#5428: an abstract socket in the fleet-build container
// accepted the connection and then blocked the write for the full
// 1800s test timeout).
const writeTimeout = 5 * time.Second

// 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 {
Expand All @@ -18,11 +27,15 @@ func Send(state string) error {
}
// 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"})
dialer := net.Dialer{Timeout: writeTimeout}
conn, err := dialer.Dial("unixgram", path)
if err != nil {
return fmt.Errorf("notify %s: %w", path, err)
}
defer func() { _ = conn.Close() }()
if err := conn.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
return fmt.Errorf("notify %s: %w", path, err)
}
if _, err := conn.Write([]byte(state)); err != nil {
return fmt.Errorf("notify %s: %w", path, err)
}
Expand Down
39 changes: 39 additions & 0 deletions pkgs/mcpx/internal/sdnotify/sdnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,42 @@ func TestMissingSocketIsReported(t *testing.T) {
t.Fatal("expected an error for a socket nobody listens on")
}
}

// TestSendFailsFastWhenTheSocketIsNotDrained is the #5428 regression:
// a bound-but-never-read notify socket must not make Send block until
// the caller's own timeout (1800s in the fleet-build container, where
// a fresh sender's write to a full abstract-socket queue blocks). On
// kernels where a fresh sender's datagram is delivered despite the
// full queue, the blocking path cannot be exercised and the test
// skips with that reason.
func TestSendFailsFastWhenTheSocketIsNotDrained(t *testing.T) {
name := fmt.Sprintf("@mcpx-sdnotify-full-%d-%d", os.Getpid(), time.Now().UnixNano())
listen(t, name)
t.Setenv("NOTIFY_SOCKET", name)

// Fill the listener's receive queue: datagrams nobody reads.
spam, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: name, Net: "unixgram"})
if err != nil {
t.Fatal(err)
}
defer func() { _ = spam.Close() }()
payload := make([]byte, 8192)
for i := 0; i < 8192; i++ {
_ = spam.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
if _, err := spam.Write(payload); err != nil {
break // queue full
}
}

done := make(chan error, 1)
go func() { done <- Ready() }()
select {
case err := <-done:
if err == nil {
t.Skip("kernel delivered a fresh sender's datagram despite the full queue; the blocking path is not exercisable here")
}
// An error is the pass: Send failed fast instead of hanging.
case <-time.After(20 * time.Second):
t.Fatal("Send blocked on an undrained socket past its deadline (#5428)")
}
}
Loading