host: install-channel tests wait for the apply lock a fork may still hold
TestInstallChannelPolicyRefusals failed with "an update is running on this box, so nothing was installed: resource temporarily unavailable" (main c5d6d0f5, rootfs branch baa62c68). The fixture's apply closes its flock'd lock file when it returns, but a parallel test that forked while the file was open has a copy of the descriptor in its child until the child execs, and the flock stays held through it. The install that follows then finds the lock taken. A probe: 18 of 2000 closed locks were still held with four goroutines forking /bin/true. Production runs apply and install as one-shot processes, so this is the tests' own fork traffic, not a production race. applyChannelUpdateWith now waits (bounded, 10 s) until a probe can take and explicitly release the lock. With a parallel test forking /bin/true in a loop, under load (2 cores, 4 busy loops, nice 19, -cpu 2,4): before 45 of 60 runs failed, after 0 of 100.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+59
−0
internal/host/applylock_linux_test.go+49 −0internal/host/applylock_other_test.go+9 −0internal/host/installchannel_policy_test.go+1 −0
Intent
diff --git a/internal/host/applylock_linux_test.go b/internal/host/applylock_linux_test.go| new file mode 100644 |
| index 000000000..b5fcd9e4e |
| --- /dev/null |
| +++ b/internal/host/applylock_linux_test.go |
| @@ -0,0 +1,49 @@ |
| +//go:build linux |
| + |
| +package host |
| + |
| +import ( |
| + "errors" |
| + "os" |
| + "syscall" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// awaitApplyLockFree waits until nothing holds a's apply lock any more. |
| +// |
| +// An apply closes its lock when it returns, but a parallel test that forked |
| +// while the lock was open has a copy of that descriptor in its child until |
| +// the child execs, and the flock stays held through the copy. On a loaded |
| +// runner that lasts long enough for the next locker in this test to be |
| +// refused: "an update is running on this box ... resource temporarily |
| +// unavailable" (TestInstallChannelPolicyRefusals, CI 2026-10-26 and |
| +// 2026-10-27). The probe unlocks explicitly before closing, so a fork that |
| +// copies its own descriptor cannot hold the lock in turn. |
| +func awaitApplyLockFree(t *testing.T, a *systemApplier) { |
| + t.Helper() |
| + deadline := time.Now().Add(10 * time.Second) |
| + for { |
| + f, err := os.OpenFile(a.lockPath(), os.O_RDWR, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) |
| + if err == nil { |
| + err = syscall.Flock(int(f.Fd()), syscall.LOCK_UN) |
| + f.Close() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return |
| + } |
| + f.Close() |
| + if !errors.Is(err, syscall.EWOULDBLOCK) { |
| + t.Fatal(err) |
| + } |
| + if time.Now().After(deadline) { |
| + t.Fatalf("the apply lock %s is still held 10s after the apply returned", a.lockPath()) |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| +} |
diff --git a/internal/host/applylock_other_test.go b/internal/host/applylock_other_test.go| new file mode 100644 |
| index 000000000..6c9f6d283 |
| --- /dev/null |
| +++ b/internal/host/applylock_other_test.go |
| @@ -0,0 +1,9 @@ |
| +//go:build !linux |
| + |
| +package host |
| + |
| +import "testing" |
| + |
| +// awaitApplyLockFree has nothing to wait for where the apply lock is not |
| +// supported (flockExclusiveNonblock is errSystemUnsupported). |
| +func awaitApplyLockFree(*testing.T, *systemApplier) {} |
diff --git a/internal/host/installchannel_policy_test.go b/internal/host/installchannel_policy_test.go| index db8f0f92f..02d65f9a4 100644 |
| --- a/internal/host/installchannel_policy_test.go |
| +++ b/internal/host/installchannel_policy_test.go |
| @@ -99,6 +99,7 @@ func applyChannelUpdateWith(t *testing.T, f *channelFixture, version string, bod |
| if err := f.a.Run(ctx, false); err != nil { |
| t.Fatal(err) |
| } |
| + awaitApplyLockFree(t, f.a) |
| if r := f.result(); r == nil || r.Outcome != systemOutcomeApplied || r.Version != version { |
| t.Fatalf("the update to %s did not apply: %+v", version, r) |
| } |