Skip to content

Abort during streaming throws “ReadableStream is locked” in StreamedRunResult #485

@madavic

Description

@madavic

Describe the bug

When running the JS Agents SDK in streaming mode with an AbortController, aborting while the consumer is iterating the stream throws a runtime error: “TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is locked.” This happens because the SDK calls ReadableStream.cancel() while the stream is locked by an active async iterator.

Debug information

  • Agents SDK version: v0.1.3
  • Runtime environment: Node.js 22.10.0

Repro steps

Run the following script:

import { Agent, OpenAIProvider, Runner, setDefaultModelProvider, setTracingDisabled } from '@openai/agents'

setTracingDisabled(true)
setDefaultModelProvider(new OpenAIProvider())

function buildSimpleAgent() {
  return new Agent({
    name: 'Simple Chat Agent',
    instructions: 'You are a helpful assistant. Respond with enough text to stream.',
    model: 'openai/gpt-4o-mini',
    tools: [],
  })
}

async function main() {
  const controller = new AbortController()
  const agent = buildSimpleAgent()
  const runner = new Runner()
  const result = await runner.run(agent, 'Say something long to stream.', { stream: true, signal: controller.signal, maxTurns: 1 })
  ;(async () => { for await (const _ of result) {} })()
  setTimeout(() => { controller.abort() }, 10)
}

main().catch(err => { console.error(err); process.exit(1) })

Observed error:

TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is locked
    at ReadableStream.cancel (node:internal/webstreams/readablestream:322:9)
    at AbortSignal.<anonymous> (.../node_modules/@openai/agents-core/dist/result.mjs:137:44)

Expected behavior

Aborting a streaming run should stop the stream cleanly without throwing, even while it is being iterated. The consumer’s for await loop should end gracefully and no unhandled exception should be raised.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions