classification
Title: Script globals in a GC cycle not finalized when exiting with SystemExit
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, ncoghlan, petr.viktorin, pitrou
Priority: normal Keywords: patch

Created on 2015-07-09 13:31 by petr.viktorin, last changed 2018-07-03 20:18 by pitrou. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7918 merged ZackerySpytz, 2018-06-25 22:59
PR 8069 merged miss-islington, 2018-07-03 19:48
PR 8070 merged miss-islington, 2018-07-03 19:48
Messages (6)
msg246491 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2015-07-09 13:31
When this program is invoked as a script (`python reproducer.py`), the __del__ is never called:

---

class ClassWithDel:
    def __del__(self):
        print('__del__ called')

a = ClassWithDel()
a.link = a

raise SystemExit(0)

---


Raising a different exception, moving the code to a function, importing the module, or invoking with -m (or even -c), causes __del__ to be called normally.
msg246498 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 13:55
It's likely that your global variable gets caught in the traceback attached to the SystemExit, and that either never gets deallocated, or gets deallocated too late.
msg246502 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 14:10
Actually, the problem is in PyRun_SimpleFileExFlags(). The executed module is decref'ed after calling PyErr_Print(), but the latter never returns when the exception is a SystemExit.
msg321001 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-07-03 19:47
New changeset d8cba5d16f1333fd625726fc72e66afbd45b8d00 by Antoine Pitrou (Zackery Spytz) in branch 'master':
bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (GH-7918)
https://github.com/python/cpython/commit/d8cba5d16f1333fd625726fc72e66afbd45b8d00
msg321003 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-07-03 20:13
New changeset 20ae4c60258479a0732d12af292f422679444e2c by Antoine Pitrou (Miss Islington (bot)) in branch '3.7':
bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (GH-7918) (GH-8070)
https://github.com/python/cpython/commit/20ae4c60258479a0732d12af292f422679444e2c
msg321004 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-07-03 20:17
New changeset e1ebf51f76037b7e02711aaeb9bf66c9147f4c74 by Antoine Pitrou (Miss Islington (bot)) in branch '3.6':
bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (GH-7918) (GH-8069)
https://github.com/python/cpython/commit/e1ebf51f76037b7e02711aaeb9bf66c9147f4c74
History
Date User Action Args
2018-07-03 20:18:03pitrousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-07-03 20:17:48pitrousetmessages: + msg321004
2018-07-03 20:13:32pitrousetmessages: + msg321003
2018-07-03 19:48:48miss-islingtonsetpull_requests: + pull_request7680
2018-07-03 19:48:01miss-islingtonsetpull_requests: + pull_request7679
2018-07-03 19:47:31pitrousetmessages: + msg321001
2018-06-25 23:02:52ZackerySpytzsetnosy: + ZackerySpytz

versions: + Python 3.7, Python 3.8, - Python 3.4, Python 3.5
2018-06-25 22:59:09ZackerySpytzsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7524
2015-07-09 14:10:29pitrousetnosy: + ncoghlan
messages: + msg246502
2015-07-09 13:55:52pitrousetnosy: + pitrou
messages: + msg246498
2015-07-09 13:31:32petr.viktorincreate