-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
bpo-34638: Store a weak reference to stream reader to break strong references loop #9201
Conversation
It breaks the strong reference loop between reader and protocol allowing to detect and close the socket if the stream is deleted (garbage collected)
Lib/asyncio/streams.py
Outdated
context = { | ||
'message': ("Close transport. " | ||
"A stream was destroyed without " | ||
"stream.close() call") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rephrase as:
An open stream is being garbage collected;
call "stream.close()" explicitly.
Lib/asyncio/streams.py
Outdated
context = { | ||
'message': ("Close transport. " | ||
"a stream was destroyed " | ||
"before connection establishment") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An open stream was garbage collected prior to
establishing network connection; call "stream.close()" explicitly.
Lib/asyncio/streams.py
Outdated
@@ -282,6 +349,9 @@ def can_write_eof(self): | |||
return self._transport.can_write_eof() | |||
|
|||
def close(self): | |||
# a reader could be garbage collected / destroyed | |||
# after connection closing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# The reader can be safely garbage collected after
# closing the connection.
Lib/asyncio/streams.py
Outdated
# server socket | ||
# we need to keep a strong reference to the reader | ||
# until connection is made | ||
self._strong_reader = stream_reader |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# This is a stream created by the `create_server()` function.
# Keep a strong reference to the reader until a connection
# is established.
Notes are fixed |
Lib/asyncio/subprocess.py
Outdated
def _untrack_reader(self): | ||
# a dummy placeholder subprocess protocol doesn't need it but | ||
# the method is required for regular stream implementation. | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`StreamWriter.close()` expects the protocol to have this method defined.
When run with Python 3.7 asyncio.subprocess.create_subprocess_exec seems to be affected by an issue that prevents correct cleanup. Tests using pytest-trio will report that signal handling is already performed by another library and fail. [1] This is possibly a bug in CPython 3.7, so we ignore this test for that Python version. CPython 3.7 uses asyncio.streams.StreamReader and asyncio.streams.StreamWriter to implement asyncio.streams.StreamReaderProtocol and asyncio.subprocess.SubprocessStreamProtocol. StreamReaderProtocol contained cyclic references between the reader and the protocol, which prevented garbage collection. While StreamReaderProtocol received a patch [2], SubprocessStreamProtocol, which is used by create_subprocess_exec, possibly has the same problem, but was not patched as part of CPython 3.7. That's why we ignore this test for CPython 3.7. [1] python-trio/pytest-trio#126 [2] python/cpython#9201 Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
… in strict mode (#307) * test: Package-scoped event_loop fixture no longer leaks into other tests. The expected behaviour is that the `event_loop` fixture defined in `tests/sessionloop/conftest.py` is torn down when all tests in `tests/sessionloop` are complete. Running the tests with the pytest option --setup-show pointed out that the fixture is torn down at the end of the test session, instead. This is an unintended side effect of the sessionloop test which may affect other tests in the test suite. Reducing the fixture scope from "package" to "module" results in the expected behaviour. The module was renamed to reflect the fact that the tests do not use a session scope. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * test: Removed test with obsolete "forbid_global_loop". forbid_global_loop was an option to pytest.mark.asyncio which was removed in v0.6.0. The two subprocess tests are otherwise identical. Therefore, one of the tests was removed along with the obsolete option. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * test: Ignore subprocess tests when running on CPython 3.7. When run with Python 3.7 asyncio.subprocess.create_subprocess_exec seems to be affected by an issue that prevents correct cleanup. Tests using pytest-trio will report that signal handling is already performed by another library and fail. [1] This is possibly a bug in CPython 3.7, so we ignore this test for that Python version. CPython 3.7 uses asyncio.streams.StreamReader and asyncio.streams.StreamWriter to implement asyncio.streams.StreamReaderProtocol and asyncio.subprocess.SubprocessStreamProtocol. StreamReaderProtocol contained cyclic references between the reader and the protocol, which prevented garbage collection. While StreamReaderProtocol received a patch [2], SubprocessStreamProtocol, which is used by create_subprocess_exec, possibly has the same problem, but was not patched as part of CPython 3.7. That's why we ignore this test for CPython 3.7. [1] python-trio/pytest-trio#126 [2] python/cpython#9201 Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * build: Added pytest-trio to the test dependencies. This allows testing compatibility between pytest-trio and pytest-asyncio. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * fix: Do not try to initialize async fixtures without explicit asyncio mark in strict mode. This fixes a bug that breaks compatibility with pytest_trio. Closes #298 Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
Breaking the strong reference loop between reader and protocol
allows to detect and close the socket if the stream is deleted (garbage collected) without
close()
call.https://bugs.python.org/issue34638