diff --git a/lib/nio/selector.rb b/lib/nio/selector.rb index 1c71107..36856d7 100644 --- a/lib/nio/selector.rb +++ b/lib/nio/selector.rb @@ -51,6 +51,8 @@ def registered?(io) # Select which monitors are ready def select(timeout = nil) + selected_monitors = Set.new + @lock.synchronize do readers, writers = [@wakeup], [] @@ -63,7 +65,6 @@ def select(timeout = nil) ready_readers, ready_writers = Kernel.select readers, writers, [], timeout return unless ready_readers # timeout or wakeup - selected_monitors = Set.new ready_readers.each do |io| if io == @wakeup @@ -88,15 +89,15 @@ def select(timeout = nil) end selected_monitors << monitor end + end - if block_given? - selected_monitors.each do |m| - yield m - end - selected_monitors.size - else - selected_monitors + if block_given? + selected_monitors.each do |m| + yield m end + selected_monitors.size + else + selected_monitors end end