eventmachine/eventmachine — Issue #277: close_connection() is not reliable for outbound connections
Author: ibc
Created: 2011-11-06 13:14:58+00:00
State: open

The following code shows how calling `close_connection()` on an outbound connection produces `unbind()` to be called after long seconds (~20 in my computer).

The scripts runs EM and creates a TCP server and a TCP client that connects to the server, sends some data and after 2 seconds the client invokes `close_connection()`. The script output clearly shows that `unbind()` is called in the client ~20 seconds later.

```ruby
require "eventmachine"

class Server < EM::Connection
def unbind cause=nil
puts "[#{Time.now}] Server: unbind(#{cause}) called, exiting"
exit
end
end

class Client < EM::Connection
def unbind cause=nil
puts "[#{Time.now}] Client: unbind(#{cause}) called"
end

def connection_completed
puts "[#{Time.now}] Client: connection_completed() called"
end
end

EM.run do
EM.start_server "127.0.0.1", 1234, Server

client = EM.connect "127.0.0.1", 1234, Client

EM.add_timer(1) do
puts "[#{Time.now}] Client: calling close_connection()..."
client.close_connection
end
end
```

Script output:

```ruby
[2011-11-06 14:10:40] Client: connection_completed() called
[2011-11-06 14:10:41] Client: calling close_connection()...
[2011-11-06 14:11:00] Client: unbind() called
[2011-11-06 14:11:00] Server: unbind() called, exiting
```

Note that `unbind()` is really invoked in Client 19 seconds after invoking `close_connection()`.