msg335022 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2019-02-07 14:25 |
Try this in a terminal: """ import gc import concurrent.futures executor = concurrent.futures.ThreadPoolExecutor(999) def a(): 1/0 future=executor.submit(a) future.result() # An exception is raised here. That is normal gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() gc.garbage """ You will see (python 3.7) that 23 objects are collected when cleaning the cycle. The problem is the attribute "future._exception". If the exception provided by the "future" is raised somewhere else, we will have reference cycles because we have the same exception/traceback in two different places in the traceback framestack. I commonly do this in my code: """ try: future.result() # This will raise an exception if the future did it except Exception: ... some clean up ... raise # Propagate the "future" exception """ This approach will create reference cycles. They will eventually cleaned up, but I noticed this issue because the cycle clean up phase was touching big objects with many references but unused for a long time, so they were living in the SWAP. The cycle collection was hugely slow because of this and the interpreter is completely stopped until done. Not sure about what to do about this. I am currently doing something like: """ try: future.result() # This will raise an exception if the future did it except Exception: if future.done(): del future._exception raise # Propagate the exception """ I am breaking the cycle manually. I do not use "future.set_exception(None) because side effects like notifying waiters. I think this is a bug to be solved. Not sure how to do it cleanly. What do you think? Ideas?. |
|
|
msg383099 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2020-12-15 21:37 |
The corrected test case in the terminal: (a "del" was missing) """ import gc import concurrent.futures executor = concurrent.futures.ThreadPoolExecutor(999) def a(): 1/0 future=executor.submit(a) future.result() # An exception is raised here. That is normal del future # Variable vanish, but data is still there because the cycle gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() gc.garbage """ |
|
|
msg383112 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2020-12-16 01:21 |
Even more reproductible case, now 100%: """ import gc import concurrent.futures executor = concurrent.futures.ThreadPoolExecutor(999) def a(): 1/0 future=executor.submit(a) future.result() # An exception is raised here. That is normal del future # Variable vanish, but data is still there because the cycle 1/0 # Raises another exception drop references to the future one gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() gc.garbage """ |
|
|
msg389714 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2021-03-29 17:22 |
New changeset 32430aadadf6e012e39167d3c18a24e49fb84874 by Jesús Cea in branch 'master': bpo-35930: Raising an exception raised in a "future" instance will create reference cycles (#24995) https://github.com/python/cpython/commit/32430aadadf6e012e39167d3c18a24e49fb84874 |
|
|
msg389717 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2021-03-29 17:53 |
New changeset dae1963cf38f730291126b7dadfda89ffb21cefd by Miss Islington (bot) in branch '3.8': bpo-35930: Raising an exception raised in a "future" instance will create reference cycles (GH-24995) (#25071) https://github.com/python/cpython/commit/dae1963cf38f730291126b7dadfda89ffb21cefd |
|
|
msg389718 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2021-03-29 17:53 |
New changeset d914813a7a9cee3b42e9c91f91ac491f3bbfe118 by Miss Islington (bot) in branch '3.9': bpo-35930: Raising an exception raised in a "future" instance will create reference cycles (GH-24995) (#25070) https://github.com/python/cpython/commit/d914813a7a9cee3b42e9c91f91ac491f3bbfe118 |
|
|
msg389779 - (view) |
Author: Ned Deily (ned.deily) *  |
Date: 2021-03-30 00:43 |
@jcea, I see you have created backports for 3.7 and 3.6 as well. Those release as in their security-fix-only phase of their life cycles. This doesn't seem like a security issue but am I missing something? |
|
|
msg389780 - (view) |
Author: Ned Deily (ned.deily) *  |
Date: 2021-03-30 00:44 |
"Those releases are in their" |
|
|
msg391907 - (view) |
Author: Jesús Cea Avión (jcea) *  |
Date: 2021-04-26 12:25 |
I retired the backporting request. Thanks, Ned. |
|
|