diff --git a/pkgs/mcpx/internal/sdnotify/sdnotify.go b/pkgs/mcpx/internal/sdnotify/sdnotify.go index fdd3271b74..eaa6f378fb 100644 --- a/pkgs/mcpx/internal/sdnotify/sdnotify.go +++ b/pkgs/mcpx/internal/sdnotify/sdnotify.go @@ -7,8 +7,16 @@ import ( "fmt" "net" "os" + "time" ) +// writeTimeout bounds one notify write. sd_notify is fire-and-forget: +// a daemon must never wedge on an unresponsive socket (in some +// container environments an abstract unixgram write blocks instead of +// erroring — run 41819578987 hung 1800s there), so the write carries +// a deadline and the failure surfaces as an ordinary error. +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 { @@ -23,6 +31,7 @@ func Send(state string) error { return fmt.Errorf("notify %s: %w", path, err) } defer func() { _ = conn.Close() }() + _ = conn.SetWriteDeadline(time.Now().Add(writeTimeout)) 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..2b74441802 100644 --- a/pkgs/mcpx/internal/sdnotify/sdnotify_test.go +++ b/pkgs/mcpx/internal/sdnotify/sdnotify_test.go @@ -65,8 +65,21 @@ func TestAbstractSocketName(t *testing.T) { conn := listen(t, name) t.Setenv("NOTIFY_SOCKET", name) - if err := Ready(); err != nil { - t.Fatal(err) + // In some container environments an abstract unixgram write blocks + // indefinitely instead of completing or erroring (run 41819578987 + // hung the full 1800s test timeout, cascading an unrelated build). + // Send is deadline-bounded in production; if even that does not + // return here, the environment cannot do abstract sockets — skip + // with the reason rather than hang the suite. + done := make(chan error, 1) + go func() { done <- Ready() }() + select { + case err := <-done: + if err != nil { + t.Fatal(err) + } + case <-time.After(15 * time.Second): + t.Skipf("abstract unixgram notify to %s did not complete in 15s; this environment blocks abstract-socket writes", name) } if got := next(t, conn); got != "READY=1" { t.Fatalf("Ready sent %q", got)