-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Memory leak in gen engine with try/finally block when connections are closed #660
Comments
One possible fix is manually closing generator on connection close, like this:
|
Have you tried this with the current master branch? I recently fixed a memory leak here (in commit bff0740 ). If that's not it I'll take another look. |
Tested with tornado 2.4.1 (so with mentioned commit) |
OK, so there are several things going on here. First, if the connection is closed, the flush callback is never called, so the generator is just left hanging forever waiting for a callback that never comes (you can see that "finally" is never actually printed to the logs). Normally, the generator would be garbage collected, at which point a GeneratorExit exception would be generated, via a magic del method on the generator object itself. However, in this case the generator is part of a reference cycle, and python's garbage collector does not collect cycles if any object in the cycle has a del method (one surprising bit of magic here is that generators only have a del method if they have a try/except/finally block). This is tricky to fix properly because of the way many tornado methods signal failure through on_connection_close instead of to their own callback. I think it's relatively easy to fix in this case (I think it would be OK if flush callbacks were run when the connection gets closed, although it would be a problem to do the same for read callbacks. I think I could also break up the reference cycle, but that's hard to do in general), but I need to think about whether there's a more general solution. |
(Also posted on http://stackoverflow.com/q/14162950/326792)
This code, shows memory leak in tornado's
gen
module, when connections are closed without reading the response:and now, run a simple test client, multiple times
Now, server output shows, incremental memory usage:
If you set
CHUNK_COUNT
to 1, the 10KB of data can be written to OS connection buffer, and 'finished' and 'finally' texts will be printed to console, and because generator is completed, no memory leak occurs.But the strange part is that if your remove the try/finally block, the problem disappears!! (even with
CHUNK_COUNT
set to 100)Is this a bug on CPython or tornado or ...?!
The text was updated successfully, but these errors were encountered: