Skip to content

fix(hf): stop generation workers on stream timeout - #1469

Merged
ajbozarth merged 1 commit into
generative-computing:mainfrom
dk3yyyy:fix/1242-hf-timeout-cancellation
Jul 31, 2026
Merged

ajbozarth merged 1 commit into
generative-computing:mainfrom
dk3yyyy:fix/1242-hf-timeout-cancellation

Conversation

@dk3yyyy

@dk3yyyy dk3yyyy commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1242

Description

Hugging Face generation runs in a worker thread with a cooperative cancellation event. When Mellea's per-chunk stream guard timed out, direct avalue() and astream() calls raised the timeout without signaling that event, so generation continued in the background until natural completion.

This change adds an optional timeout callback to send_to_queue() and connects it to the Hugging Face cancellation hook in both the standard and KV-cache streaming paths. The callback runs only when Mellea's own stream guard expires. Backend-raised TimeoutError instances remain unchanged, and callback failures cannot replace the original stream timeout.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Local verification:

  • uv run pytest --no-cov -q test/helpers/test_async_helpers.py test/backends/test_huggingface_unit.py (74 passed)
  • uv run pre-commit run --all-files (all hooks passed, including Ruff, MyPy, uv-lock, codespell, license headers, markdownlint, and generated MDX validation)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

@dk3yyyy
dk3yyyy requested a review from a team as a code owner July 29, 2026 21:03
@github-actions github-actions Bot added the bug Something isn't working label Jul 29, 2026

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean fix. The on_timeout wiring correctly closes the leak described in the deleted TODOs — direct avalue()/astream() calls now signal the worker thread on stream timeout. The callback isolation (errors logged and suppressed, not propagated) is the right contract. Test coverage is solid: both generation paths have end-to-end timeout tests, and the four send_to_queue cases cover the key behavioural boundaries.

Signal the Hugging Face cancellation event when the per-chunk stream guard expires so direct avalue and astream calls do not leave generation running in a worker thread.

Cover standard and KV-cache streaming paths, callback failures, normal completion, and backend-originated timeouts.

Assisted-by: Hermes Agent

Assisted-by: Claude
Signed-off-by: Joshua Nwachinemere <[email protected]>
@dk3yyyy
dk3yyyy force-pushed the fix/1242-hf-timeout-cancellation branch from bcb6fa7 to d0630ec Compare July 30, 2026 20:47
@planetf1
planetf1 requested a review from ajbozarth July 31, 2026 07:29

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also LGTM, thank you for the contribution

@ajbozarth
ajbozarth added this pull request to the merge queue Jul 31, 2026
Merged via the queue into generative-computing:main with commit 5643e21 Jul 31, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(hf): fire _cancel_event on STREAM_TIMEOUT for direct avalue/astream paths

3 participants