activator: executables written under ForkLock; marker waits need content
Two load flakes in the exec tests: - "fork/exec .../versions/v2/app: text file busy" (TestExecBackendStartSidecarGen, CI 2026-10-27). A test wrote a script with os.WriteFile and then execed it while parallel tests forked. A fork in that window inherits the write descriptor until its child execs, and the exec fails with ETXTBSY (Go issue 22315). writeExecutable holds syscall.ForkLock for reading while the file is open, so no fork can happen then. Every test write of an executable in the package uses it. A probe with four goroutines forking /bin/true: os.WriteFile 30 of 400 execs ETXTBSY, writeExecutable 0. - waitForFile returned "" when the child's shell had created the file for its redirect but printf had not written yet (TestExecBackendStartStagedRunsAForeignRowOnItsCanonicalSocket, 1 in 780 under load). It now waits for content; every recorder writes at least a newline or a path, in one write. Under load (2 cores, 4 busy loops, nice 19, -cpu 4,8) the exec, staged and sidecar tests: before 1 failure in 780, after 0 in 1300.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
14 files changed,
+54
−32
internal/activator/activator_test.go+14 −10internal/activator/binary_golden_test.go+1 −1internal/activator/execwrite_test.go+19 −0internal/activator/genbackend_test.go+5 −5internal/activator/identity_test.go+3 −3internal/activator/replica_instancekey_test.go+1 −1internal/activator/rolelaunch_root_linux_test.go+1 −1internal/activator/swap_fixture_test.go+1 −1internal/activator/swap_sidecar_test.go+2 −2internal/activator/swap_sim_test.go+1 −1internal/activator/wakeidentity_test.go+1 −2internal/activator/wakeplan_test.go+2 −2internal/activator/wakerecord_refused_test.go+1 −1internal/activator/worker_staged_test.go+2 −2
Intent
diff --git a/internal/activator/activator_test.go b/internal/activator/activator_test.go| index 49684b033..0571d8619 100644 |
| --- a/internal/activator/activator_test.go |
| +++ b/internal/activator/activator_test.go |
| @@ -4661,7 +4661,7 @@ func stageLive(t *testing.T, b *ExecBackend, host, ver string) { |
| t.Fatal(err) |
| } |
| bin := filepath.Join(vdir, "app") |
| - if err := os.WriteFile(bin, []byte("#!/bin/sh\n"), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte("#!/bin/sh\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -4773,7 +4773,7 @@ func stageLiveUnderAccount(t *testing.T, optDir, host, acct, ver, app string) { |
| if err := os.MkdirAll(dir, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(filepath.Join(dir, app), []byte("bin"), 0o755); err != nil { |
| + if err := writeExecutable(filepath.Join(dir, app), []byte("bin"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(optDir, "live"), 0o755); err != nil { |
| @@ -5075,7 +5075,7 @@ func stageRecordingLive(t *testing.T, b *ExecBackend, host, ver string) (argvPat |
| t.Fatal(err) |
| } |
| bin := filepath.Join(vdir, "app") |
| - if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte(script), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -5108,7 +5108,7 @@ func stageRecordingLiveFullEnv(t *testing.T, b *ExecBackend, host, ver string) ( |
| t.Fatal(err) |
| } |
| bin := filepath.Join(vdir, "app") |
| - if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte(script), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -5121,13 +5121,17 @@ func stageRecordingLiveFullEnv(t *testing.T, b *ExecBackend, host, ver string) ( |
| return argvPath, envPath |
| } |
| -// waitForFile polls until path exists (the child writes it concurrently with |
| -// Start's health wait) and returns its contents. |
| +// waitForFile polls until path exists and has something in it (the child |
| +// writes it concurrently with Start's health wait) and returns its |
| +// contents. Existing is not enough: the child's shell creates the file for |
| +// its redirect before printf writes to it, and a read in between saw "" |
| +// (CI-load repro, TestExecBackendStartStagedRunsAForeignRowOnItsCanonicalSocket). |
| +// Every recorder writes at least a newline or a path, in one write. |
| func waitForFile(t *testing.T, path string, within time.Duration) string { |
| t.Helper() |
| deadline := time.Now().Add(within) |
| for time.Now().Before(deadline) { |
| - if b, err := os.ReadFile(path); err == nil { |
| + if b, err := os.ReadFile(path); err == nil && len(b) > 0 { |
| return string(b) |
| } |
| time.Sleep(10 * time.Millisecond) |
| @@ -5722,7 +5726,7 @@ func stageSeedScript(t *testing.T, b *ExecBackend, host, ver, body string) { |
| t.Fatal(err) |
| } |
| bin := filepath.Join(vdir, "app") |
| - if err := os.WriteFile(bin, []byte("#!/bin/sh\n"+body), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte("#!/bin/sh\n"+body), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -6316,7 +6320,7 @@ func TestExecBackendDoesNotNormalizeSquatterSocket(t *testing.T) { |
| t.Fatal(err) |
| } |
| bin := filepath.Join(vdir, "app") |
| - if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte(script), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -6397,7 +6401,7 @@ func TestChildProcStopKeepsTheControlSocketUpUntilTheChildExits(t *testing.T) { |
| "touch '" + ready + "'\n" + |
| "while [ ! -f '" + release + "' ]; do sleep 0.02; done\n" + |
| "exit 0\n" |
| - if err := os.WriteFile(script, []byte(body), 0o755); err != nil { |
| + if err := writeExecutable(script, []byte(body), 0o755); err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/activator/binary_golden_test.go b/internal/activator/binary_golden_test.go| index 428c5a943..cc7aeb82b 100644 |
| --- a/internal/activator/binary_golden_test.go |
| +++ b/internal/activator/binary_golden_test.go |
| @@ -224,7 +224,7 @@ func TestBinaryStartStagedGolden(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nprintf '%s\\n' \"$0\" \"$@\" > '"+marker+"'\nexec sleep 30\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nprintf '%s\\n' \"$0\" \"$@\" > '"+marker+"'\nexec sleep 30\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), |
diff --git a/internal/activator/execwrite_test.go b/internal/activator/execwrite_test.go| new file mode 100644 |
| index 000000000..4f7edafb1 |
| --- /dev/null |
| +++ b/internal/activator/execwrite_test.go |
| @@ -0,0 +1,19 @@ |
| +package activator |
| + |
| +import ( |
| + "os" |
| + "syscall" |
| +) |
| + |
| +// writeExecutable is os.WriteFile for a file a test will exec. It holds |
| +// syscall.ForkLock for reading while the file is open for writing, so no |
| +// other goroutine can fork in that window. Without it, a parallel test's |
| +// fork could inherit the write descriptor and keep it until its child |
| +// execs, and an exec of this file in between fails with ETXTBSY ("text |
| +// file busy", CI 2026-10-27, TestExecBackendStartSidecarGen; Go issue |
| +// 22315). |
| +func writeExecutable(name string, data []byte, perm os.FileMode) error { |
| + syscall.ForkLock.RLock() |
| + defer syscall.ForkLock.RUnlock() |
| + return os.WriteFile(name, data, perm) |
| +} |
diff --git a/internal/activator/genbackend_test.go b/internal/activator/genbackend_test.go| index 5dac8fc54..26583dc94 100644 |
| --- a/internal/activator/genbackend_test.go |
| +++ b/internal/activator/genbackend_test.go |
| @@ -22,7 +22,7 @@ import ( |
| func TestVerifyStaged(t *testing.T) { |
| dir := t.TempDir() |
| bin := filepath.Join(dir, "app") |
| - if err := os.WriteFile(bin, []byte("build v2"), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte("build v2"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum := sha256.Sum256([]byte("build v2")) |
| @@ -113,7 +113,7 @@ func TestExecBackendStartStagedExecsTheStagedPath(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), |
| @@ -143,7 +143,7 @@ func TestExecBackendStartStagedReturnsTheProcessAtOnce(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { // never binds |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { // never binds |
| t.Fatal(err) |
| } |
| rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), |
| @@ -187,7 +187,7 @@ func TestExecBackendStartStagedRunsAForeignRowOnItsCanonicalSocket(t *testing.T) |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), |
| @@ -218,7 +218,7 @@ func TestExecBackendStartStagedClearsALeftoverCanonicalLink(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), |
diff --git a/internal/activator/identity_test.go b/internal/activator/identity_test.go| index 23ea4b384..90f02dc7c 100644 |
| --- a/internal/activator/identity_test.go |
| +++ b/internal/activator/identity_test.go |
| @@ -29,7 +29,7 @@ func stageVersion(t *testing.T, b *ExecBackend, host, ver, body string) string { |
| if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(bin, []byte("#!/bin/sh\n# "+ver+"\n"+body), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte("#!/bin/sh\n# "+ver+"\n"+body), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| repointLiveLink(t, b, host, bin) |
| @@ -566,7 +566,7 @@ func TestADeclinedLinkOutsideTheStoreAsksRootForNothing(t *testing.T) { |
| b.Control = identityControl{ctl} |
| rt.Seed = `["seed"]` |
| hand := filepath.Join(t.TempDir(), "app") |
| - if err := os.WriteFile(hand, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { |
| + if err := writeExecutable(hand, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| repointLiveLink(t, b, rt.Host, hand) |
| @@ -689,7 +689,7 @@ func TestBuildIdentityNamesTheStagedCopy(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(path, []byte("#!/bin/sh\n# v1\n"), 0o755); err != nil { |
| + if err := writeExecutable(path, []byte("#!/bin/sh\n# v1\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum, err := release.FileSHA256(path) |
diff --git a/internal/activator/replica_instancekey_test.go b/internal/activator/replica_instancekey_test.go| index 0e15f8b18..ab5c47d86 100644 |
| --- a/internal/activator/replica_instancekey_test.go |
| +++ b/internal/activator/replica_instancekey_test.go |
| @@ -82,7 +82,7 @@ func stageHelperApp(t *testing.T, b *ExecBackend, host, ver string) string { |
| } |
| bin := filepath.Join(vdir, "app") |
| script := fmt.Sprintf("#!/bin/sh\nexec '%s' -test.run='^TestInstanceKeyHelperProcess$'\n", self) |
| - if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte(script), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
diff --git a/internal/activator/rolelaunch_root_linux_test.go b/internal/activator/rolelaunch_root_linux_test.go| index 6fdd808bf..e64d4a93a 100644 |
| --- a/internal/activator/rolelaunch_root_linux_test.go |
| +++ b/internal/activator/rolelaunch_root_linux_test.go |
| @@ -92,7 +92,7 @@ func newRootLaunchFixture(t *testing.T) *rootLaunchFixture { |
| t.Fatal(err) |
| } |
| } |
| - if err := os.WriteFile(f.exe, []byte("build"), 0o555); err != nil { |
| + if err := writeExecutable(f.exe, []byte("build"), 0o555); err != nil { |
| t.Fatal(err) |
| } |
| var st unix.Stat_t |
diff --git a/internal/activator/swap_fixture_test.go b/internal/activator/swap_fixture_test.go| index caf99676c..78afa4b3d 100644 |
| --- a/internal/activator/swap_fixture_test.go |
| +++ b/internal/activator/swap_fixture_test.go |
| @@ -301,7 +301,7 @@ func newSwapFixtureOn(t *testing.T, wrap func(*fakeSwapBackend) Backend) *swapFi |
| t.Fatal(err) |
| } |
| f.staged = filepath.Join(dir, "app") |
| - if err := os.WriteFile(f.staged, []byte("build v2"), 0o755); err != nil { |
| + if err := writeExecutable(f.staged, []byte("build v2"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum := sha256.Sum256([]byte("build v2")) |
diff --git a/internal/activator/swap_sidecar_test.go b/internal/activator/swap_sidecar_test.go| index 326f91549..8da368f19 100644 |
| --- a/internal/activator/swap_sidecar_test.go |
| +++ b/internal/activator/swap_sidecar_test.go |
| @@ -587,7 +587,7 @@ func TestStartSidecarGenKeepsTheRoleSidecarChild(t *testing.T) { |
| t.Fatal(err) |
| } |
| // It stays up until the stop: its exit removes its own child. |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.WriteFile(filepath.Join(b.DataDir, host+"-sidecar.env"), []byte("X=1\n"), 0o600); err != nil { |
| @@ -627,7 +627,7 @@ func TestExecBackendStartSidecarGen(t *testing.T) { |
| if err := os.MkdirAll(filepath.Dir(v2), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - if err := os.WriteFile(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| + if err := writeExecutable(v2, []byte("#!/bin/sh\nprintf '%s' \"$0\" > '"+marker+"'\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.WriteFile(filepath.Join(b.DataDir, host+"-sidecar.env"), []byte("X=1\n"), 0o600); err != nil { |
diff --git a/internal/activator/swap_sim_test.go b/internal/activator/swap_sim_test.go| index 4362f8540..2f0e3d710 100644 |
| --- a/internal/activator/swap_sim_test.go |
| +++ b/internal/activator/swap_sim_test.go |
| @@ -126,7 +126,7 @@ func stageSimBuilds(t *testing.T) map[string]simArtifact { |
| } |
| p := filepath.Join(dir, "app") |
| body := []byte("build " + b) |
| - if err := os.WriteFile(p, body, 0o755); err != nil { |
| + if err := writeExecutable(p, body, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum := sha256.Sum256(body) |
diff --git a/internal/activator/wakeidentity_test.go b/internal/activator/wakeidentity_test.go| index 6c317d1fd..3cc2fe9ae 100644 |
| --- a/internal/activator/wakeidentity_test.go |
| +++ b/internal/activator/wakeidentity_test.go |
| @@ -4,7 +4,6 @@ import ( |
| "context" |
| "errors" |
| "fmt" |
| - "os" |
| "path/filepath" |
| "slices" |
| "strings" |
| @@ -567,7 +566,7 @@ func TestANestedUnversionedLinkWithNoHookWakesAsToday(t *testing.T) { |
| eb, rt := nestedBackend(t, ctl, unit) |
| eb.Control = rootLikeControl{identityControl{ctl}} |
| hand := filepath.Join(t.TempDir(), "app") |
| - if err := os.WriteFile(hand, []byte("#!/bin/sh\n"), 0o755); err != nil { |
| + if err := writeExecutable(hand, []byte("#!/bin/sh\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| repointLiveLink(t, eb, rt.Host, hand) |
diff --git a/internal/activator/wakeplan_test.go b/internal/activator/wakeplan_test.go| index b0c767c4e..ccf333ea7 100644 |
| --- a/internal/activator/wakeplan_test.go |
| +++ b/internal/activator/wakeplan_test.go |
| @@ -134,7 +134,7 @@ func stagedID(t *testing.T, dir, ver string) release.StagedIdentity { |
| t.Helper() |
| body := []byte("build " + ver) |
| path := filepath.Join(dir, ver) |
| - if err := os.WriteFile(path, body, 0o755); err != nil { |
| + if err := writeExecutable(path, body, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum := sha256.Sum256(body) |
| @@ -675,7 +675,7 @@ func (b *execStagedBackend) KillGen(ctx context.Context, _ registry.Route, n uin |
| func stagedScript(t *testing.T, dir, ver, body string) release.StagedIdentity { |
| t.Helper() |
| path := filepath.Join(dir, ver) |
| - if err := os.WriteFile(path, []byte(body), 0o755); err != nil { |
| + if err := writeExecutable(path, []byte(body), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| sum := sha256.Sum256([]byte(body)) |
diff --git a/internal/activator/wakerecord_refused_test.go b/internal/activator/wakerecord_refused_test.go| index 2c46a9fad..c94a829d7 100644 |
| --- a/internal/activator/wakerecord_refused_test.go |
| +++ b/internal/activator/wakerecord_refused_test.go |
| @@ -38,7 +38,7 @@ func (b *execIdentityBackend) identityFor(registry.Route) identityBackend { retu |
| func stageScript(t *testing.T, eb *ExecBackend, host, body string) { |
| t.Helper() |
| stageLive(t, eb, host, "v1") |
| - if err := os.WriteFile(filepath.Join(eb.OptDir, "versions", "v1", "app"), []byte(body), 0o755); err != nil { |
| + if err := writeExecutable(filepath.Join(eb.OptDir, "versions", "v1", "app"), []byte(body), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| } |
diff --git a/internal/activator/worker_staged_test.go b/internal/activator/worker_staged_test.go| index 9939d4243..b20dfe35d 100644 |
| --- a/internal/activator/worker_staged_test.go |
| +++ b/internal/activator/worker_staged_test.go |
| @@ -101,7 +101,7 @@ func stageArgv0Worker(t *testing.T, b *ExecBackend, host, ver, out string) strin |
| } |
| bin := filepath.Join(vdir, "app") |
| script := "#!/bin/sh\n# build " + ver + "\nprintf '%s\\n' \"$0\" > '" + out + ".tmp' && mv '" + out + ".tmp' '" + out + "'\nexec sleep 30\n" |
| - if err := os.WriteFile(bin, []byte(script), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte(script), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.MkdirAll(filepath.Join(b.OptDir, "live"), 0o755); err != nil { |
| @@ -187,7 +187,7 @@ func TestExecBackendWorkerRecycleRefusesAChangedStagedCopy(t *testing.T) { |
| waitForFile(t, out, 5*time.Second) |
| p1.Stop(context.Background()) |
| os.Remove(out) |
| - if err := os.WriteFile(bin, []byte("#!/bin/sh\n# tampered, and longer than the original\nexec sleep 30\n"), 0o755); err != nil { |
| + if err := writeExecutable(bin, []byte("#!/bin/sh\n# tampered, and longer than the original\nexec sleep 30\n"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| p2, _, err := b.StartWorkerStaged(context.Background(), workerRoute(host), nil, &id1) |