Skip to content

Commit

Permalink
Don't enforce registering the same IO multiple times.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 5, 2021
1 parent 9e83b76 commit 61a1650
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
18 changes: 8 additions & 10 deletions lib/async/debug/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'delegate'

module Async
module Debug
class Monitor
class Monitor < Delegator
def initialize(monitor, selector)
@monitor = monitor
@selector = selector
end

def close
@selector.deregister(@monitor.io)
@monitor.close
def __getobj__
@monitor
end

def method_missing(*arguments, &block)
@monitor.send(*arguments)
end

def respond_to?(*arguments)
@monitor.respond_to?(*arguments)
def close
@selector.deregister(self)
@monitor.close
end

def inspect
Expand Down
23 changes: 8 additions & 15 deletions lib/async/debug/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require_relative '../logger'

require 'nio'
require 'set'

module Async
module Debug
Expand All @@ -36,7 +37,7 @@ def initialize(monitors)
class Selector
def initialize(selector = NIO::Selector.new)
@selector = selector
@monitors = {}
@monitors = Set.new
end

def register(object, interests)
Expand All @@ -46,26 +47,18 @@ def register(object, interests)
raise RuntimeError, "Could not convert #{io} into IO!"
end

if monitor = @monitors[io.fileno]
raise RuntimeError, "Trying to register monitor for #{object.inspect} but it was already registered: #{monitor.inspect}!"
end

monitor = Monitor.new(@selector.register(object, interests), self)

@monitors[io.fileno] = monitor
@monitors.add(monitor)

return monitor
end

def deregister(object)
Async.logger.debug(self) {"Deregistering #{object.inspect}."}

unless io = ::IO.try_convert(object)
raise RuntimeError, "Could not convert #{io} into IO!"
end
def deregister(monitor)
Async.logger.debug(self) {"Deregistering #{monitor.inspect}."}

unless @monitors.delete(io.fileno)
raise RuntimeError, "Trying to remove monitor for #{io.inspect} but it was not registered!"
unless @monitors.delete?(monitor)
raise RuntimeError, "Trying to remove monitor for #{monitor.inspect} but it was not registered!"
end
end

Expand All @@ -75,7 +68,7 @@ def wakeup

def close
if @monitors.any?
raise LeakError, @monitors.values
raise LeakError, @monitors
end
ensure
@selector.close
Expand Down

0 comments on commit 61a1650

Please sign in to comment.