Skip to content

Instantly share code, notes, and snippets.

@jsvd
Created July 19, 2019 10:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsvd/a3556820266838c53dff0ed4466dc214 to your computer and use it in GitHub Desktop.
Save jsvd/a3556820266838c53dff0ed4466dc214 to your computer and use it in GitHub Desktop.
# this terminates quickly in mri but hangs in jruby
def regex_dos
"foo========:bar baz================================================bingo".scan(/(?:=+=+)+:/)
end
t = Thread.new { regex_dos(); puts "done" }
puts t.status
t.kill
puts t.status
t.join
puts "joined"
posstr = "NRNRNNVVNRNNNNNNPUNNNNPUNRNNNNNNNNNNNNNRNNNNNRNNNRNNNNJJNNCCNNNRNNVVNNNNNRNNNNNRNNNNNRNNNNNRNNNRNNNNNNNNNNCCNNNNNNNNNNNNNNNRNNNNNNPUNNNNPUNNNNNNNRNNNNNNNRNNNNNNNNNNNNNNNNNNNNNNNNNRNNNNNNNNVVNNNNNNNNNNNNNNNNNNNNNRNNNNNNNNNRNNNNNNPUNNNNPUNRNNNNNNNNCCNNJJNN"
re = /((?:(?:(?:JJ|VA)(?:DEC)?)*|AS)?(?:(?:NN|NR|NT)+(?:PU|CC|PP)?)+)$/
re_thread = Thread.new { posstr.scan(re) }
java_thread = JRuby.reference(re_thread).native_thread
# timeout after 1 seconds
sleep 1
java_thread.interrupt
sleep 1
# wait for thread to die
re_thread.join
puts "successfully interrupted thread!"
require 'timeout'
posstr = "NRNRNNVVNRNNNNNNPUNNNNPUNRNNNNNNNNNNNNNRNNNNNRNNNRNNNNJJNNCCNNNRNNVVNNNNNRNNNNNRNNNNNRNNNNNRNNNRNNNNNNNNNNCCNNNNNNNNNNNNNNNRNNNNNNPUNNNNPUNNNNNNNRNNNNNNNRNNNNNNNNNNNNNNNNNNNNNNNNNRNNNNNNNNVVNNNNNNNNNNNNNNNNNNNNNRNNNNNNNNNRNNNNNNPUNNNNPUNRNNNNNNNNCCNNJJNN"
re = /((?:(?:(?:JJ|VA)(?:DEC)?)*|AS)?(?:(?:NN|NR|NT)+(?:PU|CC|PP)?)+)$/
status = Timeout::timeout(5) { posstr.scan(re) }
puts status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment