Skip to content

Commit

Permalink
[#1239] Trap SIGINT _before_ starting the main Java process
Browse files Browse the repository at this point in the history
* Only pay for message construction on 'real' errors
* Wait for process termination before leaving Play process
  • Loading branch information
LouisJackman authored and Alexandre Chatiron committed Aug 23, 2018
1 parent d80e51b commit 62d90b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion framework/pym/play/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def run(app, args):
try:
process = subprocess.Popen (java_cmd, env=os.environ)
signal.signal(signal.SIGTERM, handle_sigterm)
signal.signal(signal.SIGINT, handle_sigint)
return_code = process.wait()
signal.signal(signal.SIGINT, handle_sigint)
if 0 != return_code:
sys.exit(return_code)
except OSError:
Expand Down
22 changes: 21 additions & 1 deletion framework/pym/play/commands/daemon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import errno
import os
import os.path
import signal
import subprocess
import time

Expand Down Expand Up @@ -160,13 +162,31 @@ def kill(pid):
print "~ Process with PID %s terminated" % pid
else:
try:
os.kill(int(pid), 15)
_terminate_unix_process_if_exists(int(pid))
except OSError:
print "~ Play was not running (Process id %s not found)" % pid
print "~"
sys.exit(-1)


def _terminate_unix_process_if_exists(pid_to_terminate):
os.kill(pid_to_terminate, signal.SIGTERM)

try:
os.waitpid(pid_to_terminate, 0)
except OSError as error:

# If the child process managed to terminate itself before Play started
# waiting for it, that's OK.
if error.errno != errno.ECHILD:
message_format = (
"~ Failed to wait for process {} to finish after requesting "
"termination\n~")
message = message_format.format(pid_to_terminate)
print(message)



def process_running(pid):
if os.name == 'nt':
return process_running_nt(pid)
Expand Down

0 comments on commit 62d90b1

Please sign in to comment.