carlos / platform Public

instancectl test: order the lock-wait start's hang-up explicitly (#420, test only)

TestAStartWhoseRequestEndsWhileWaitingForTheLockLaysNothingOut ended
start B with a 100 ms client deadline and let start A go on as soon as
the client's Do returned. B's request context ends only when net/http's
background read sees the closed connection; when A's release woke B
first, B took the lock with a live context and started the unit a
second time (about 90% of runs at GOMAXPROCS=1, 5-40% under load, on
main and on every branch). The production code is right: a server
cannot act on a hang-up it has not seen (#292).

The hook now orders it: B runs in a goroutine with a cancellable
context; the hook waits for the unit's lock to have two references (A's
hold, B's wait), cancels B and waits for Do, then waits for one
reference (B saw its request end and let go) before A goes on. 1 ms
polls, 5 s deadlines, t.Errorf on a timeout. rootfsLockRefs is a
test-only helper beside the test.

150/150 at -count=50 -cpu 1,4,16; with holdRootfsUnit ignoring its
context the test still fails (6/6).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev fa7399ea1a0128c8d026a837002a24e573234f22 parent a64f5a4
1 file changed, +44 −8
  • internal/instancectl/rootfs_test.go +44 −8

CI cancelled — superseded by a newer push — run details

diff --git a/internal/instancectl/rootfs_test.go b/internal/instancectl/rootfs_test.go
index add372ca4..d77ee71dd 100644
--- a/internal/instancectl/rootfs_test.go
+++ b/internal/instancectl/rootfs_test.go
@@ -2617,29 +2617,65 @@ func TestRootfsUnitLockEntriesGoWithTheirLastHolder(t *testing.T) {
}
}
+// rootfsLockRefs is how many holds and waits unit's lock entry has now
+// (zero when it has no entry): a test orders a waiter against a holder by
+// it.
+func (s *Server) rootfsLockRefs(unit string) int {
+ s.rootfsLocksMu.Lock()
+ defer s.rootfsLocksMu.Unlock()
+ if e := s.rootfsLocks[unit]; e != nil {
+ return e.refs
+ }
+ return 0
+}
+
// Task 11 fix L3: a start waiting for the unit's lock gives up when its
// request ends first, before any layout: start B, issued while start A is
-// inside its systemctl start with a request that ends in 100 ms, never
-// starts the unit, so systemd sees exactly one start.
+// inside its systemctl start and whose request ends while it waits for the
+// lock, never starts the unit, so systemd sees exactly one start.
+//
+// The order is explicit (#420): A goes on only once B's handler has seen
+// its request end and dropped its reference. A client deadline alone
+// raced the server noticing the hang-up: when A's release woke B first, B
+// took the lock with a live context and started the unit again.
func TestAStartWhoseRequestEndsWhileWaitingForTheLockLaysNothingOut(t *testing.T) {
f := rootfsFixture(t)
unit := WorkerUnitName(f.host, "jobs")
q := f.startQuery(RoleWorker, "jobs", 0)
q.Set("worker", "jobs")
+ waitRefs := func(want int, what string) bool {
+ for end := time.Now().Add(5 * time.Second); time.Now().Before(end); time.Sleep(time.Millisecond) {
+ if f.srv.rootfsLockRefs(unit) == want {
+ return true
+ }
+ }
+ t.Errorf("the unit's lock never had %d references (%s); it has %d", want, what, f.srv.rootfsLockRefs(unit))
+ return false
+ }
f.ctl.mu.Lock()
f.ctl.before = map[string]func(){"start": func() {
- ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
- defer cancel()
+ ctx, cancel := context.WithCancel(context.Background())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, f.ts.URL+"/start?"+q.Encode(), nil)
if err != nil {
+ cancel()
t.Error(err)
return
}
- resp, err := f.ts.Client().Do(req)
- if err == nil {
- resp.Body.Close()
- t.Errorf("start B answered %d while start A held the unit's lock", resp.StatusCode)
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ if resp, err := f.ts.Client().Do(req); err == nil {
+ resp.Body.Close()
+ t.Errorf("start B answered %d while start A held the unit's lock", resp.StatusCode)
+ }
+ }()
+ defer func() { cancel(); <-done }()
+ if !waitRefs(2, "A's hold and B's wait") {
+ return
}
+ cancel()
+ <-done
+ waitRefs(1, "B saw its request end and let go")
}}
f.ctl.mu.Unlock()
if status, body := f.post(t, "/start", q); status != 200 {