<commit_before>require 'thread'

pid_file = ARGV.shift
scenario = ARGV.shift
ruby_exe = ARGV.shift

# We must do this first otherwise there will be a race with the process that
# creates this process and the TERM signal below could go to that process
# instead, which will likely abort the specs process.
Process.setsid if scenario

signaled = false
mutex = Mutex.new

Signal.trap(:TERM) do
  if mutex.try_lock
    unless signaled
      signaled = true
      STDOUT.puts "signaled"
      STDOUT.flush
    end
  end
end

File.open(pid_file, "wb") { |f| f.puts Process.pid }

if scenario
  # We are sending a signal to the process group
  process = "Process.getpgrp"

  case scenario
  when "self"
    signal = %["SIGTERM"]
    process = "0"
  when "group_numeric"
    signal = %[-Signal.list["TERM"]]
  when "group_short_string"
    signal = %["-TERM"]
  when "group_full_string"
    signal = %["-SIGTERM"]
  else
    raise "unknown scenario: #{scenario.inspect}"
  end

  code = "Process.kill(#{signal}, #{process})"
  system(*ruby_exe.split(' '), "-e", code)
end

sleep 0.001 until signaled<commit_msg>Use the Mutex to know whether we entered the signal handler in Process#kill specs<commit_after>require 'thread'

pid_file = ARGV.shift
scenario = ARGV.shift
ruby_exe = ARGV.shift

# We must do this first otherwise there will be a race with the process that
# creates this process and the TERM signal below could go to that process
# instead, which will likely abort the specs process.
Process.setsid if scenario

mutex = Mutex.new

Signal.trap(:TERM) do
  if mutex.try_lock
    STDOUT.puts "signaled"
    STDOUT.flush
    $signaled = true
  end
end

File.open(pid_file, "wb") { |f| f.puts Process.pid }

if scenario
  # We are sending a signal to the process group
  process = "Process.getpgrp"

  case scenario
  when "self"
    signal = %["SIGTERM"]
    process = "0"
  when "group_numeric"
    signal = %[-Signal.list["TERM"]]
  when "group_short_string"
    signal = %["-TERM"]
  when "group_full_string"
    signal = %["-SIGTERM"]
  else
    raise "unknown scenario: #{scenario.inspect}"
  end

  code = "Process.kill(#{signal}, #{process})"
  system(*ruby_exe.split(' '), "-e", code)
end

sleep 0.001 until mutex.locked? and $signaled
{'fraction_non_alphanumeric': 0.11056620597414177, 'fraction_numerical': 0.004458314757021846, 'mean_word_length': 3.5702647657841142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1979894', 'n_tokens_mistral': 779, 'n_tokens_neox': 745, 'n_words': 249}