sstephenson / execjs

Run JavaScript code from Ruby
MIT License
1.16k stars 46 forks source link

Freeze/hang when using ExecJS::RubyRacerRuntime concurrently #162

Closed dtinth closed 9 years ago

dtinth commented 9 years ago

Environment

I'm developing a site using Middleman, apparently it is somehow threaded. Whenever multiple JavaScript is run in the same time using different threads, Ruby process freezes, cannot be interrupted, and must be kill -9ed.

Test Code

# deadlock_test.rb
require 'execjs'

# The following code is used to test this bug.
# It does heavy computation stuff (pauses for 0.5 seconds) and returns "ok".
code = %q((function() {
  var s = new Date().getTime() + 500
  while (new Date().getTime() < s) {
  }
  return "ok"
})())

run = proc do
  ExecJS.eval(code)
end

puts "The expected result is [ \"ok\", \"ok\" ]"

puts
puts "Running sequentially without threads (should take 1 second)"
p [run.call, run.call]

puts
puts "Running concurrently using threads (should take 0.5 seconds)"
p [Thread.new(&run), Thread.new(&run)].map(&:value)

Expected Result

▹ ruby deadlock_test.rb
The expected result is [ "ok", "ok" ]

Running sequentially without threads (should take 1 second)
["ok", "ok"]

Running concurrently using threads (should take 0.5 seconds)
["ok", "ok"]

Actual Result

▹ ruby deadlock_test.rb
The expected result is [ "ok", "ok" ]

Running sequentially without threads (should take 1 second)
["ok", "ok"]

Running concurrently using threads (should take 0.5 seconds)
^C
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C
^Z
[1]  + 41277 suspended  ruby deadlock_test.rb
▹ kill %1
▹ kill %1
▹ kill %1
▹ kill %1
▹ kill -9 %1
[1]  + 41277 killed     ruby deadlock_test.rb

Workaround

Use Node runtime with ExecJS instead: EXECJS_RUNTIME=Node

Related Issues

josh commented 9 years ago

This repository has been deprecated and has moved to https://github.com/rails/execjs.