Skip to content

Instantly share code, notes, and snippets.

@monolar
Last active August 29, 2015 14:07
Show Gist options
  • Save monolar/8835598d59ef9d1a2d41 to your computer and use it in GitHub Desktop.
Save monolar/8835598d59ef9d1a2d41 to your computer and use it in GitHub Desktop.
Celluloid-IO TCPSocket memleak/filehandle leak issue
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'celluloid/io'
require 'chromatic'
require 'objspace'
class Client
include Celluloid::IO
def initialize
@socket = nil
@connecting_timer = nil
reconnect
run
end
def run
loop do
sleep 1
end
end
def disconnect
return unless @socket
@socket.close
@socket = nil
end
def reconnect
puts "connecting ...".green
if @connecting_timer
@connecting_timer.cancel
@connecting_timer = nil
end
disconnect
@socket = TCPSocket.new('localhost', 55555)
rescue Exception => e
puts "error while connecting: #{e.inspect}:\n #{e.backtrace.join("\n ")}".red
disconnect
dump_objects
@connecting_timer = after(1) {
reconnect
}
end
def dump_objects
GC.start(full_mark: true, immediate_sweep: true)
puts ObjectSpace.count_objects[:T_OBJECT]
c = ObjectSpace.each_object(Socket) do |s|
s.close unless s.closed?
end
puts "#{c} sockets"
end
end
Client.new
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'celluloid'
require 'celluloid/io'
class Manager
include Celluloid
trap_exit :actor_died
def initialize
connect
run
end
def actor_died(actor, reason)
c = ObjectSpace.each_object(Socket) { |s| }
puts "#{c} sockets"
after (1) {
connect
}
end
def connect
Connection.new_link.async.connect
end
def run
loop do
sleep 1
end
end
end
class Connection
include Celluloid::IO
def connect
@socket = Celluloid::IO::TCPSocket.new('localhost', 5555)
# would actually do sensible stuff with the socket now...
end
end
Manager.new
@mikeatlas
Copy link

I have something like:

finalizer :terminate_myactor

def terminate_myactor
        begin       
            @socket.close if [email protected]? && [email protected]? && [email protected]?
        rescue Celluloid::Task::TerminatedError
            begin
                @socket.close if [email protected]? && [email protected]?
            rescue Exception
                # at this point we can assume things are cleaned up enough
            end
        end
end

The operating system will eventually release the socket file handles in the process, after they've been in CLOSE_WAIT for awhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment