Skip to content

fix(manager): stop old project work from overlapping a handoff - #154

Merged
scotwells merged 5 commits into
mainfrom
fix/manager-lifecycle-safety
Nov 24, 2026
Merged

scotwells merged 5 commits into
mainfrom
fix/manager-lifecycle-safety

Conversation

@scotwells

@scotwells scotwells commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three races in how the fleet manager hands off a project. A project could leave, or come back, while work from its old runtime was still running and could still write.

Related to #90. These fixes come from that inventory feature branch. They don't depend on inventory, so they land first, on their own.

What changes

  • Leaving waits for running work. The manager waits for reconciles already in flight before it drops a departing project. A stuck reconcile now delays the departure instead of running on after it. Work that started before the departure isn't undone.
  • Coming back waits for the old runtime. The new runtime starts only after the old one drains, so old and new work can't overlap. If the project changed owners during the wait, the manager refuses the handoff. A cancelled handoff starts nothing.
  • One handoff at a time. Two handoffs of the same project can't interleave while controllers are being set up.
  • The development guide describes each wait and its limit. They work inside one manager process. They don't fence executors across machines.

Test plan

  • New tests hold a reconcile open and check each wait, including cancellation, stale runtimes, owner changes and two handoffs at once
  • Full suite, including the API server tests, passes with the race detector
  • The new tests passed 100 more race-detector runs

🤖 Generated with Claude Code

@scotwells
scotwells requested a review from a team as a code owner November 23, 2026 22:17
@scotwells
scotwells requested review from ecv and kevwilliams November 23, 2026 22:17
ecv
ecv previously approved these changes Nov 23, 2026

@ecv ecv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve.

Departure and re-engagement now wait for running reconciles before teardown or replacement. go test -race -count=1 ./internal/manager/... passes at 8cac778, and removing the acquire() gate in both reconcilers fails TestProjectDepartureDrainsReconciliation, TestProjectReengagementDrainsReconciliation and TestProjectEngagementSerializesDownstreamSetupAndCleanup.

  • A hung reconcile blocks teardown forever. drain() takes reconcileMu.Lock() with no bound, and unbindLocked calls stop() while holding the per-cluster engagement token. The cancel does reach the engage context, so a reconcile that honours its context ends. One that ignores it stalls every later Engage of that cluster name until the process restarts. Waiters that carry a ctx do return on cancel.

Follow-ups:

  • Bound the drain wait (timeout plus a log line naming the project) so a stuck reconcile surfaces instead of wedging re-engagement silently.
  • A failed re-engage after the prior runtime drained leaves the project unregistered until discovery retries; the doc says so, but there is no metric or alert for a project stuck unengaged.
  • Nothing mutation-tests the drainOnce / stopped pair: dropping only the Lock() wait in drain() was not checked (my sed broke the build), so confirm one test fails without the wait.

@scotwells scotwells changed the title Fleet manager waits for running work before a project leaves or returns fix(manager): stop old project work from overlapping a handoff Nov 23, 2026
@scotwells

Copy link
Copy Markdown
Contributor Author

One stress run of the manager tests failed before these numbers were taken. I didn't keep its output, and 108 later runs passed, as did a reviewer's 90 runs with shuffling across 1, 2 and 8 CPUs. If it shows up in CI, the cause is still unknown.

@kevwilliams kevwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold: provider-event Machine reconciles bypass the drain, so old work can still overlap a handoff.

  • ClusterProviderEventReconciler.Reconcile (provider_events.go:137) takes r.Projects.Get and calls runtime.Machine without runtime.acquire(); a temporary probe test saw departure finish while that handler was still running.
  • Fix: if !runtime.acquire() { return } plus defer runtime.reconcileMu.RUnlock() there; staging.EngagedPublisher writes through runtime.Binding.Client the same way.
  • Test: add a provider-event case to the drain tests.

Provider MachineCapacity events and the synthetic publisher resolved the
engaged runtime without acquiring it, so departure or re-engagement could
finish while one of them was still writing through the old runtime.

Both now acquire the runtime first and skip work once it is draining.
The departure drain test gains a provider-event case, which fails
without the gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scotwells

Copy link
Copy Markdown
Contributor Author

@kevwilliams the hold is fixed in fe3a15b:

  • The provider-event reconciler now acquires the runtime before it runs, and skips work once the runtime is draining.
  • The synthetic publisher does the same, through a new exported acquire on the runtime, before it writes through the consumer client.
  • The departure drain test has a provider-event case. It fails with "departure completed while a handler could still write" when the gate is removed.

@ecv on your follow-up about the drain wait: removing the lock wait in the drain fails five tests, including both drain tests and the re-engagement registry test. The time limit on the wait and the alert for a project left unengaged are still open as follow-ups.

@scotwells
scotwells requested review from ecv and kevwilliams November 24, 2026 15:04

@kevwilliams kevwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified removing the new provider-event acquire fails the provider_event drain case under race; the publisher gate copies it but has no test of its own.

@ecv ecv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve.

The re-review delta closes the provider-event gap from the hold on 8cac778. ClusterProviderEventReconciler and EngagedPublisher now take the runtime's reconcile gate before writing, and both return without work once the runtime is draining. The provider-event path joins the departure drain test.

Follow-ups:

  • The EngagedPublisher gate has no test. Removing it passes the suite.
  • My earlier follow-ups stand: an unbounded drain wait, and no signal for a project left unengaged after a failed re-engage.

@scotwells
scotwells merged commit 87fbf5e into main Nov 24, 2026
6 checks passed
@scotwells
scotwells deleted the fix/manager-lifecycle-safety branch November 24, 2026 15:09
@ecv

ecv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up: #169
Follow-up: #170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants