Skip to content

Instantly share code, notes, and snippets.

@pettyjamesm
Created August 3, 2012 20:41
Show Gist options
  • Save pettyjamesm/3251326 to your computer and use it in GitHub Desktop.
Save pettyjamesm/3251326 to your computer and use it in GitHub Desktop.
Awesome Ruby Synchronization Behaviors
class Browser
CreationMutex = Mutex.new
def _create_monitor_synchronized
if ((not defined?(@monit)) or @monit.nil?)
CreationMutex.synchronize do
if ((not defined?(@monit)) or @monit.nil?)
puts "Thread #{Thread.current.object_id} created @monit"
@monit = Object.new
@monit.extend(MonitorMixin)
end
end
end
@monit
end
def synchronize(&block)
_create_monitor_synchronized if ((not defined?(@monit)) or @monit.nil?)
@monit.synchronize(&block)
end
alias original_command command
def command(*args)
res = nil
synchronize do
puts "Thread #{Thread.current.object_id} running #{args[0]}"
res = original_command(*args)
puts "Thread #{Thread.current.object_id} exiting monitor"
end
res
end
end
Thread 18826100 created @monit
Thread 18826100 running current_url
{"name"=>"current_url", "args"=>[]}
Thread 18820220 created @monit
Thread 18820220 running current_url
{"name"=>"current_url", "args"=>[]}
{"response"=>"about:blank"}
Thread 18820220 exiting monitor
*BOOM* blowup on the websocket server from unsynchronized socket reads.
Exception: negative string size (or size too big)
/.../poltergeist-0.7.0/lib/capybara/poltergeist/web_socket_server.rb:95:in `<<'
/.../poltergeist-0.7.0/lib/capybara/poltergeist/web_socket_server.rb:95:in `accept'
/.../poltergeist-0.7.0/lib/capybara/poltergeist/web_socket_server.rb:143:in `send'
/.../poltergeist-0.7.0/lib/capybara/poltergeist/server.rb:29:in `send'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment