<commit_before>
# 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
    Rails.logger.warn("kill -TERM -#{$?.pid}")
    msg = `kill -TERM -#{$?.pid}`
    Rails.logger.warn("output = " + msg)
    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 6<commit_after>
# runner that provides some isolation/protection/security.

require 'Runner'
require 'timeout'

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)}\""

    pipe = nil
    begin
      pipe = IO::popen(cmd, 'r')
    rescue Exception => e
      Rails.logger.warn "Execution of command #{cmd} unsuccessful"
    end

    output = ''
    begin
      status = Timeout::timeout(max_seconds) {
        Process.waitpid2(pipe.pid)
        output = pipe.gets(nil)
      }
    rescue Timeout::Error
      Process.kill('-KILL', pipe.pid)
      output = terminated(max_seconds)
    end
    pipe.close
    output


=begin
    kill = 9
    pid = Process.spawn(cmd)
    begin
      Timeout.timeout(max_seconds) do
        Process.wait(pid)
      end
    rescue TimeoutError
      Process.kill("-#{kill}", pid)
    end


    output = `timeout --signal=#{kill} --kill-after=1 #{max_seconds}s #{cmd}`
    exit_status = $?.exitstatus
    # kill process and all children
    msg = `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

end

{'fraction_non_alphanumeric': 0.13105691056910568, 'fraction_numerical': 0.0045528455284552845, 'mean_word_length': 2.888748419721871, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1841442', 'n_tokens_mistral': 1024, 'n_tokens_neox': 983, 'n_words': 249}