Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upfix a memory leak on exception (caused by the stored traceback) #11572
Conversation
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 22, 2019
|
I see that it affected a few tests, which fail since the traceback printout got affected. I trust you will know whether the tests need to be adjusted or perhaps you have a different solution to avoid this very problematic leak. Thanks. |
rgbkrk
approved these changes
Jan 22, 2019
|
Thank you so much for investigating and fixing this one! Are you able to update the tests too, to make them less strict about the output? |
rgbkrk
added this to the 7.3 milestone
Jan 22, 2019
This comment has been minimized.
This comment has been minimized.
spott
commented
Jan 22, 2019
|
Is there a test to check if the Since the workaround is to raise another exception, it might make sense to just note that in the text for the OutOfMemory exception, or to only clear the frames when an oom exception is raised. |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 22, 2019
•
|
Yes, someone else has just mentioned to me that %debug is affected, since it relies on the locals() to be set. So more work needs to be done. I don't know anything about ipython's internals, but for its devs it should be an easy task to figure out. Note that my patch was applied to a helper function It's possible that %debug magic needs to activate a flag that doesn't call
And while in the user code we can make a special case for OOM, it is definitely not an option on ipython level, since the leakage affects all exceptions! For example, jupyter notebook via ipython leaks memory on keyboard interrupt. |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 22, 2019
:) First, help is needed to figure out how ipython can avoid this leak w/o affecting %debug magic |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 23, 2019
|
Found a solution for making
the problem with Are there any other cases this change needs to be concerned about, other than |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 23, 2019
@rgbkrk, once I was able to figure out how to run the tests, (which is not documented #11573), the failures are the same in all tests, because the tb no longer contains the
So I'm not sure it's just about fixing the tests to be more forgiving. It could be a real issue that changing tests would then mask it. It's the same issue as with Though in the particular case of the backtrace in the failing tests - it can be made into a string while |
This comment has been minimized.
This comment has been minimized.
|
I wanted to stop by and first, thank you for this! It sounds like getting to the bottom of it was quite the nightmare... I unfortunately don't have time to look for a cleaner implementation, but perhaps the following on the intent behind At some point we realized that, if we saved enough tb state, we could provide a better UX with a new magic that could be activated at any time, without any work in advance, to dig back into a previous exception. Thus was This explains why it's so aggressive at storing exception state. Now, don't know for sure if the problem you've encountered can be mitigated in a way that saves less state and still allows The question that remains is then what to do by default; we have two options: a) Preserve current default behavior so b) Toggle it off by default (basically consider the current behavior a bad enough bug worth breaking backward expectations of behavior). Users could still configure it to force on if desired, and a naked I'm slightly inclined towards (a) as this, while problematic, still seems to be a slightly specialized bug and changing the behavior may confuse many existing users. But I could see a good argument going the other way too, happy to hear others' thoughts. Many thanks again for the hard work you've put into it, probably one of the craziest bug fixes in terms of time invested vs. final lines of code :) |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 23, 2019
|
Thank you for your follow up, @fperez, and sharing the history of
I personally am fine with (a), because debugging should be as easy as possible and your explanation of why %debug was born supports that. In which case we need an easy way to clear the %tb, which currently can be done with 'assert True' as a workaround, so something like We just need to make it easy for users to find out this information. Perhaps a very compact notice should be printed at the end of the exception if debug is on? To give users some breadcrumbs to follow. So the new workflow will be: Scenario: An exception occurred and the gpu card is unusable. Was
Well, to support Thank you for the kind words, everybody. I knew it was a big problem, but I didn't realize that it was so big. |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 23, 2019
|
BTW, through trial and error I learned that the leak had to do with saving the exception object, but this is how I stumbled upon traceback.clear_frames(tb). I saw it quite a few times in the traceback manpage, but I had no idea what it was meant for. Until I stumbled upon this bug report from 2006 https://bugs.python.org/issue1565525 which took 8 years to resolve! So thanks to Greg Hazel who started that battle and all involved who over the years contributed to having a solution. |
This comment has been minimized.
This comment has been minimized.
Just leaving a note that I'm in favor of (b), especially because the source of kernel death is really hard to track down in these cases. It makes me wonder about all the times I've had to tell a user to "just" restart when it was something they could have diagnosed themselves. |
This comment has been minimized.
This comment has been minimized.
spott
commented
Jan 23, 2019
|
I'm in favor of (a). It would inexplicably break a lot of common behavior if suddenly |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 24, 2019
•
Just to clarify: the problem affects any exception, not just CUDA OOM, i.e. any exception leaks memory (temporarily until the next exception arrives if ever). The only special thing about CUDA OOM exception is that it prevents the user from being able to continue 100% of the cases. But, otherwise, any exception could lead to an almost 100% RAM usage, as it'd leak all locals(), and the OOM is just around the corner. For example, if you hit Interrupt during the training, the memory leaks just the same. And it's very likely that the next training loop will OOM, and it wouldn't have happened w/o the interrupt exception that leaked memory a call earlier. So, no, it'd solve it for many more situations than just CUDA OOM. Nevertheless, I'm with you on (a). |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the bug report and the PR. As you said there may be many places where the As we are still in a minor release cycle I'm in favor of not changing the behavior, and postponing thee change of behavior (if any) to IPython 8. Though I've only scanned though the issue and need to get a better deeper look. I'm in the process of releasing 7.3 soon, which is overdue by ~a month. But I'm hopping to do minor releases monthly when possible anyway. |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 24, 2019
|
From my experimentation it seems to be that a more precise location for it is here: #11572 (comment) as compared to the original PR commit. But as I said earlier someone who knows the insides of ipython will have a much better judgement. Indeed, since this PR will require more work by one of your devs, to integrate the new %debug behavior it makes sense to move it into a PR. So please feel free to do what you think is the best. You no longer need my input as I think the issue is very clear and you have the test case to go with it. Personally, I don't think there is a rush to squeeze the solution into the next minor release, since we have solid workarounds and we will still need those to support users with older ipython versions. As long as there is a champion on your dev team to ensure that this does get resolved. And thank you. |
This comment has been minimized.
This comment has been minimized.
stas00
commented
Jan 24, 2019
•
|
One thing would be useful to rush in in the minor release, and that's an ipython magic to clear %tb, so that we don't have to make a manual assertion to free the memory. If it is sensible, of course. |
This comment has been minimized.
This comment has been minimized.
spott
commented
Jan 24, 2019
That makes sense. Then I would like to propose a (c), which might be a good compromise: (c) If an exception occurs, the traceback is cleared at the beginning of the next cell execution if:
|


stas00 commentedJan 22, 2019
•
edited
Currently ipython stores the traceback on exception. The problem is that it creates a circular reference to locals() in each frame of the traceback. And until next exception arrives gc.collect() will fail to collect. If locals() happened to be huge variables, like 8GB of GPU RAM, the user has no choice but to restart ipython/jupyter to recover.
Solution - cleanse the saved tb from any references to
locals()by running it through:traceback.clear_frames(tb)My fix was made to only one such location in the ipython code and there is a dozen other places where it does that. So please kindly review for other similar situations.
I wrote a test case that demonstrates the problem and putting this fix in that particular location fixed that leak. But I don't know whether they are other situations where a similar fix needs to be applied.
You can see the test case here (run it with Alt-F so it runs all cells ignoring the errors)
https://hub.mybinder.org/user/stas00-fastai-misc-xbrsnjae/notebooks/debug/ipython/locals_leak_on_exc.ipynb
It shows how if there is a local variable that is of 128MB in size, an exception involving that function will leak that much RAM. And it shows how the next exception will release the previously held locals() allowing gc.collect() to recover that leaked memory.
The source of the test case is here:
https://github.com/stas00/fastai-misc/blob/master/debug/ipython/locals_leak_on_exc.ipynb
and here it is in straight ipython's output: