Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/claude_agent_sdk/_internal/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,14 @@ async def close(self) -> None:
with suppress(anyio.get_cancelled_exc_class()):
await self._tg.__aexit__(None, None, None)
await self.transport.close()
# Clean up resources to prevent memory leaks in long-running applications
self.hook_callbacks.clear()
self.pending_control_responses.clear()
self.pending_control_results.clear()
with suppress(Exception):
await self._message_send.aclose()
with suppress(Exception):
await self._message_receive.aclose()

# Make Query an async iterator
def __aiter__(self) -> AsyncIterator[dict[str, Any]]:
Expand Down