fix: enforce task state machine on task status writes - #97
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The task state machine was documented but never enforced.
openjd.ValidateTaskTransitionencoded the legal arrows and was well tested, but had zero non-test callers —
store.UpdateTaskStatuswrote whatever status it was handed.What changed
internal/store(openjdimportsstore, so the store could notimport it back).
openjdkeeps the step machine; its dead task half is removed rather thanleft as a second table that can drift.
UpdateTaskStatusnow validates before writing, in both implementations — SQLite reads andwrites in one transaction (serialized by the single-connection pool, mirroring
TryClaimSlots),the fake under its mutex. An illegal arrow returns
store.ErrInvalidTransitionand leaves therow untouched.
Two rules that make enforcement safe under at-least-once delivery
runningmessages areroutine on JetStream.
handleTaskStatusMessageNaks on error, so without this a single stale message would redeliver forever.
Deliberately legal:
assigned→succeeded/failedThis looks like a skipped state but is reachable in normal operation: the worker publishes
runningfirst, butstatus.Publisher.publishWithRetrygives up afterMaxRetriesand returns,so that message can be lost while the task still completes. Rejecting the terminal message would
strand finished work until the heartbeat sweep reclaimed it.
Cancellation
CancelTask's terminal guard is a separate read from its write, so a task can complete inbetween and the state machine then rejects the cancel. That is treated as the no-op it would have
been had the guard seen the newer value. It is narrow by construction: every non-terminal status
has a legal arrow to
canceled, soErrInvalidTransitionon that write can only mean "alreadyterminal". Other store errors still propagate — there is a test pinning that.
Notes for review
machine by construction. The fixture churn is test-only.
faked
failed → readyviaUpdateTaskStatus, where production'sRetryTaskswritesfailed → pendingwith its own SQL; and a scheduler harness droveready → failed, skippingassignment. Both now match production.
RetryTasks,TransitionStepPendingTasks,CancelJobTasks, reclaim sweeps) keeptheir own guarded SQL and do not route through
UpdateTaskStatus.walkTaskTohelper.Developed test-first;
make ciandmake test-integrationgreen.