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.

Author wizzardx
Recipients wizzardx
Date 2010-09-30.07:01:32
SpamBayes Score 3.8056225e-12
Marked as misclassified No
Message-id <1285830097.12.0.730062200867.issue9994@psf.upfronthosting.co.za>
In-reply-to
Content
Hi, I mentioned this on the mailing list over here:

http://mail.python.org/pipermail/python-list/2010-September/1256407.html

I think it's a Python bug, so reposting it here:

-------------
Hi there, I have a strange situation.

If I do this:

1. Make a script /tmp/test.py on a remote server, with this contents:

#!/usr/bin/python
from subprocess import check_call
check_call(['ping', 'www.google.com'])

2. Call the script like this over SSH:

ssh root at testbox /tmp/test.py

3. Interrupt the script with Ctrl+C.

Then what happens:

The SSH session terminates, as expected.

However:

On the testing box, the Python script is still running, and so is the
ping session.

However, if I make an equivalent shell script, /tmp/test.sh, with this contents:

#!/bin/bash
ping www.google.com

And then run it over ssh like this:

ssh root at testbox /tmp/test.sh

And then hit Ctrl+C, then the shell script and ping are both
interrupted remotely, as expected.

Here is how 'pstree -p' looks for the python script on the test box,
before Ctrl+C:

<init (1) up here>
├─sshd(1158)─┬─sshd(19756)───test.py(19797)───ping(19798)
│            └─sshd(20233)───bash(20269)───pstree(19875)

And after Ctrl+C:

<init (1) up here>
├─sshd(1158)───sshd(20233)───bash(20269)───pstree(20218)
├─test.py(19797)───ping(19798)

Basically, the server-side sshd sub-process has disconnected, but the
Python script (and it's ping subprocess) have become orphaned, and now
belong to the init process.

Note, this only seems to happen if Python is executing a subprocess,
and only while Python is being run through a non-interactive ssh
session.

How can I make Python behave better? I want it to close down itself
and it's subprocess, and not orphan itself when I hit ctrl+C

PS:

The Python version on the testing box: 2.6.4, and the box itself is
running Ubuntu Karmic. Also, it's not just ping, but other utilities,
eg wget.

PPS:

I did also try adding logic to the python script, to keep an eye on
all the ppids (parent, grandparent, etc), and then to interrupt itself
and kill it's subprocess, but that doesn't seem to work: For whatever
reason, Python seems to be unable to kill it's subprocess in this
situation. The Python process closes, and ping becomes a child of
init. But I can then kill ping manually, from a separate ssh session.
-------------

I've seen this in both Python 2.6 and Python 3.1.
History
Date User Action Args
2010-09-30 07:01:37wizzardxsetrecipients: + wizzardx
2010-09-30 07:01:37wizzardxsetmessageid: <1285830097.12.0.730062200867.issue9994@psf.upfronthosting.co.za>
2010-09-30 07:01:34wizzardxlinkissue9994 messages
2010-09-30 07:01:32wizzardxcreate