Bug #13501

Process.kill behaviour for negative pid is not documented and may be wrong

Added by toy (Ivan Kuchin) about 2 years ago. Updated almost 2 years ago.

Target version:
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin14]


Following code will not fail and will print "TERM caught by NN" both for spawned and forked processes:

pid = spawn 'ruby', '-e', <<-'RUBY', :pgroup => true
  trap('TERM'){ print "TERM caught by #{$$}\n" }
  sleep 10
sleep 1
Process.kill('TERM', -pid)

Documentation explains such behaviour for using negative signal (or a string starting with '-'), but not negative pid.

linux, osx and POSIX documentation for kill describes behaviour for negative pid is to send signal to process group.

As Process.kill is using killpg if it is defined, the behaviour may be undefined on systems with killpg present but kill not conforming to POSIX specification.
Also sending negative signal with negative pid will call killpg with negative pid.



Updated by toy (Ivan Kuchin) about 2 years ago

  • ruby -v changed from ruby 2.2.7p470 (2017-03-28 revision 58194) [x86_64-darwin14] to ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin14]

Updated by shevegen (Robert A. Heiler) about 2 years ago

Agreed - the behaviour should also be documented. Not that I think anyone wants to use a
negative PID (how is that even possible) but simply because one could pass -1 or -42 or

Updated by toy (Ivan Kuchin) about 2 years ago

Negative pid will send signal to the process group.
And specifically -1 will send it nearly system wide if the process user has root privileges.

Updated by shyouhei (Shyouhei Urabe) almost 2 years ago

We looked at this issue at yesterday's developer meeting and agreed that the acceptance of negative pid shall be documented.

But killing other process is ultimately dome by the platform. It seems not possible to completely remove platform dependency.

Updated by toy (Ivan Kuchin) almost 2 years ago

What do you think about intercepting negative pid and also using it to set negative, so that behaviour is consistent on ruby side and killpg is used when needed?

Also available in: Atom PDF