Skip to content

fix(proxy): cancel periodic TOIN task on shutdown - #2906

Merged
chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
abhinavkr26104:fix/2896-periodic-toin-shutdown
Aug 11, 2026
Merged

chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
abhinavkr26104:fix/2896-periodic-toin-shutdown

Conversation

@abhinavkr26104

Copy link
Copy Markdown
Contributor

Description

Retains the periodic TOIN statistics task on application state and reaps it during proxy lifespan shutdown.

Fixes #2896

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)

Changes Made

  • Store the periodic TOIN task as app.state.periodic_toin_stats_task when enabled.
  • Cancel and await the task with the existing bounded shutdown helper before stopping proxy resources.
  • Clear the application state reference after shutdown.
  • Add regression coverage proving the task is canceled and reaped when the FastAPI lifespan exits.

Testing

  • Unit tests pass (pytest)
  • Linting passes (ruff check .)
  • Type checking passes (mypy headroom)
  • New tests added for new functionality
  • Manual testing performed

Test Output

python -m pytest -q tests/test_proxy_telemetry_env.py
0 items / 1 error
ModuleNotFoundError: No module named 'headroom._core'

Temporary in-process native-core stub + real FastAPI TestClient:
python -m pytest -q tests/test_proxy_telemetry_env.py
8 passed

ruff check .
All checks passed!

ruff format --check .
1382 files already formatted

python -m mypy headroom
Success: no issues found in 515 source files

python -m pytest -q
Collected 8878 items / 174 errors / 18 skipped.
Interrupted during collection because this Windows environment lacks the compiled headroom._core extension.

Real Behavior Proof

  • Environment: Windows, Python 3.12, real FastAPI TestClient lifespan; only the unavailable native headroom._core import was replaced with an in-process test stub.
  • Exact command / steps: Ran the telemetry test module with the temporary core stub. The new test enabled periodic TOIN stats, held the real lifespan open, observed the stored task, exited the TestClient context, and checked that the task was canceled and the state reference cleared.
  • Observed result: 8 telemetry tests passed, including the new shutdown regression test; the periodic task reported canceled after lifespan exit and no task reference remained on application state.
  • Who maintains it: Headroom Labs maintains this active upstream repository and proxy lifecycle.
  • Install surface: No dependencies or install behavior changed. The fix uses existing asyncio and FastAPI lifecycle APIs; no native code or runtime network access is introduced.
  • Not tested: The complete suite and the unmodified proxy test command cannot run in this Windows environment without the compiled headroom._core extension.

Review Readiness

  • I have performed a self-review
  • This PR is ready for human review

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (full suite blocked by missing native extension; stubbed focused tests pass)
  • I did not edit CHANGELOG.md - it is generated by release-please from my Conventional Commit PR title.

Screenshots (if applicable)

Not applicable.

Additional Notes

The shutdown uses the existing three-second _timed() bound and handles the disabled configuration without creating a task.

@github-actions

Copy link
Copy Markdown
Contributor

PR governance

This PR does not yet satisfy the required template fields:

  • Missing required section Description.

Please update the PR body, or move the PR back to draft while it is still in progress.

@github-actions github-actions Bot added the status: needs author action Pull request body or readiness checklist still needs author updates label Aug 10, 2026

@JerrettDavis JerrettDavis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The periodic task is now owned by the application lifespan, initialized safely before startup, cancelled and awaited through the existing bounded shutdown helper, and cleared after cleanup. The TestClient lifecycle regression verifies that the task is live during service operation and cancelled at exit. Broader CI has not run on this fork.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@chopratejas chopratejas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer review complete: the periodic task is owned by application state, cancelled and awaited through bounded shutdown, then cleared. The lifespan regression verifies the real startup and shutdown contract. Merge after full CI.

@chopratejas
chopratejas merged commit 739fdef into headroomlabs-ai:main Aug 11, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs author action Pull request body or readiness checklist still needs author updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Periodic TOIN stats task is never cancelled during proxy shutdown

4 participants