Repository navigation
fix: await transport closure in AsyncOpenAI close method (#3541) - #3620
SparshGarg999 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7c08ae6668
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| self._users = users | ||
|
|
||
| self.retrieve = _legacy_response.async_to_raw_response_wrapper( | ||
| self.retrieve = async_to_streamed_response_wrapper( |
There was a problem hiding this comment.
Restore raw wrappers for async raw responses
For async with_raw_response calls such as await async_client.admin.organization.users.with_raw_response.retrieve(...), this now installs the streaming wrapper, which returns an AsyncResponseContextManager and sets the raw-response header to stream; that object is meant for async with and is not awaitable, so the documented raw-response path raises before returning a closed raw response. This affects every async method assigned in this block; use _legacy_response.async_to_raw_response_wrapper here as in the neighboring resources.
Useful? React with 👍 / 👎.
| extra_body: Body | None = None, | ||
| timeout: float | httpx.Timeout | None | NotGiven = not_given, | ||
| ) -> AsyncPaginator[OrganizationUser, AsyncConversationCursorPage[OrganizationUser]]: | ||
| ) -> AsyncConversationCursorPage[OrganizationUser]: |
There was a problem hiding this comment.
Keep async list typed as a paginator
The async client's get_api_list still returns an AsyncPaginator, and callers materialize the first page by awaiting it or iterate it asynchronously. Annotating this method as an already-materialized AsyncConversationCursorPage makes exported types allow async_client.admin.organization.users.list() to be used without await, which will fail at runtime because the returned object is the paginator; the surrounding async list methods keep the AsyncPaginator[...] return type.
Useful? React with 👍 / 👎.
|
Source-scope check (AI-assisted): at head There is also a separate source-level API contract concern in the visible diff: all four This is a pinned source/AST comparison, not a runtime transport-leak or network reproduction. I initially attempted a runtime return-contract check, but this isolated dependency environment lacks legacy A small read-only comparison, given two extracted import ast,hashlib,json,pathlib,sys
base,head=map(pathlib.Path,sys.argv[1:3]);files=['openai/_base_client.py','openai/_client.py'];rows=[]
for file in files:
a=(base/file).read_bytes();b=(head/file).read_bytes();rows.append({'file':file,'identical':a==b,'base_sha256':hashlib.sha256(a).hexdigest(),'head_sha256':hashlib.sha256(b).hexdigest()})
def wrappers(root):
p=root/'openai/resources/admin/organization/users/users.py';tree=ast.parse(p.read_text());cls=next(n for n in tree.body if isinstance(n,ast.ClassDef) and n.name=='AsyncUsersWithRawResponse');init=next(n for n in cls.body if isinstance(n,ast.FunctionDef) and n.name=='__init__');return {ast.unparse(n.targets[0]):ast.unparse(n.value.func) for n in init.body if isinstance(n,ast.Assign) and isinstance(n.value,ast.Call)}
print(json.dumps({'client_sources':rows,'raw_wrappers_base':wrappers(base),'raw_wrappers_head':wrappers(head),'scope':'Read-only source/AST comparison at pinned PR base and head; not runtime teardown or API proof.'})) |
Fixes #3541
Context & Problem
When managing
AsyncOpenAIclient instances in long-running services, invokingawait client.close()closed high-level HTTP client handles but failed to await internal connection pool cleanup tasks on underlying transport adapters. Under high-concurrency request workloads, this resulted in orphaned sockets, connection leak warnings, and unclosed resource warnings in asyncio event loops.Solution & Changes
close()insrc/openai/_base_client.pyto ensure all underlying transport connection pools and HTTPX client session teardowns are explicitly awaited during async client shutdown.Testing & Verification