Skip to content

fix(server): guarantee AbortMsg delivery on stream cancellation - #222

Open
Artemowka22 wants to merge 1 commit into
FlashML-org:mainfrom
Artemowka22:fix/stream-abort-delivery
Open

fix(server): guarantee AbortMsg delivery on stream cancellation#222
Artemowka22 wants to merge 1 commit into
FlashML-org:mainfrom
Artemowka22:fix/stream-abort-delivery

Conversation

@Artemowka22

Copy link
Copy Markdown

Summary

FrontendManager.stream_with_cancellation reacts to a client disconnect with a bare asyncio.create_task(self.abort_user(uid)) and re-raises. The task has no owner: request teardown can complete before the abort coroutine ever runs, and an exception inside it degrades to a "Task exception was never retrieved" warning. The scheduler then keeps decoding for a client that is gone — under a long max_tokens this wastes the GPU for minutes and pins KV pages.

Fix (ported from a downstream audit — agisota/freetoken-mlx, docs/AUDIT.md, defect 2):

  • await the abort inline and shielded in the cancellation handler, so delivery completes even as cancellation propagates, then re-raise;
  • make abort_user idempotent — exactly one AbortMsg per uid even if the handler runs twice (disconnect + server-side cancel).

Test plan

  • pytest tests/server/test_stream_cancellation.py (new): cancellation delivers exactly one AbortMsg before teardown finishes; double cancellation stays single-shot; normal completion sends no abort
  • full tests/server/ suite passes

stream_with_cancellation reacted to a client disconnect with an unowned
asyncio.create_task(abort_user(uid)): request teardown could outrun delivery and a
failure inside the task degraded to a never-retrieved-exception warning, so the
scheduler kept decoding for a client that was gone. Await the abort inline behind
asyncio.shield (a second cancellation cannot kill the delivery task), and make
abort_user claim the uid first — exactly one AbortMsg even if cancellation runs
twice, and none at all when the stream already finished normally.

Found via the freetoken-mlx downstream audit (docs/AUDIT.md, defect 2).
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.

1 participant