Project

General

Profile

Actions

Bug #20490

closed

Process.waitpid2(-1, Process::WNOHANG) misbehaves on Ruby 3.1 & 3.2 with detached process

Added by stanhu (Stan Hu) about 2 months ago. Updated about 2 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-linux]
[ruby-core:117882]

Description

This is a follow-up issue for a bug that I thought was fixed in https://bugs.ruby-lang.org/issues/19837 and duplicated in https://bugs.ruby-lang.org/issues/20181.

The following script doesn't terminate quickly in Ruby 3.1.5 and 3.2.4, even with the patches to address https://bugs.ruby-lang.org/issues/19837. It works fine in Ruby 3.3. It appears that the Process::WNOHANG argument passed to Process.wait2 causes this script to spin until the child process stops:

#!/bin/env ruby

Process.spawn({}, "sh -c 'sleep 600'").tap do |pid|
  puts "detaching PID #{pid}"
  Process.detach(pid)
end

forked_pid = fork do
  loop { sleep 1 }
end

child_waiter = Thread.new do
  puts "Waiting for child process to die..."

  # The spawned process has to exit before this returns in Ruby 3.1 and 3.2
  loop do
    pid, status = Process.wait2(-1, Process::WNOHANG)

    puts "Exited PID: #{pid}, status: #{status}"

    break if pid
    sleep 1
  end
end

process_killer = Thread.new do
  puts "Killing #{forked_pid}"
  system("kill #{forked_pid}")
end

child_waiter.join
process_killer.join

If I drop the Process::WNOHANG argument, it works fine.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0