[Python-Dev] What is the precise problem? [was: Reference cycles in Exception.__traceback__]

Antoine Pitrou solipsis at pitrou.net
Sat Mar 8 18:24:21 CET 2014


On Sat, 8 Mar 2014 16:14:23 +0100
Victor Stinner <victor.stinner at gmail.com> wrote:
> 2014-03-08 14:33 GMT+01:00 Antoine Pitrou <solipsis at pitrou.net>:
> > Ok, it's actually quite trivial. The whole chain is kept alive by the
> > "fut" global variable. If you arrange for it to be disposed of:
> >
> >   fut = asyncio.Future()
> >   asyncio.Task(func(fut))
> >   del fut
> >   [etc.]
> >
> > then the problem disappears: as soon as gc.collect() happens, the
> > MyObject instance is destroyed, the future is collected, and the
> > future's traceback is printed out.
> 
> Well, the problem is more general than this specific example. I would
> like to implement a general solution which would not hold references
> to local variables, to destroy objects when Python exits the except
> block.

How about the following patch:

diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -315,6 +315,7 @@ class Future:
         self._schedule_callbacks()
         if _PY34:
             self._log_traceback = True
+            self._loop.call_soon(traceback.clear_frames, exception.__traceback__)
         else:
             self._tb_logger = _TracebackLogger(exception, self._loop)
             # Arrange for the logger to be activated after all callbacks


That said, I agree an automated mechanism would be useful. It is
overwhelmingly rare to want to analyze local variables in a traceback,
yet Python always keeps a reference to those. Perhaps tracebacks could
have a __debug__ attribute which, when sent to False, would prevent the
locals from being kept alive (but how?).

Regards

Antoine.


More information about the Python-Dev mailing list