Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cool.io::TCPServer continues to receive data even if its close method is called #61

Closed
abicky opened this issue Oct 22, 2017 · 3 comments

Comments

@abicky
Copy link

abicky commented Oct 22, 2017

I expect Cool.io::TCPServer to stop receiving data after Cool.io::TCPServer#close is called, but it continues to receive data. Is this behavior expected?

Here is a reproducible code:

require 'socket'
require 'cool.io'

HOST = '127.0.0.1'
PORT = 1234

class ServerConnection < Coolio::TCPSocket
  def on_read(data)
    puts "received: #{data}"
  end
end

event_loop = Coolio::Loop.new
server = Cool.io::TCPServer.new(HOST, PORT, ServerConnection)
server.attach(event_loop)

server_thr = Thread.new { event_loop.run }
sock = TCPSocket.new(HOST, PORT)

puts "cool.io version: #{Coolio::VERSION}"

puts 'send a message'
sock.send "message", 0
sleep 1

server.close
puts 'server stopped'
sleep 1

puts 'send a message'
sock.send "message", 0
sleep 1

puts 'done'

Output

% docker run --rm -v $PWD/coolio_example.rb:/coolio_example.rb ruby:2.4.2 bash -c 'gem install cool.io && ruby /coolio_example.rb'
Building native extensions.  This could take a while...
Successfully installed cool.io-1.5.1
1 gem installed
cool.io version: 1.5.1
send a message
received: message
server stopped
send a message
received: message
done
@repeatedly
Copy link
Contributor

You need to call Loop#stop before server.close.

@ganmacs
Copy link

ganmacs commented Oct 3, 2019

I think this is expected behavior.
What Cool.io::TCPServer#close does is just closing a socket listening*, not closing the accepted socket. the method is similar to TcpServer#close.

So here is the correct code to shutdown coolio.

require 'socket'
require  'cool.io'

HOST = '127.0.0.1'
PORT = 1234

class ServerConnection < Coolio::TCPSocket
  def on_read(data)
    puts "received: #{data}"
  end
end

event_loop = Coolio::Loop.new
conns = []
server = Cool.io::TCPServer.new(HOST, PORT, ServerConnection) do |conn|
  conns << conn
end
server.attach(event_loop)

server_thr = Thread.new { event_loop.run }
sock = TCPSocket.new(HOST, PORT)

puts "cool.io version: #{Coolio::VERSION}"

puts 'send a message'
sock.send "message", 0
sleep 1

server.close
conns.each do |c|
  c.close
end
puts 'server stopped'
sleep 1

puts 'send a message'
sock.send "message", 0
sleep 1

puts 'done'
$ docker run --rm -v $PWD/coolio_example.rb:/coolio_example.rb ruby:2.6 bash -c 'gem install cool.io && ruby /coolio_example.rb'
Building native extensions. This could take a while...
Successfully installed cool.io-1.5.4
1 gem installed
cool.io version: 1.5.4
send a message
received: message
server stopped
send a message
done

We can find another problem here.
Calling second sock.send "message", 0 succeeded even if the accepted socket was already closed on the server-side.
it seems to be related to fluent/fluentd#1985 (also I don't think this is coolio's issue, it's a TCP stack issue).

@ioquatix
Copy link
Member

ioquatix commented Aug 2, 2023

It sounds wrong to me, but please consider making a PR, as I don't think anyone is invested in maintaining this code base.

@ioquatix ioquatix closed this as completed Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants