问题
I'm using Node.js v0.10.31 on Windows 8.1 x64. I noticed that for a process (a node.js or python script) that handles the SIGINT
handler, the handler is not called when the signal is sent from another node.js process by process.kill(PID, "SIGINT")
, and thus causing it to terminate. However I indeed verified that the handlers are called if the SIGINT is sent by pressing CTRL-C
in console.
Here's the Node.js script that handles SIGINT
(CoffeeScript):
process.on 'SIGINT', -> console.log "SIGINT handled"
process.stdin.pipe(process.stdout)
console.log "PID: #{process.pid}"
Console Output:
PID: 6916
SIGINT handled (this happens when pressing ctrl-c in console)
SIGINT handled (this happens when pressing ctrl-c in console)
# process terminates when another process calls process.kill(6916, 'SIGINT')
And here's a python script that handles SIGINT
, which is also killed unconditionally by node.js process.kill(PID, 'SIGINT')
:
from signal import signal, SIGINT
import os
import time
def handler(signum, frame):
print "signal handled:", signum,
raise KeyboardInterrupt
signal(SIGINT, handler)
print "PID: ", os.getpid()
while True:
try:
time.sleep(1e6)
except KeyboardInterrupt:
print " KeyboardInterrupt handled"
Console output:
PID: 6440
signal handled:2 KeyboardInterrupt handled (this happens when pressing ctrl-c in console)
signal handled:2 KeyboardInterrupt handled (this happens when pressing ctrl-c in console)
# process terminated by another node.js script's process.kill(6440, 'SIGINT')
Why isn't the handler called?
回答1:
It seems like it's not the problem of Node.js that sends SIGINT
, but rather a Windows platform issue. That's because when I send SIGINT
from a python program, it also unconditionally terminates the process that handles the SIGINT
event:
os.kill(pid, signal.SIGINT)
Luckily Python documents this better:
os.kill(pid, sig)
Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.
Windows: The signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT signals are special signals which can only be sent to console processes which share a common console window, e.g., some subprocesses. Any other value for sig will cause the process to be unconditionally killed by the TerminateProcess API, and the exit code will be set to sig. The Windows version of kill() additionally takes process handles to be killed.
来源:https://stackoverflow.com/questions/26053813/node-js-sigint-sent-from-process-kill-cant-be-handled