backend: recover studies stuck in "running" — startup reconciliation, CancelledError handling, strong task refs - #42
Open
ChrisW09 wants to merge 1 commit into
Open
backend: recover studies stuck in "running" — startup reconciliation, CancelledError handling, strong task refs#42ChrisW09 wants to merge 1 commit into
ChrisW09 wants to merge 1 commit into
Conversation
Three gaps guaranteed a study whose in-process run task died stayed 'running' in the DB forever (POST /run -> 409 'already running'; the only escape was archive + permanent delete): - no startup reconciliation: PROGRESS is in-memory, so any DB-'running' study at boot is orphaned by definition, but lifespan never repaired it. Startup now marks those rows failed (re-runnable). - asyncio.CancelledError is a BaseException, so the 'except Exception' failure path never fired on graceful shutdown/reload (docker-compose runs uvicorn --reload, so ANY backend edit cancelled in-flight runs). It is now caught, best-effort marks the study failed, and re-raises. - launch() discarded the Task reference; the event loop holds only weak refs, so a long run could be garbage-collected mid-flight and vanish silently. launch() now keeps a strong reference until done. Verified against a SQLite DB: orphaned 'running' study marked failed at startup (other statuses untouched), task reference held during the run and released after. Fixes #19 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Fixes #19.
A study whose in-process run task died stayed
"running"in the DB forever:POST /runreturned 409 "already running" indefinitely, and the only escape was archive + permanent delete of the (possibly expensive) study. Three gaps closed:PROGRESSis in-memory, so any DB-"running" study at boot is orphaned by definition, butlifespanonly raninit_db()+seed_if_empty(). It now marks orphaned rowsfailed(re-runnable) and logs how many it repaired.CancelledErrorhandling — graceful shutdown/reload cancels the task with aBaseExceptionthat theexcept Exceptionfailure path never saw; docker-compose runsuvicorn --reloadwith the backend mounted, so any file edit cancelled in-flight runs into the stuck state. Now caught: progress marked failed, best-effort DB write (guarded — the startup reconciliation covers the case where it can't land during shutdown), and re-raised.launch()discarded theasyncio.Task; the event loop holds only weak refs, so a long run could be garbage-collected mid-flight ("a task that isn't referenced elsewhere may get garbage collected at any time" — asyncio docs).launch()now keeps the reference in a module-level set until the task completes.Verified against a real SQLite DB: an orphaned "running" study is marked failed at startup while other statuses are untouched;
launch()holds the task reference during the run and releases it after;app.mainimports and builds cleanly.Note: the SSE stream's infinite-idle loop for such studies is #20 (separate fix), and the run-endpoint check-then-act race is #21.
🤖 Generated with Claude Code