Skip to content

fix(hosted): stop a running campaign before closing SQLite on shutdown (#168) - #170

Open
RupeshMaster wants to merge 1 commit into
CodewithJha:mainfrom
RupeshMaster:fix/hosted-stop-during-campaign
Open

RupeshMaster wants to merge 1 commit into
CodewithJha:mainfrom
RupeshMaster:fix/hosted-stop-during-campaign

Conversation

@RupeshMaster

Copy link
Copy Markdown

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 running by a stopped or killed process then blocked every later campaign with 409 campaign_conflict.

  • Shutdown: 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 marked failed with a campaign.error event ("Hosted stopped while this campaign was running").
  • Close: the lifespan now calls 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.
  • Startup: Repository.fail_interrupted_campaigns() marks rows left running as failed (same reason, completed_at set, existing metrics kept, a campaign.error event added). Only the in-process supervisor sets running, and it runs one campaign per API process, so at startup such a row has no worker behind it. Ingest campaigns are stored as created and are not touched.
  • Campaign tasks are now referenced until they finish (they were created without keeping a reference), and a worker that ends with an exception is logged.

Behavior to be aware of

  • A campaign that is still running when the API stops is now recorded as failed rather than left running.
  • The startup step assumes one API process per database, which is how python -m mutiny_api and 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.
  • A campaign stuck in an adapter step still delays process exit until the step returns or times out, because asyncio waits for its executor threads. That was already the case and is not changed here.

Type of change

Checklist

  • I read CONTRIBUTING.md (where to ask, how reviews work, how to run tests)
  • Linked related issue (Fixes #N) or Discussion when applicable
  • Core stays free of framework SDK imports (adapters own glue)
  • Tests added or updated when behavior changes (uv run pytest tests/unit -q)
  • Docs / README claims match reality (no PyPI / multi-adapter claims unless shipped)
  • One concern per PR when possible
  • Authorized testing only — no open-internet attack-proxy behavior

Test plan

  • Python lint: uv run --locked ruff check → all checks passed
  • Backend tests: uv 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_KEY unset); 3.11 is left for CI.
  • CLI smoke: from a copy of examples/openai_support_agent, mutiny --help, mutiny init, mutiny run with MUTINY_SAMPLE_OFFLINE=1 and empty OPENAI_API_KEY / FEATHERLESS_API_KEY → violation found, regression saved.
  • Web typecheck + build: not run; apps/web is not touched.
  • Package build + isolated artifact gate: tests/unit/test_package_release.py passed as part of the unit run; ./scripts/verify_release_artifacts.sh not run.

Extra:

Notes

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.
@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@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

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.

[bug] Hosted: stopping the API during a campaign can segfault, and a campaign left running blocks all later campaigns

1 participant