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: python hangs if import statement launches threads
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: jeffstearns, loewis
Priority: normal Keywords:

Created on 2005-04-02 05:24 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:24 Script demonstrating bug described here
Messages (2)
msg24856 - (view) Author: Jeff Stearns (jeffstearns) Date: 2005-04-02 05:24
(This bug looks similar to bug 1175194, but reports a different 
problem.)

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.

But if I launch the script via
  python -c 'import bug1'
it runs a bit, then hangs.

Here's an example:

jps@cure> ./python -c 'import bug1'
Using 10 threads
cccccccccc  <- [program hangs here]

Each thread prints a character every time that it does something 
interesting.  The 'c' characters indicate that a connect syscall was 
made.  These should be followed by 'C', indicating the the connect 
returned.  That never happens.

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

Here's a trace of the last few syscalls before it hangs (pids are 
actually thread ids):
[pid 15481] futex(0x820cc48, FUTEX_WAKE, 1) = 0
[pid 15481] futex(0x8148698, FUTEX_WAIT, 0, NULL <unfinished ...>
[pid 15482] futex(0x81a9a80, FUTEX_WAKE, 1) = 0
[pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0
[pid 15482] write(2, "c", 1c)            = 1
[pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0
[pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0
[pid 15482] futex(0x8148698, FUTEX_WAIT, 0, NULL  <- hangs here

Please note that this bug is similar to bug 1175194, but reports a 
different problem.
msg24857 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-04-03 16:24
Logged In: YES 
user_id=21627

This is not a bug. The import statement returns when the
code of the module completes. In this example, the import
statement returns when the call

run(10)

completes. As the run method has a very long sleep, the
import won't return until that sleep is over. So the problem
is not that 10 threads are started, but that the module then
blocks in its module execution.

Furthermore, Python serializes all import statements, with a
single import lock. So while one thread performs an import,
other threads block when trying to import. So if the threads
just started try to import something, they wait until the
"main" import completes. If the main imports wait for the
threads to complete (i.e. the commented-out thread.join),
you get a dead-lock.

Closing as "won't fix".
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41793
2005-04-02 05:24:51jeffstearnscreate