Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rack middleware to lock v8
# Rack middleware which synchronizes access to an
# in-process v8 engine embedded with The Ruby Racer
class LockV8
def initialize(app)
@app = app
# Grabs the V8 GIL for the duration of this request.
# The current implentation of Locker() is naive and
# does not catch any exceptions inside the passed in
# block. Instead, manually manage the exceptions and
# return value outside of the critical section.
# Locking will be made more robust in future versions.
def call(*args)
result = nil
err = nil
V8::C::Locker() do
result =*args)
rescue Exception => e
err = e
raise err if err
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment