celluloid / celluloid-io

UNMAINTAINED: See celluloid/celluloid#779 - Evented sockets for Celluloid actors
https://celluloid.io
MIT License
879 stars 93 forks source link

Celluloid::TaskThread seems to be broken for UDP sockets. #111

Open ioquatix opened 10 years ago

ioquatix commented 10 years ago

The following example fails unless line 8 is commented out:

#!/usr/bin/env ruby
#
# Run this as: bundle exec examples/echo_server.rb

require 'bundler/setup'
require 'celluloid/io'

Celluloid.task_class = Celluloid::TaskThread

class Server
    include Celluloid::IO
    finalizer :finalize

    def initialize
        async.run
    end

    def run
        @handler = UDPHandler.new("0.0.0.0", 8000)

        loop do
            puts @handler.inspect
            sleep 10
        end
    end
end

class UDPHandler
    include Celluloid::IO
    finalizer :finalize

    def initialize(host, port)
        puts "*** Starting echo server on #{host}:#{port}"

        # Since we included Celluloid::IO, we're actually making a
        # Celluloid::IO::TCPServer here
        @server = UDPSocket.new
        @server.bind(host, port)

        async.run
    end

    def finalize
        @server.close if @server
    end

    def run
        loop { handle_connection }
    end

    def handle_connection
        data, (_, port, host) = @server.recvfrom(1024)
        puts "*** Received connection from #{host}:#{port}"

        @server.send(data, 0, host, port)
    rescue => error
        puts "*** #{error.inspect}"
    end
end

supervisor = Server.supervise
trap("INT") { supervisor.terminate; exit }
sleep

You can test the server by running:

dig @localhost -p 8000 dev.mydomain.org A

It works fine once, then the 2nd time, it gets stuck inside recvfrom. Is this correct behaviour?

ioquatix commented 10 years ago

I believe this issue is related to https://github.com/celluloid/celluloid/pull/432 as this has been merged this should now be working - could form the basis of a spec for that bug/issue.

digitalextremist commented 9 years ago

Still an issue @ioquatix?