This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: import statement likely to crash if module launches threads
Type: behavior Stage:
Components: Documentation, Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, georg.brandl, jeffstearns, nnorwitz
Priority: normal Keywords:

Created on 2005-04-02 05:11 by jeffstearns, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug1.py jeffstearns, 2005-04-02 05:11 Program demonstrating bug described here
Messages (4)
msg24854 - (view) Author: Jeff Stearns (jeffstearns) Date: 2005-04-02 05:11
I have a simple module which launches multiple HTTP client threads.

The main thread creates 10 HTTP clients, each of which fetches 
documents from a web server.  The main thread then simply goes to 
sleep while the client threads work. The threads are launched when 
the module is imported.

If I launch the script via
  python bug1.py
it launches and runs OK, and dies cleanly upon ^C.

But if I launch the script via
  python -c 'import bug1'
it hangs at launch, and dumps core upon ^C.

Here's an example:

jps@cure> ./python -c 'import bug1'
Using 10 threads
cccccccccc  <- [program hangs here]
Traceback (most recent call last):
  File "<string>", line 1, in ?
  File "test/bug1.py", line 55, in ?
    run (10)
  File "test/bug1.py", line 50, in run
    time.sleep (30000)
KeyboardInterrupt
Fatal Python error: PyImport_GetModuleDict: no module dictionary!
Aborted (core dumped)

You might argue that it's inappropriate to launch threads from within  
import statement, but I can't find a specific prohibition against it.

In any event, it shouldn't cause the interpreter to crash.

Please note that the crash isn't consistent. Sometimes the import 
statement doesn't lead to a crash.
msg24855 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-10-04 05:42
Logged In: YES 
user_id=33168

Reproduced on Linux.
msg60174 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-19 14:42
Don't spawn new threads at module level. An import must never trigger
the creation of a new thread. It's a design issue on your side.

But you can blame us for not documenting the issues with imports and
threads. ;)
msg62844 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-02-23 23:28
The documentation has now been updated to reflect this.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41792
2008-02-23 23:28:59georg.brandlsetstatus: pending -> closed
nosy: + georg.brandl
resolution: not a bug -> duplicate
messages: + msg62844
2008-01-19 14:42:32christian.heimessetstatus: open -> pending
versions: + Python 2.6, - Python 2.4
nosy: + christian.heimes
messages: + msg60174
components: + Documentation
type: behavior
resolution: not a bug
2005-04-02 05:11:09jeffstearnscreate