Skip to content

fix(scheduler): reap hung fires past FireDeadline (Fixes #2588) - #2631

Open
jayunit100 wants to merge 4 commits into
stacklok:mainfrom
jayunit100:fix/2588-reap-past-fire-deadline
Open

jayunit100 wants to merge 4 commits into
stacklok:mainfrom
jayunit100:fix/2588-reap-past-fire-deadline

Conversation

@jayunit100

Copy link
Copy Markdown

Summary

  • Reconcile stale in-flight schedule fires after FireDeadline lapses even when the prior session lease is still held, so singleton schedules are not stuck skipping every tick (production incident in Scheduler: hung schedule fire never reaped past Deadline; Singleton schedule stalls #2588).
  • Add regression tests at the scheduler adapter and app layers.
  • Emit searchable diagnostics and a distinct terminal fire Err when this path runs (reconcile stale fire past FireDeadline (issue #2588)).

Context

Patch, tests, and comments were drafted in Cursor (Composer 2.5 Fast) from the production write-up; please treat CI as the final validation gate.

Fixes #2588

Test plan

  • go test ./internal/adapter/scheduler/ -run 'Issue2588|ReconcileStaleFire' -count=1
  • go test ./internal/app/ -run Issue2588 -count=1 -timeout 2m
  • Confirm TestReconcileStaleFireLiveNotReconciled still passes (no FireDeadline → lease gate unchanged)

Made with Cursor

jay vyas and others added 2 commits October 28, 2026 12:06
…eld lease

When Singleton schedules hung past their explicit FireDeadline, stale-fire
reconciliation must not treat a still-held session lease as "live" forever.
Adds regression tests for issue stacklok#2588.

Co-authored-by: Cursor <[email protected]>
…lok#2588)

Add searchable diagnostics and a distinct terminal fire Err when reaping
hung in-flight fires past FireDeadline; extend regression test coverage.

Co-authored-by: Cursor <[email protected]>
jay vyas and others added 2 commits October 31, 2026 11:51
Allow stale-fire reconciliation while a hung fire is still in flight;
fix Issue2588 log test to use capturingDiag.msgs.

Co-authored-by: Cursor <[email protected]>
@peppescg

peppescg commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Hi @jayunit100 , thanks a lot for reporting this and for putting the PR together, and sorry it took us so long to get back to you. The write-up made it easy to dig in, and a scheduler that heals itself is exactly what we want.

I reproduced it locally. On current main, I created a schedule from a chat, so the schedule has an origin session. The first fire finished its own run, but the fire was never recorded. It stayed in flight past its deadline with no events, no Stop and no Err, kept its session lease, and the schedule stopped firing. Those are the same symptoms as in the issue.

Why reaping past the deadline isn't enough. The lease is held because the fire is still in progress in the process. The fire function is blocked, and it releases the lease only when it returns. So reaping it:

  • doesn't release the lease. The singleton trial-acquire still gets ErrLeaseHeld, so the schedule still doesn't fire again.
  • records a terminal StopError for a fire that is still running. In my repro, that fire later finished with end_turn.
  • goes against the guarantees from fix(scheduler): keep polling behind stuck fires without releasing ownership #2277: the name fence covers a fire until it really ends, and a replica must not settle another replica's live fire. Six existing tests cover these guarantees and fail on the branch, for example TestFireDeadlineNonclosingEventsKeepLeaseUntilRecovery and TestReplicaOwnershipAndCapacitySurviveEpochTurnover. CI hasn't run on the PR yet, which is why they didn't show up.

There is also a smaller issue. With the unlock moved into fireOne, any early return (singleton skip, misfire skip, failed Claim) leaves the name mutex locked, and the schedule stops firing for the rest of the process. Two more existing tests catch this: TestSingletonOverlapPreservesLivePointer and TestMisfireSkipLeaseBackendSettlesBeforeFreshSlot.

The real cause: your second suggestion. When a schedule's origin session is idle, the "fire started" notice runs a full turn in that session (deliverFireStarted in internal/app/scheduler_delivery_run.go). The fire waits for that run to finish before it reads its own events. That run has no fire deadline, and the deadline watchdog cancels only the fire's own run. If the origin's run stalls, the fire stays in flight with zero events, keeps its lease and blocks the schedule. That is "Detach fire context from origin session". The result notice (deliverFireResult) has the same pattern after RecordFire.

Suggested fix. Move only the origin session's run off the fire path:

  1. Split deliverFireStarted and deliverFireResult. Every decision stays where it is today: authorize, enqueue, busy or idle origin. Only the part that starts and drains the run in the origin (StartRunContent through MarkDelivered) is handed to a runner. Existing callers keep running it inline.
  2. In the scheduler wiring (startScheduler in internal/app/build.go), use a runner that runs those origin turns in the background. The fire then returns, records its outcome and releases its lease.
  3. Keep each fire's result notice after its start notice. If the start notice's run is still going, deliver the result after it, in the background.
  4. Don't cancel a slow origin run, because it's a real turn in the user's chat. Log a WARN once it runs past the schedule's fire timeout.
  5. On shutdown, wait for in-flight origin runs within the same 10s budget as the scheduler's fires.

The FireNow response doesn't change. Only the timing of the origin note changes: it may arrive just after FireNow returns, which the API doesn't promise. That's worth a line in the PR's API compatibility statement.

Tests that would show it works:

  • The issue scenario: the origin notice's run never finishes. The fire still completes before its deadline, its lease is released, the schedule fires again on the next tick, and the origin later receives the start note before the result note.
  • A fire whose own run ignores cancellation: while it's alive there is no second run and it isn't recorded as finished, and the schedule fires again once the run ends. This is the fix(scheduler): keep polling behind stuck fires without releasing ownership #2277 contract, and it already passes on main.
  • The existing scheduler and delivery tests stay green under -race.

We'll track the case where the fire's own run ignores cancellation in #2955, so there's no need to cover it here.

Happy to answer questions or take an early look while you're on it.

One question: was the stuck schedule created from a chat, so that it has an origin session? If not, the hang is somewhere else, for example the fire's own run ignoring cancellation, and the logs around the stuck fire would help. The issue mentions v0.0.47, which predates #2277, so a repro on a recent build would be great.

Thanks again!

@jayunit100

Copy link
Copy Markdown
Author

It was from a chat yup

@peppescg

Copy link
Copy Markdown
Contributor

Thanks for confirming, @jayunit100! That matches what I reproduced. With an origin session, the fire waits for the "fire started" turn in the chat, and v0.0.47 already has that same path, so it fits your version too.

Two small updates since my last comment:

We'll take a look whenever you update the PR, and I'm happy to help if anything is unclear.

This branch has not been deployed

No deployments
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.

Scheduler: hung schedule fire never reaped past Deadline; Singleton schedule stalls

2 participants