Title: Popen.send_signal raises AttributeError if not used with SIGTERM
Type: Severity: normal
Components: Versions: Jython 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: pekka.klarck, zyasoft
Priority: high Keywords:

Created on 2014-10-13.21:46:28 by pekka.klarck, last changed 2015-02-02.16:32:51 by zyasoft.

msg9137 (view) Author: Pekka Klärck (pekka.klarck) Date: 2014-10-13.21:46:28
Jython 2.7b3 (default:e81256215fb0, Aug 4 2014, 02:39:51) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_60
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess, signal
>>> subprocess.Popen('sleep 10', shell=True).send_signal(signal.SIGTERM)
>>> subprocess.Popen('sleep 10', shell=True).send_signal(signal.SIGINT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peke/Prog/jython2.7b3/Lib/", line 1399, in send_signal
    elif sig == signal.CTRL_C_EVENT:
AttributeError: 'module' object has no attribute 'CTRL_C_EVENT'
msg9139 (view) Author: Jim Baker (zyasoft) Date: 2014-10-14.02:01:44
Looks like the send_signal method was just copied from Windows support in CPython. We should presumably use the signal module instead, if available under the security manager, but that's likely if a subprocess can be created.
msg9143 (view) Author: Pekka Klärck (pekka.klarck) Date: 2014-10-14.09:20:22
How could signal module be used here? Doesn't it only contain signal constants, a function to register signal handlers, and some UNIX only functions to set timers and alarms?

It seems to me that really supporting `send_signal` on any platform would require `os.kill` (currently missing) and `` (always None). I know supporting these on JVM is far from trivial, but on the other hand these features would be very useful also independently.

I totally understand if fixing this is impossible in Jython 2.7.0 timeline. In that case it would probably be a good idea to change to code to just this:

    def send_signal(self, sig):
        if sig == signal.SIGTERM:
        raise ValueError("Unsupported signal: {}".format(sig))

It seems that JVM actually sends SIGTERM on POSIX when calling `process.destroy()` (like `self.terminate()` does), and on Windows sending SIGTERM is anyway just an alias for terminating the process also on CPython.
msg9146 (view) Author: Pekka Klärck (pekka.klarck) Date: 2014-10-14.10:48:15
Submitted a highly related issue about Popen.kill not really killing the process outside Windows:

Also submitted a separate issue about being None. This time the issue also contains proto code that seems to work at lest on Windows and Linux:
msg9150 (view) Author: Jim Baker (zyasoft) Date: 2014-10-14.17:53:45
Right besides fixing the attribute errors (which is the  high portion of this bug), we need pid support for created subprocesses. See further discussion on #2220 on how we would use with SIGKILL.

If we have these other dependencies fulfilled, this can readily make it to beta 4.
msg9151 (view) Author: Pekka Klärck (pekka.klarck) Date: 2014-10-14.21:49:44
Yeah, pid and os.kill ought to be enough to get this fully implemented. Could mostly use the default subprocess logic both on POSIX and on Windows after that. The only exception I can think of is that SIGTERM should be mapped to Popen.terminate (and through it to Java's Process.destroy) also on POSIX. That would give us SIGTERM support even if pid wouldn't be available.

I though Jython didn't have os.kill at all, but apparently it has it except on Windows. Is there a reason it's not there on Windows like it is on Python 2.7? Should a separate issue be submitted about it? It ought to be possible to send CTRL_C_EVENT and CTRL_BREAK_EVENT using ctypes. Signals are obviously much more useful on POSIX, and thus I consider Windows support lowest priority here.
msg9447 (view) Author: Pekka Klärck (pekka.klarck) Date: 2015-01-23.13:42:57
Implemented by this pull request:
msg9449 (view) Author: Jim Baker (zyasoft) Date: 2015-01-23.23:33:58
Fixed as of
Date User Action Args
2015-02-02 16:32:51zyasoftsetstatus: pending -> closed
2015-01-23 23:33:58zyasoftsetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg9449
2015-01-23 13:42:57pekka.klarcksetmessages: + msg9447
2014-10-14 21:49:44pekka.klarcksetmessages: + msg9151
2014-10-14 17:53:45zyasoftsetmessages: + msg9150
2014-10-14 10:48:15pekka.klarcksetmessages: + msg9146
2014-10-14 09:20:22pekka.klarcksetmessages: + msg9143
2014-10-14 02:01:45zyasoftsetversions: + Jython 2.7
nosy: + zyasoft
messages: + msg9139
priority: high
assignee: zyasoft
resolution: accepted
2014-10-13 21:46:28pekka.klarckcreate