Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue15578] Crash when modifying sys.modules during import

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Aug 7, 2012, 1:50 PM

Post #1 of 6 (115 views)
Permalink
[issue15578] Crash when modifying sys.modules during import

New submission from Thomas Wouters:

In a submodule of a package, replacing the parent package in sys.modules during import of the package (meaning the only reference to it is in sys.modules) and then importing itself (or something from itself) crashes the interpreter:

centurion:~/python/crasher > cat sa/__init__.py
import v1
centurion:~/python/crasher > cat sa/v1/__init__.py
import sys
sys.modules['sa'] = sys.modules[__name__]
import sa
centurion:~/python/crasher > python -c 'import sa'
Segmentation fault (core dumped)

It seems the crash is not entirely deterministic, as we've had the original code this was reduced from run in Python 2.4 and 2.6 for years, and only discovered the crash when switching to 2.7. However, running the reduced testcase crashes for me in all of Python 2.7, 2.6, 2.4 and 2.2, in debug builds and opt builds. Given the nature of the bug I expect debug builds to crash reliably.

I haven't found the actual source of the problem, but what seems to happen is that the import mechanism has a borrowed reference to the 'sa' module around, assuming it'll stay alive while the submodules are imported because it's also stored in sys.modules. This assumption is incorrect. However, changing the import in sa/__init__.py into an absolute or explicit relative import seems to fix the problem, which means this probably won't affect Python 3.

----------
components: Interpreter Core
messages: 167640
nosy: twouters
priority: normal
severity: normal
status: open
title: Crash when modifying sys.modules during import
type: crash
versions: Python 2.6, Python 2.7

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 7, 2012, 2:21 PM

Post #2 of 6 (111 views)
Permalink
[issue15578] Crash when modifying sys.modules during import [In reply to]

Changes by Gregory P. Smith <greg [at] krypto>:


----------
nosy: +gregory.p.smith

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 7, 2012, 2:24 PM

Post #3 of 6 (110 views)
Permalink
[issue15578] Crash when modifying sys.modules during import [In reply to]

R. David Murray added the comment:

If I remember correctly there were non-trivial improvements made to the way modules are finalized in 2.7 compared to earlier versions. That could be what exposed the bug. The pre-2.7 code might have been leaving another reference alive because of your more complex real world example, whereas the simplified example doesn't.

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 8, 2012, 1:13 AM

Post #4 of 6 (107 views)
Permalink
[issue15578] Crash when modifying sys.modules during import [In reply to]

Changes by nordaux <nordaux [at] gmail>:


----------
nosy: +nordaux

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 9, 2012, 10:42 AM

Post #5 of 6 (106 views)
Permalink
[issue15578] Crash when modifying sys.modules during import [In reply to]

Changes by Martin v. Löwis <martin [at] v>:


----------
versions: -Python 2.6

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 17, 2012, 10:59 PM

Post #6 of 6 (100 views)
Permalink
[issue15578] Crash when modifying sys.modules during import [In reply to]

Eric Snow added the comment:

Here's the deal. import_module_level() gets called for v1 from sa (where "globals" comes from). In that function it first calls get_parent(), which returns a borrowed reference to the sa module object. Then that parent object is passed to load_next() where the actual load of v1 will take place (and the segfault happens).

The problem is that get_parent() returns a borrowed reference. When the sa module is replaced in sys.modules, the old sa module is decref'ed. That's fine except load_next is using that same module as the parent. Enter segfault, stage left.

Here's a quick patch that fixes the failure (along with a test).

----------
keywords: +patch
nosy: +brett.cannon, eric.snow
stage: -> patch review
Added file: http://bugs.python.org/file26883/eric.snow_issue15578.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue15578>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.