From 940e4c2c6708fb0182262ac85aeb074d0c47e2d3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 22 Nov 2026 01:18:33 +0000 Subject: [PATCH] ssh/tailssh: write watchdog test PID markers atomically TestMonitorCloseWatchdogForwardChild occasionally failed on Linux CI with `invalid helper PID marker ".../forward-pid": ""`. The re-exec'd helper wrote its PID marker with os.WriteFile, which creates (and truncates) the file before writing the contents. The test polls for the marker every 20ms and treats any successful read as final, so a poll landing between the create and the write saw an empty file and failed. This is a test-only race; the monitor itself was fine. Write the marker to a temp file and rename it into place, so the poll sees either no file or the full PID. The natural rate is too low to measure locally (0/168 runs of the ~14s-under-race test in 5m with flakestress -race -p 8 -taskset 0-3), so the window was widened by injecting a 100ms sleep: before (sleep between create and write): 3/3 runs failed with the CI error. after (sleep between write and rename): 0/3 runs failed. after, without injection, flakestress -race -p 4, 5m: 0/84 runs. Fixes #22559 Change-Id: I855d18f006535140bc136ee876b311212f8884b6 Signed-off-by: Brad Fitzpatrick --- ssh/tailssh/monitor_watchdog_linux_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ssh/tailssh/monitor_watchdog_linux_test.go b/ssh/tailssh/monitor_watchdog_linux_test.go index ca1e0ec3f..fbec8a88d 100644 --- a/ssh/tailssh/monitor_watchdog_linux_test.go +++ b/ssh/tailssh/monitor_watchdog_linux_test.go @@ -26,7 +26,12 @@ func TestMonitorWatchdogHelperProcess(t *testing.T) { continue } signal.Ignore(syscall.SIGHUP) - if err := os.WriteFile(os.Args[i+2], []byte(strconv.Itoa(os.Getpid())), 0600); err != nil { + // Write then rename so the polling test never reads a partial marker. + marker := os.Args[i+2] + if err := os.WriteFile(marker+".tmp", []byte(strconv.Itoa(os.Getpid())), 0600); err != nil { + os.Exit(3) + } + if err := os.Rename(marker+".tmp", marker); err != nil { os.Exit(3) } time.Sleep(25 * time.Second)