# File lib/celluloid/io/reactor.rb, line 30
      def wait(io, set)
        # zomg ugly type conversion :(
        unless io.is_a?(::IO) || io.is_a?(OpenSSL::SSL::SSLSocket)
          if io.respond_to? :to_io
            io = io.to_io
          elsif ::IO.respond_to? :try_convert
            io = ::IO.try_convert(io)
          end

          fail TypeError, "can't convert #{io.class} into IO" unless io.is_a?(::IO)
        end

        monitor = @selector.register(io, set)
        monitor.value = Task.current

        begin
          Task.suspend :iowait
        ensure
          # In all cases we want to ensure that the monitor is closed once we
          # have woken up. However, in some cases, the monitor is already
          # invalid, e.g. in the case that we are terminating. We catch this
          # case explicitly.
          monitor.close unless monitor.closed?
        end
      end