classification
Title: 'import readline' hangs when launching with '&' on BSD and OS X
Type: behavior Stage: resolved
Components: Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: ronaldoussoren Nosy List: ned.deily, olivier-mattelaer, ronaldoussoren
Priority: normal Keywords:

Created on 2012-05-23 18:55 by olivier-mattelaer, last changed 2012-11-30 14:24 by ronaldoussoren. This issue is now closed.

Messages (4)
msg161450 - (view) Author: (olivier-mattelaer) Date: 2012-05-23 18:55
Hi Everyone, 

I have found a strange behavior of the import command for the routine readline:

The commands (put in the file test.py) is simply:
import readline
print readline.__doc__

If I run this programs "normally" (i.e. python2.x test.py) everything runs normally:
[tmp]$ python2.7 tmp.py 
Importing this module enables command line editing using GNU readline.

But if I launched it in the following way:
python2.X test.py &
It's stops before finishing the import command (so no output even if I print the output into a file). 
This doesn't raise any error just stop the program.

I tried to add a try...except...finally... but even the finally statement is not executed.

Of course, I realize that readline is not that interesting to load with that options. So I would be more than happy if you could indicate me a way to either detect that the user launch the program with '&' or a way to avoid readline to stop the program in this situations.

Thanks a lot,

Olivier
msg161465 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2012-05-23 21:52
I took a quick look at this. It's not just OS X, the following also fails on FreeBSD 8.2:

$ python2.7 -c 'import readline'
$ 
$ python2.7 -c 'import readline' &
$ 
[1] + Stopped (tty output)    python2.7 -c ?import readline

It seems to be hanging on a read from stdin, as adding a redirect of stdin to /dev/null prevents the hang:

$ python2.7 -c 'import readline' </dev/null &
[1] 36178
$ 
[1]+  Done                    python2.7 -c 'import readline' < /dev/null

But, on Debian and Ubuntu, neither case hangs. Also, on OS X, the same hang behavior is observed when linked with either GNU readline or BSD libedit.  I'm not sure what the significant difference here is: possibly a BSDism vs Linuxism?
msg173501 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2012-10-22 10:46
I've just tested this on OSX 10.8.2 and the problem is not present there (that is, 'python -c "import deadline" &' does not hang)

The problem is present on OSX 10.6.8, and when using ksh(1) you can see why the process is stopped:

[1] + Stopped(SIGTTOU)         python -c 'import readline' &

That signal is generated when the program generates output to a tty and the TOSTOP option of stty is active.  Whether or not that option is active is OS configuration (and can be changed using the stty command).

Reading the option:

import termios
lflags = termios.tcgetattr(0)[3]
print (lflags & termios.TOSTOP) != 0

Oddly enough the option off on both OSX 10.6 and 10.8, which point to a bug in OSX 10.6 (or to a misunderstanding of low-level tty programming details on my part which would not be unlikely)

Anyway, there is an easy workaround: before importing readline add:

import signal
signal.signal(signal.SIGTTOU, signal.SIG_IGN)

This ignores the SIGTTOU option and allows me to import readline on the background.

IMHO This issue can be closed because this behavior is not a Python bug but a platform feature.
msg173541 - (view) Author: (olivier-mattelaer) Date: 2012-10-22 16:42
Thanks a lot Ronald.

Cheers,

Olivier
History
Date User Action Args
2012-11-30 14:24:18ronaldoussorensetstatus: open -> closed
stage: resolved
2012-10-22 16:42:00olivier-mattelaersetstatus: pending -> open

messages: + msg173541
2012-10-22 10:46:32ronaldoussorensetstatus: open -> pending
resolution: not a bug
messages: + msg173501
2012-05-23 21:52:47ned.deilysettype: crash -> behavior
title: 'import readline' fails when launching with '&' -> 'import readline' hangs when launching with '&' on BSD and OS X
components: - macOS

nosy: + ned.deily
versions: + Python 3.2, Python 3.3, - Python 2.6
messages: + msg161465
2012-05-23 18:55:18olivier-mattelaercreate