Logging hangs thread after detaching a StreamHandler's terminal

  • Thread starter python-list.overbored
  • Start date
P

python-list.overbored

Hi all,

After many hours, I think I've found a bug in the logging module!

If you add a (stdout) StreamHandler to a logger, then detach the
terminal for that stdout, subsequent calls to log() will hang the
calling thread.

To reproduce this, write the following scripts (this was a small test
case I came up with - maybe there's something simpler):

$ cat tryhup.bash
#!/usr/bin/env bash
scp hup.* localhost:
ssh localhost './hup.bash ; while true ; do sleep 1 ; done'

$ cat hup.bash
#!/usr/bin/env bash
../hup.py &

$ cat hup.py
#!/usr/bin/env python
import time
import logging
f = file( '/tmp/hup.out', 'w' )
try:
logging.basicConfig( filename = '/tmp/lup.out', filemode = 'w' )
# XXX PROBLEM LINE BELOW
logging.getLogger('').addHandler( logging.StreamHandler() )
while True:
f.write( '-------\n' )
f.flush()
logging.critical( '======' )
time.sleep(1)
finally:
f.close()

Run ./tryhup.bash. It will sit in an idle spin. Monitor the files
/tmp/hup.out and /tmp/lup.out. Hit Ctrl-C on tryhup to kill it. The
python process is still running, but is stalled (the .out files are no
longer changing).

If you remove the above labeled line, however, this doesn't happen.

Can anybody please acknowledge this bug? Any temporary workarounds to
this problem? Thanks in advance for looking into this and for hearing
me in!
 
V

Vinay Sajip

Hi all,

After many hours, I think I've found a bug in the logging module!

If you add a (stdout) StreamHandler to a logger, then detach the
terminal for that stdout, subsequent calls to log() will hang the
calling thread.

To reproduce this, write the following scripts (this was a small test
case I came up with - maybe there's something simpler):
[scripts snipped]
Run ./tryhup.bash. It will sit in an idle spin. Monitor the files
/tmp/hup.out and /tmp/lup.out. Hit Ctrl-C on tryhup to kill it. The
python process is still running, but is stalled (the .out files are no
longer changing).

If you remove the above labeled line, however, this doesn't happen.

Can anybody please acknowledge this bug? Any temporary workarounds to
this problem? Thanks in advance for looking into this and for hearing
me in!

I tried these scripts on Ubuntu 5.10 and did not see the problem you
are experiencing. The script continues to run (printing dashes to the
console), and hup.out/lup.out are also updated continuously.

What's your configuration? Also, note that the logging module merely
opens the stream passed to the StreamHander for output, so check if in
your configuration you get a hang just doing a write to sys.stderr.

Best regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top