instancectl: the start-lock test orders B's hang-up instead of racing it (#420)
TestAStartWhoseRequestEndsWhileWaitingForTheLockLaysNothingOut assumed the server noticed start B's client hang-up before start A released the unit's lock; under CI load it failed 5-40% of runs (about 90% at GOMAXPROCS=1), on main and branches alike. The hook now waits for B to hold a lock reference, cancels B, and waits for the reference to drop before A goes on. Test only. 150/150 under load; still fails when holdRootfsUnit ignores its context. Squashed from fix-rootfs-lock-test-420 into main.
1 file changed,
+44
−8
internal/instancectl/rootfs_test.go+44 −8
CI passed — 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 { |