Skip to content

Streaming runs can't be canceled gracefully #1895

Closed

Description

@habema

Problem

When canceling a streaming run with result.cancel(), it stops immediately and discards the current turn. This means:

  • The incomplete turn isn't saved to the session
  • Usage tracking is cut off mid-turn
  • Can't resume the session cleanly

This is painful if the user interrupts a long task - all progress from current turn is lost, and its usage cant be tracked.

Current behavior

result = Runner.run_streamed(agent, "Task", session=session)
async for event in result.stream_events():
    if timeout:
        result.cancel()  # Stops immediately, turn is discarded

Would be nice to have a way to let the current turn complete before stopping.

Activity

  1. added
    enhancement
    on Oct 15, 2025
  2. idzikovski commented on Jun 15, 2026

    @idzikovski

    I have run into another painful side-effect of not canceling gracefully which is unpredictable/incorrect resource management and release. In my case I pass an httpx client to the sdk to perform the calls to my LLM and I noticed that after some use and cancelled requests, the httpx client exhausts every available connection in the pool leading to every request failing with a PoolTimeout.

    I would propose that after_turn is the default value of result.cancel(). If needed I can try to create a minimal reproduction code of the issue I mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeature:core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions