Repository navigation
fix(hosted): stop a running campaign before closing SQLite on shutdown (#168) - #170
Open
RupeshMaster wants to merge 1 commit into
Open
RupeshMaster wants to merge 1 commit into
RupeshMaster wants to merge 1 commit into
Conversation
The lifespan closed the SQLite connection while the campaign worker
thread could still be inside a statement on it. Closing a connection
under a running statement crashes the interpreter, so stopping Hosted
during a campaign ended in a segmentation fault, and the test suite
crashed intermittently for the same reason (a test client exiting while
its campaign was still running).
When the process survived, or was killed outright, the campaign row
stayed `running`. Nothing reconciled it, so after a restart every
`POST /api/campaigns/{id}/start` returned `409 campaign_conflict` and
`/api/health` stayed `degraded` until the row was edited by hand.
- On shutdown the supervisor asks the running campaign to stop at its
next event and waits up to 5 seconds. The campaign is marked `failed`
with a `campaign.error` event saying it was interrupted.
- The connection is closed through `Repository.close()`, which takes the
repository lock, so it cannot overlap a query even when a campaign is
stuck inside an adapter step and outlives the wait.
- On startup, rows left `running` are marked `failed` with the same
reason and event. Only the in-process supervisor sets `running`, one
campaign per API process, so such a row has no worker behind it.
- Campaign tasks are now referenced until they finish, and a worker that
ends with an exception is logged.
|
@RupeshMaster is attempting to deploy a commit to the priyanshu's projects Team on Vercel. A member of the Team first needs to authorize it. |
This branch has not been deployed
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.
Summary
Fixes #168. Stopping Hosted while an in-process campaign was running closed the SQLite connection under the campaign worker thread, which can segfault the interpreter (and intermittently crashes the test suite). A campaign left
runningby a stopped or killed process then blocked every later campaign with409 campaign_conflict.CampaignSupervisor.shutdown()sets a stop flag and waits up to 5 seconds for the running campaign. The campaign sees the flag at its next event, ends through the engine's normal error path, and is markedfailedwith acampaign.errorevent ("Hosted stopped while this campaign was running").Repository.close(), which takes the repository lock like every query. If a campaign is stuck inside an adapter step and outlives the wait, the close still cannot overlap one of its statements.Repository.fail_interrupted_campaigns()marks rows leftrunningasfailed(same reason,completed_atset, existing metrics kept, acampaign.errorevent added). Only the in-process supervisor setsrunning, and it runs one campaign per API process, so at startup such a row has no worker behind it. Ingest campaigns are stored ascreatedand are not touched.Behavior to be aware of
failedrather than leftrunning.python -m mutiny_apiand the Dockerfile run it. If you want to support several processes on one SQLite file, the startup step would need an owner marker; say so and I'll add it or drop that part.Type of change
Checklist
Fixes #N) or Discussion when applicableuv run pytest tests/unit -q)Test plan
uv run --locked ruff check→ all checks passeduv run pytest tests/unit tests/integration tests/reliability -q→ 852 passed, six runs in a row. Run locally on Python 3.12 only (macOS,OPENAI_API_KEYunset); 3.11 is left for CI.examples/openai_support_agent,mutiny --help,mutiny init,mutiny runwithMUTINY_SAMPLE_OFFLINE=1and emptyOPENAI_API_KEY/FEATHERLESS_API_KEY→ violation found, regression saved.apps/webis not touched.tests/unit/test_package_release.pypassed as part of the unit run;./scripts/verify_release_artifacts.shnot run.Extra:
main(ee7e898); 30 of 30 exit 0 with this change, each leaving the campaignfailed.python -m mutiny_api, start a campaign, Ctrl-C) with each demo agent step slowed to 20 ms: onmain2 of 8 shutdowns exit with 139 and all 8 leave the campaignrunning; with this change 8 of 8 exit 0 with the campaignfailed.ok,running_campaigns: 0, and the next campaign starts.tests/integration/test_api_hosted_shutdown.py: a campaign interrupted by shutdown isfailedwith onecampaign.errorevent; a row leftrunningis failed at startup and the slot is free;shutdown()gives up after its grace period when a campaign is stuck in an agent step, and the campaign fails once the step returns;Repository.close()waits for the repository lock.Notes