ammar / regexp_parser

A regular expression parser library for Ruby
MIT License
143 stars 22 forks source link

regexp_parser is not thread-safe #45

Closed jaynetics closed 6 years ago

jaynetics commented 7 years ago

test case:

require 'uri'

@results = []
1000.times { Thread.new { @results << Regexp::Parser.parse(URI.regexp) } }
@results.map(&:strfre).uniq.count # => 10 or so (should be 1)

I guess this is due to the "instance" variables on the Scanner, Lexer and Parser classes existing on the classes themselves and thus being shared among executions. If so, it can be easily fixed by making their class methods into instance methods. E.g. for Scanner:

def self.scan(input_object, &block)
  new.scan(input_object, &block)
end

def scan(input_object, &block)
  @literal, top, stack = nil, 0, []
  # ...
end