classification
Title: segmentation fault with del sys.modules['__main__']
Type: crash Stage: resolved
Components: Versions: Python 3.4, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, amaury.forgeotdarc, ezio.melotti, haypo, hynek, jcea, ncoghlan, python-dev, shaitanich
Priority: normal Keywords: patch

Created on 2012-06-04 19:49 by amaury.forgeotdarc, last changed 2012-11-07 08:52 by hynek. This issue is now closed.

Files
File name Uploaded Description Edit
pyrun_simple.patch haypo, 2012-06-05 23:53 review
Messages (9)
msg162284 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-06-04 19:49
This simple script segfaults the interpreter (all versions), at least when run with "./python myscript.py"

myscript.py::
    import sys
    del sys.module['__main__']
msg162286 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-06-04 20:06
Many paths in pythonrun.c contains these lines:
    m = PyImport_AddModule("__main__");
    d = PyModule_GetDict(m);
both return borrowed references, from sys.modules.
in most cases, d is simply passed to PyEval_EvalCode() and not used afterwards, *except* in PyRun_SimpleFileExFlags which calls PyDict_DelItemString(d, "__file__"); this is where the crash occurs.
msg162381 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2012-06-05 23:53
Attached patch changes PyRun_SimpleFileExFlags() to keep a reference to the module.
msg174492 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-11-02 07:19
Fun fact, on 2.7 & 3.2 I get infinite loops @ 100% CPU. 3.3 & default crash.

Unless someone yells, I'll polish this up and commit next week.
msg174745 - (view) Author: Dmytro Korsakov (shaitanich) Date: 2012-11-04 02:59
I was unable to reproduce the case with stock python 2.7 neither on OSX nor on Linux. System python 3.2 seems to be fine too. 
At the same time python 2.7 built with debug enabled gets infinite loop and uses all the cpu.
However, python 3.4 crashes just as described built both with or without debug flag, and suggested patch fixes it.
msg174748 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-11-04 03:32
Same here, I tested the patch on 3.4 (debug and non-debug) and it seems to fix the problem.
msg175049 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-07 08:14
New changeset b256d054f229 by Hynek Schlawack in branch '3.2':
Issue #15001: fix segfault on "del sys.module['__main__']"
http://hg.python.org/cpython/rev/b256d054f229

New changeset 215297665098 by Hynek Schlawack in branch '3.3':
Issue #15001: fix segfault on "del sys.module['__main__']"
http://hg.python.org/cpython/rev/215297665098

New changeset 859ef54bdce2 by Hynek Schlawack in branch 'default':
Issue #15001: fix segfault on "del sys.module['__main__']"
http://hg.python.org/cpython/rev/859ef54bdce2
msg175055 - (view) Author: Roundup Robot (python-dev) Date: 2012-11-07 08:48
New changeset 4ebe1ede981e by Hynek Schlawack in branch '2.7':
Issue #15001: fix segfault on "del sys.modules['__main__']"
http://hg.python.org/cpython/rev/4ebe1ede981e
msg175056 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-11-07 08:52
This should be fixed now, thanks to all who helped!
History
Date User Action Args
2012-11-07 08:52:46hyneksetstatus: open -> closed
resolution: fixed
messages: + msg175056

stage: commit review -> resolved
2012-11-07 08:48:28python-devsetmessages: + msg175055
2012-11-07 08:31:59Arfreversettitle: segmentation fault with del sys.module['__main__'] -> segmentation fault with del sys.modules['__main__']
2012-11-07 08:14:32python-devsetnosy: + python-dev
messages: + msg175049
2012-11-05 12:12:47ncoghlansetnosy: + ncoghlan
2012-11-04 03:32:40ezio.melottisetnosy: + ezio.melotti
messages: + msg174748
2012-11-04 02:59:26shaitanichsetnosy: + shaitanich
messages: + msg174745
2012-11-02 07:19:05hyneksetstage: commit review
messages: + msg174492
versions: + Python 3.4, - Python 3.1
2012-06-06 01:08:10jceasetnosy: + jcea
2012-06-05 23:53:40hayposetfiles: + pyrun_simple.patch
keywords: + patch
messages: + msg162381
2012-06-04 22:55:25hayposetnosy: + haypo
2012-06-04 20:06:59amaury.forgeotdarcsetmessages: + msg162286
2012-06-04 19:59:29hyneksetnosy: + hynek
2012-06-04 19:54:12alexsetnosy: + alex
2012-06-04 19:49:25amaury.forgeotdarccreate