-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG][Python] Asyncio cleanup fails when exceptions are raised #5538
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
I haven't tested it yet, but it looks like #5621 fixed this. |
Confirmed fixed with latest OpenAPI-Generator Docker image
The same command-line for generation as used above, now reports:
And the repro case passes, with one minor change due to API change from this fix: with openapi_client.ApiClient(configuration) as api_client: is now async with openapi_client.ApiClient(configuration) as api_client: I also confirmed this fix in the 4.3.0 release, repeating the reproduction case using Docker image openapitools/openapi-generator-cli:v4.3.0.
|
Bug Report Checklist
Have you validated the input using an OpenAPI validator (example)?[Optional] Bounty to sponsor the fixDescription
When an exception occurs in python
--library asyncio
generated code, it fails to clean up correctly due to use of the__del__
magic method to clean up the aiohttpClientSession
object.This is due to using
RESTClientObject.__del__
to scheduleaiohttp.ClientSession.close
withasyncio.ensure_future
.__del__
doesn't run until the exception is discarded, and in this repro case, that happens outside the asyncio event loop, and hence raises an exception itself.openapi-generator version
4.3.0-SNAPSHOT, pulled as follows:
I don't believe this is a recent regression, the code hasn't been touched since it was introduced in #107 in 2018.
OpenAPI declaration file content or url
https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml
Command line used for generation
Steps to reproduce
After code-gen, create and run the following Python script:
Actual output
Expected output
Related issues/PRs
None I'm aware of.
Suggest a fix
Workaround
A monkey-patch to remove
__del__
and afinally
block to directly callaiohttp.ClientSession.close
within the asyncio context fixes the issue from the user side:My ideal solution would be if
ApiClient
in the asyncio config was an asynchronous context manager, which would mean using it as:Then its
__aexit__
code could call anasync def close()
onRESTClientObject
, which would close itsaiohttp.ClientSession
.However, the tornado and urllib implementations of
RESTClientObject
do not have any particular need to be notified onApiClient.close
, so this would be introducing an asyncio-specific path.The text was updated successfully, but these errors were encountered: