This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio.BaseSubprocessTransport triggers an unavoidable ResourceWarning if process doesn't start
Type: resource usage Stage:
Components: asyncio Versions: Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, martius, python-dev, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2015-07-31 14:57 by martius, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
transport_close_when_exception_init.patch martius, 2015-07-31 14:57 review
popen_error_close.patch vstinner, 2015-07-31 15:30 review
Messages (6)
msg247746 - (view) Author: Martin Richard (martius) * Date: 2015-07-31 14:57
An exception can be raised in SubprocessTransport.__init__() from SubprocessTransport._start() - for instance because an exception is raised in the preexec_fn callback.

In this case, the calling function never gets a reference to the transport object, and cannot close the transport. Hence, when the object is deleted, an "unclosed transport" ResourceWarning is always triggered.

Here is a test case showing this behavior:
import asyncio

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(asyncio.create_subprocess_exec('/doesntexist'))
except FileNotFoundError:
    pass
finally:
    loop.close()


I propose the attached patch as a solution, which call SubprocessTransport.close() when an exception is raised in SubprocessTransport._start() in the constructor.
msg247748 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-07-31 15:31
popen_error_close.patch: I put more code in the try block to ensure that the transport is close on any kind of error, and I added an unit test.
msg247749 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-07-31 15:51
New changeset f6b8a0c6b8c9 by Victor Stinner in branch '3.4':
Fix ResourceWarning in asyncio.BaseSubprocessTransport
https://hg.python.org/cpython/rev/f6b8a0c6b8c9
msg247750 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-07-31 15:52
I applied your patch + my patch to Python 3.4, 3.5 and 3.6 and asyncio at Github. Thanks for the report and the patch Martin!
msg247775 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-07-31 21:37
New changeset 6703ac68bf49 by Victor Stinner in branch '3.4':
Issue #24763: Fix asyncio test on Windows
https://hg.python.org/cpython/rev/6703ac68bf49
msg248341 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-08-09 22:22
New changeset 2648dddd3248 by Yury Selivanov in branch '3.4':
Issue #24763: Fix asyncio test on Windows (fix reverted change)
https://hg.python.org/cpython/rev/2648dddd3248

New changeset aa527ee0d27f by Yury Selivanov in branch '3.5':
Merge 3.4 (issues #24835, #24763)
https://hg.python.org/cpython/rev/aa527ee0d27f

New changeset f304ba9425a3 by Yury Selivanov in branch 'default':
Merge 3.5 (issues #24835, #24763)
https://hg.python.org/cpython/rev/f304ba9425a3
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68951
2015-08-09 22:22:43python-devsetmessages: + msg248341
2015-07-31 21:37:40python-devsetmessages: + msg247775
2015-07-31 15:52:27vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg247750

versions: + Python 3.6
2015-07-31 15:51:30python-devsetnosy: + python-dev
messages: + msg247749
2015-07-31 15:31:20vstinnersetmessages: + msg247748
2015-07-31 15:30:45vstinnersetfiles: + popen_error_close.patch
2015-07-31 14:57:06martiuscreate