Skip to content
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

Merged
merged 10 commits into from
Sep 12, 2018

Conversation

asvetlov
Copy link
Contributor

@asvetlov asvetlov commented Sep 11, 2018

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

It breaks the strong reference loop between reader and protocol allowing to detect and close the socket if the stream is deleted (garbage collected)
@asvetlov asvetlov requested a review from gpshead as a code owner September 12, 2018 17:30
@asvetlov asvetlov changed the title bpo-34638: Store a weak reference to stream readerfor breaking strong references bpo-34638: Store a weak reference to stream reader to break strong references loop Sep 12, 2018
context = {
'message': ("Close transport. "
"A stream was destroyed without "
"stream.close() call")
Copy link
Member

@1st1 1st1 Sep 12, 2018

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.

context = {
'message': ("Close transport. "
"a stream was destroyed "
"before connection establishment")
Copy link
Member

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.

@@ -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
Copy link
Member

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.

# server socket
# we need to keep a strong reference to the reader
# until connection is made
self._strong_reader = stream_reader
Copy link
Member

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.

@asvetlov
Copy link
Contributor Author

Notes are fixed

def _untrack_reader(self):
# a dummy placeholder subprocess protocol doesn't need it but
# the method is required for regular stream implementation.
pass
Copy link
Member

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.

@asvetlov asvetlov merged commit a5d1eb8 into python:master Sep 12, 2018
@asvetlov asvetlov deleted the weakref branch September 12, 2018 18:43
seifertm added a commit to seifertm/pytest-asyncio that referenced this pull request Mar 11, 2022
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>
seifertm added a commit to pytest-dev/pytest-asyncio that referenced this pull request Mar 15, 2022
… 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants