<commit_before>
# runner that provides some isolation/protection/security.

require 'RawRunner'
require 'Runner'

class DockerRunner
  include Runner

  def runnable?(language)
    @installed ||= `docker images`.lines.each.collect{|line| line.split[0]}
    @installed.include?(language.image_name)
  end

  def run(paas, sandbox, command, max_seconds)
    language = sandbox.avatar.kata.language
    cmd = 'docker run -u root --rm' +
          " -v #{paas.path(sandbox)}:/sandbox:rw" +
          " -v #{paas.path(language)}:#{paas.path(language)}:ro" +
          ' -w /sandbox' +
          " #{language.image_name} /bin/bash -c \"#{with_stderr(command)}\""

    # timeout must go on 'docker run' command and not on
    # the command passed to docker run. This is to ensure
    # the docker run command does not start doing a docker pull
    # from the docker index which could easily take considerably
    # longer than the max_seconds limit.
    kill = 9
    output = `timeout --signal=#{kill} --kill-after=1 #{max_seconds}s #{cmd}`
    exit_status = $?.exitstatus
    RawRunner.new.kill_including_decendants($?.pid)
    fatal_error_signal = 128
    killed_by_timeout = fatal_error_signal + kill

    exit_status != killed_by_timeout ? output : terminated(max_seconds)
  end

end<commit_msg>DockerRunner kill child processes attempt 4<commit_after>
# runner that provides some isolation/protection/security.

require 'Runner'

class DockerRunner
  include Runner

  def runnable?(language)
    @installed ||= `docker images`.lines.each.collect{|line| line.split[0]}
    @installed.include?(language.image_name)
  end

  def run(paas, sandbox, command, max_seconds)
    language = sandbox.avatar.kata.language
    cmd = 'docker run -u root --rm' +
          " -v #{paas.path(sandbox)}:/sandbox:rw" +
          " -v #{paas.path(language)}:#{paas.path(language)}:ro" +
          ' -w /sandbox' +
          " #{language.image_name} /bin/bash -c \"#{with_stderr(command)}\""

    # timeout must go on 'docker run' command and not on
    # the command passed to docker run. This is to ensure
    # the docker run command does not start doing a docker pull
    # from the docker index which could easily take considerably
    # longer than the max_seconds limit.
    kill = 9
    output = `timeout --signal=#{kill} --kill-after=1 #{max_seconds}s #{cmd}`
    exit_status = $?.exitstatus
    # kill process and all children
    `kill -TERM -#{$?.pid}`
    fatal_error_signal = 128
    killed_by_timeout = fatal_error_signal + kill

    exit_status != killed_by_timeout ? output : terminated(max_seconds)
  end

end
{'fraction_non_alphanumeric': 0.11972371450498849, 'fraction_numerical': 0.0049884881043745204, 'mean_word_length': 3.2598039215686274, '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': '4296863', 'n_tokens_mistral': 846, 'n_tokens_neox': 801, 'n_words': 233}