Skip to content

Fixes #220 sslproto memory issue #222

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

Merged
merged 1 commit into from
Feb 11, 2019

Conversation

fantix
Copy link
Member

@fantix fantix commented Jan 31, 2019

I think this could at least fix the issue that is reproducible with the given script in #220. In the meantime, I'm still looking for more corner cases that may lead to issues.

Tasks below shall be addressed in a new PR:

  • SSLTransport that is never closed by orphaned AppProtocol (shared with asyncio?)
  • uvloop should clear reference to the callback (not just args, new PR) (see Fix ref cycles in callback handles #225)
  • Review all corner cases and write more tests

@fantix fantix force-pushed the t220-ssl-memory-leak branch from 9d93438 to 57c62af Compare February 1, 2019 03:36
@fantix fantix changed the title [WIP] Fixes #220 memory issue Fixes #220 memory issue Feb 1, 2019
@fantix fantix changed the title Fixes #220 memory issue Fixes #220 sslproto memory issue Feb 1, 2019
@fantix fantix force-pushed the t220-ssl-memory-leak branch from 57c62af to e6406b4 Compare February 1, 2019 03:54
@fantix
Copy link
Member Author

fantix commented Feb 1, 2019

OK if tests pass I think this PR is good enough now, please kindly review.

@1st1
Copy link
Member

1st1 commented Feb 1, 2019

OK if tests pass I think this PR is good enough now, please kindly review.

Looks like it's red :(

@fantix fantix force-pushed the t220-ssl-memory-leak branch from e6406b4 to e2bba47 Compare February 1, 2019 23:23
@fantix
Copy link
Member Author

fantix commented Feb 2, 2019

Oh I'm stupid, has been investigating why Test_UV_TCPSSL.test_handshake_timeout_handler_leak passes locally while Travis is red ... it is actually failing for Test_AIO_TCPSSL!! Let me see


Okay found the same (part of) bug in asyncio, skipping the test for now, I'll create another PR to fix in asyncio.

(So the bug is, the handshake timeout handle is never cancelled if the handshake was terminated early - an additional timeout for example. In this case, all objects in the reference chain including SSLProtocol, transport, SSLContext will remain in memory until the handle actually times out, thus it is some kind of a memory leak.)

@fantix fantix force-pushed the t220-ssl-memory-leak branch 4 times, most recently from 4197eb4 to 2f9ea1a Compare February 2, 2019 02:05
@fantix
Copy link
Member Author

fantix commented Feb 2, 2019

Yay! Green now. Also added a second test.

Using Amir's script, it's like this before this fix:

220-before

And now after this fix:

220-after

There were two issues: 1) at `connection_lost()` the timeout handle
was never cancelled because getting a cdef property on a cdef class
with `getattr` always return `None`, and 2) for now cancelling a
uvloop timeout handle only clears references to call arguments but
not the callback object itself, therefore the closure is now a part
of the arguments to avoid SSLProtocol object stays in memory until
the cancelled timeout handle actually get deallocated.
@fantix fantix force-pushed the t220-ssl-memory-leak branch from 2f9ea1a to c3e8ce6 Compare February 11, 2019 19:07
@1st1 1st1 merged commit 386fbf9 into MagicStack:master Feb 11, 2019
@fantix fantix deleted the t220-ssl-memory-leak branch February 11, 2019 20:41
fantix added a commit to fantix/cpython that referenced this pull request Mar 17, 2019
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 17, 2019
* Fix handshake timeout leak in asyncio/sslproto

Refs MagicStack/uvloopGH-222

* Break circular ref _SSLPipe <-> SSLProtocol

* bpo-34745: Fix asyncio ssl memory leak

* Break circular ref SSLProtocol <-> UserProtocol

* Add NEWS entry
(cherry picked from commit f683f46)

Co-authored-by: Fantix King <fantix.king@gmail.com>
1st1 pushed a commit to python/cpython that referenced this pull request Mar 17, 2019
* Fix handshake timeout leak in asyncio/sslproto

Refs MagicStack/uvloop#222

* Break circular ref _SSLPipe <-> SSLProtocol

* bpo-34745: Fix asyncio ssl memory leak

* Break circular ref SSLProtocol <-> UserProtocol

* Add NEWS entry
miss-islington added a commit to python/cpython that referenced this pull request Mar 17, 2019
* Fix handshake timeout leak in asyncio/sslproto

Refs MagicStack/uvloopGH-222

* Break circular ref _SSLPipe <-> SSLProtocol

* bpo-34745: Fix asyncio ssl memory leak

* Break circular ref SSLProtocol <-> UserProtocol

* Add NEWS entry
(cherry picked from commit f683f46)

Co-authored-by: Fantix King <fantix.king@gmail.com>
fantix added a commit to fantix/cpython that referenced this pull request Mar 18, 2019
* Fix handshake timeout leak in asyncio/sslproto, refs MagicStack/uvloop#222

* Break circular ref _SSLPipe <-> SSLProtocol

* bpo-34745: Fix asyncio ssl memory leak

* Break circular ref SSLProtocol <-> UserProtocol

* Add NEWS entry
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.

2 participants