Skip to content
Browse files

rescue selector deadlock and reraise with explanation message

  • Loading branch information...
1 parent 2834677 commit 3f1c437a21f0fade515fe92a6f01df2b815d4711 @igrigorik igrigorik committed Mar 30, 2011
Showing with 21 additions and 13 deletions.
  1. +21 −13 lib/agent/selector.rb
View
34 lib/agent/selector.rb
@@ -1,5 +1,4 @@
module Agent
-
Notification = Struct.new(:type, :chan)
class Selector
@@ -49,16 +48,25 @@ def select
@w.map {|c| c.register_callback(:send, s) }
@r.map {|c| c.register_callback(:receive, s) }
- n = s.receive
-
- case n.type
- when :send then @w.map {|c| c.remove_callback(:send, n.chan.name)}
- when :receive then @r.map {|c| c.remove_callback(:receive, n.chan.name)}
+ begin
+ n = s.receive
+
+ case n.type
+ when :send then @w.map {|c| c.remove_callback(:send, n.chan.name)}
+ when :receive then @r.map {|c| c.remove_callback(:receive, n.chan.name)}
+ end
+
+ op, c = @cases["#{n.chan.name}-#{n.type}"], n.chan
+ rescue Exception => e
+ if e.message =~ /deadlock/
+ raise Exception.new("Selector deadlock: can't select on channel running in same goroutine")
+ else
+ raise e
+ end
+ ensure
+ s.close
end
- op, c = @cases["#{n.chan.name}-#{n.type}"], n.chan
- s.close
-
end
op.call(c) if op
@@ -67,9 +75,9 @@ def select
private
- def uuid_channel
- UUID.generate.gsub('-','_').to_sym
- end
+ def uuid_channel
+ UUID.generate.gsub('-','_').to_sym
+ end
end
-end
+end

0 comments on commit 3f1c437

Please sign in to comment.
Something went wrong with that request. Please try again.