lib/safe_regexp.rb
# - keepalive gets extended by whatever time the matching takes, but that should not be too bad
#   we could fix it, but that means extra overhead that I'd rather avoid
# - using select to avoid having extra threads / Timeout
keepalive gets extended by whatever time the matching takes, but that should not be too bad
we could fix it, but that means extra overhead that I'd rather avoid
using select to avoid having extra threads / Timeout
keepalive gets extended by whatever time the matching takes but that should not be too bad we could fix it but that means extra overhead that I ' d rather avoid using select to avoid having extra threads / Timeout
def executor
      Thread.current[:safe_regexp_executor] ||= begin
        in_read, in_write = IO.pipe
        out_read, out_write = IO.pipe
        pid = fork do
          in_write.close
          out_read.close
          Process.setproctitle("ruby safe_regexp worker")
          keepalive = 1 
          loop do
            break unless IO.select([in_read], nil, nil, keepalive)
            begin
              regexp, method, args, keepalive = Marshal.load(in_read)
            rescue EOFError 
              break
            end
            begin
              result = regexp.public_send(method, *args)
              result = result.to_a if result.is_a?(MatchData) 
            rescue RESCUED_EXCEPTION
              result = $!
            end
            Marshal.dump(result, out_write)
          end
          exit! 
        end
        Process.detach(pid)
        in_read.close
        out_write.close
        [out_read, in_write, pid]
      end
    end
def executor Thread . current [ :safe_regexp_executor ] ||= begin in_read , in_write = IO . pipe out_read , out_write = IO . pipe pid = fork do in_write . close out_read . close Process . setproctitle ( "ruby safe_regexp worker" ) keepalive = 1 loop do break unless IO . select ( [ in_read ] , nil , nil , keepalive ) begin regexp , method , args , keepalive = Marshal . load ( in_read ) rescue EOFError break end begin result = regexp . public_send ( method , * args ) result = result . to_a if result . is_a? ( MatchData ) rescue RESCUED_EXCEPTION result = $! end Marshal . dump ( result , out_write ) end exit! end Process . detach ( pid ) in_read . close out_write . close [ out_read , in_write , pid ] end end
keepalive gets extended by whatever time the matching takes, but that should not be too bad
we could fix it, but that means extra overhead that I'd rather avoid
 using select to avoid having extra threads / Timeout
keepalive gets extended by whatever time the matching takes but that should not be too bad we could fix it but that means extra overhead that I ' d rather avoid using select to avoid having extra threads / Timeout
# initial payload should come in shortly after boot # someone killed this fork # cannot be dumped # to not run any kind of at_exit hook the parent might have configured