Skip to content

restart: fix DelayStart store-lock deadlock - #3315

Open
SabyaXSachi wants to merge 1 commit into
moby:masterfrom
SabyaXSachi:fix/54128-delaystart-store-lock-deadlock
Open

SabyaXSachi wants to merge 1 commit into
moby:masterfrom
SabyaXSachi:fix/54128-delaystart-store-lock-deadlock

Conversation

@SabyaXSachi

Copy link
Copy Markdown

What I did

Fixed a deadlock in Supervisor.DelayStart that could block the Swarm manager's store write lock indefinitely.

When a delayed start was replaced while the caller held the store write lock, the existing implementation cancelled the previous delay and synchronously waited for its goroutine to finish. If that goroutine had already passed its timers and was waiting to acquire the same store lock through MemoryStore.Update, neither goroutine could proceed.

Fixes moby/moby#54128.

How I did it

  • Removed the synchronous wait for a superseded delayed-start goroutine.
  • Cancelled the previous delay and registered its replacement atomically under r.mu.
  • Added a cancellation check inside the store transaction callback to prevent a superseded delay from performing a stale StartNow.
  • Added identity-guarded deferred cleanup so an older goroutine cannot delete a newer delay's map entry.
  • Preserved the existing context behavior and ensured each delayed-start goroutine closes its own doneCh exactly once.
  • Documented the requirement that callers invoke DelayStart while holding the store write lock.

How to test it

Added manager/orchestrator/restart/restart_test.go covering:

  • Replacing a delayed start while the store write lock is held.
  • Preventing stale StartNow execution after cancellation.
  • Identity-guarded cleanup.
  • Rapid repeated replacements.
  • The normal, non-superseded start path.

Validation performed:

  • Restart package tests passed.
  • Restart package race tests passed.
  • go test -race ./manager/orchestrator/... passed.
  • Store race tests were checked against the baseline; the pre-existing TestTimestamps failure was not introduced by this change.
  • Formatting and git diff --check passed.

Description for the changelog

Fix a deadlock in delayed task restart handling when replacing an existing delayed start while holding the store write lock.

AI assistance disclosure

AI tools assisted with investigation and implementation of this contribution. I reviewed the relevant source code, examined the concurrency behavior, reviewed the implementation and tests, and take responsibility for the submitted changes and their maintenance.

DelayStart cancelled an existing delayed start for the same task ID and
waited for its goroutine on doneCh while the caller held the store's
write lock. A superseded goroutine that had already passed its timers
sits blocked in MemoryStore.Update waiting for that same lock, so the
wait never finished: the updater's Batch deadlocked and every
subsequent store write queued behind the lock until process restart.
Reported in moby/moby#54128.

Cancel the superseded delay and register the replacement atomically
under r.mu instead of waiting. That a superseded goroutine cannot
perform a stale StartNow rests on the requirement, now documented,
that every DelayStart caller holds the store write lock: a
replacement therefore cannot run concurrently with a superseded
goroutine's store transaction, and the goroutine observes its
cancellation in the transaction callback and skips StartNow. The
deferred map cleanup is identity-guarded so a superseded goroutine
cannot delete a newer delay's entry, and doneCh is still closed
exactly once by the owning goroutine.

Adds restart_test.go covering: replacement while the write lock is
held (deadlocks before this change), stale-StartNow prevention,
identity-guarded cleanup, rapid repeated replacements, and the
non-superseded start path.

Fixes moby/moby#54128

Signed-off-by: SabyaXSachi <sabyasachipanda507@gmail.com>
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.

Swarm manager deadlock: updater waits in DelayStart while holding the store lock; every write then times out

1 participant