Skip to content

fix(bedrock): consume orphaned task exception on stream cancellation#2269

Open
AnnasMazhar wants to merge 1 commit intostrands-agents:mainfrom
AnnasMazhar:fix/bedrock-stream-task-leak
Open

fix(bedrock): consume orphaned task exception on stream cancellation#2269
AnnasMazhar wants to merge 1 commit intostrands-agents:mainfrom
AnnasMazhar:fix/bedrock-stream-task-leak

Conversation

@AnnasMazhar
Copy link
Copy Markdown

Motivation

When a consumer cancels, breaks from, or times out on BedrockModel.stream, the internal asyncio.Task wrapping asyncio.to_thread is never awaited. If boto3 eventually raises (e.g., ReadTimeoutError after read_timeout), asyncio emits "Task exception was never retrieved" — polluting application logs across three common usage patterns: asyncio.wait_for, async for ... break, and client disconnects in streaming HTTP handlers.

The happy path already awaits the task. The unhappy path (any BaseException interrupting the yield loop) leaves it orphaned.

Resolves: #2266

Public API Changes

No public API changes. Internal cleanup only — BedrockModel.stream now ensures the background task's exception is consumed on all exit paths via a done-callback.

Checked AnthropicModel.stream and OpenAIModel.stream — neither uses asyncio.create_task or asyncio.to_thread, so this pattern is unique to the Bedrock provider.

When a consumer cancels, breaks from, or times out on BedrockModel.stream,
the internal asyncio.Task wrapping asyncio.to_thread is never awaited. If
boto3 eventually raises, asyncio emits 'Task exception was never retrieved'.

Add a done-callback on the unhappy path that retrieves the exception,
silencing the warning without interrupting the background thread.

Resolves: strands-agents#2266
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] BedrockModel.stream leaks inner task on outer cancellation, causing "Task exception was never retrieved"

1 participant