Skip to content

Commit

Permalink
Fix error when stopping server on UNIX domain socket, fixes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
macournoyer committed Feb 13, 2008
1 parent a359e88 commit 7e8c975
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,4 +1,5 @@
== 0.6.4 Sexy Lobster release
* Fix error when stopping server on UNIX domain socket, fixes #42
* Rescue errors in Connection#get_peername more gracefully, setting REMOTE_ADDR to nil, fixes #43

== 0.6.3 Ninja Cookie release
Expand Down
8 changes: 6 additions & 2 deletions lib/thin/connectors/unix_server.rb
@@ -1,6 +1,6 @@
module Thin
module Connectors
# Connectior to act as a UNIX domain socket server.
# Connector to act as a UNIX domain socket server.
class UnixServer < Connector
# UNIX domain socket on which the server is listening for connections.
attr_accessor :socket
Expand All @@ -14,7 +14,10 @@ def initialize(socket)
# Connect the server
def connect
at_exit { remove_socket_file } # In case it crashes
@signature = EventMachine.start_unix_domain_server(@socket, UnixConnection, &method(:initialize_connection))
EventMachine.start_unix_domain_server(@socket, UnixConnection, &method(:initialize_connection))
# HACK EventMachine.start_unix_domain_server doesn't return the connection signature
# so we have to go in the internal stuff to find it.
@signature = EventMachine.instance_eval{@acceptors.keys.first}
end

# Stops the server
Expand All @@ -38,6 +41,7 @@ def remove_socket_file
end
end

# Connection through a UNIX domain socket.
class UnixConnection < Connection
def remote_address
# FIXME not sure about this, does it even make sense on a UNIX socket?
Expand Down
22 changes: 22 additions & 0 deletions spec/connectors/tcp_server_spec.rb
@@ -0,0 +1,22 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Connectors::TcpServer do
before do
@connector = Connectors::TcpServer.new('0.0.0.0', 3333)
end

it "should connect" do
EventMachine.run do
@connector.connect
EventMachine.stop
end
end

it "should disconnect" do
EventMachine.run do
@connector.connect
@connector.disconnect
EventMachine.stop
end
end
end
23 changes: 23 additions & 0 deletions spec/connectors/unix_server_spec.rb
@@ -1,6 +1,29 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Connectors::UnixServer do
before do
@connector = Connectors::UnixServer.new('/tmp/thin-test.sock')
end

it "should connect" do
EventMachine.run do
@connector.connect
EventMachine.stop
end
end

it "should disconnect" do
EventMachine.run do
@connector.connect
@connector.disconnect
EventMachine.stop
end
end

it "should remove socket file on close" do
@connector.close
File.exist?('/tmp/thin-test.sock').should be_false
end
end

describe UnixConnection do
Expand Down

0 comments on commit 7e8c975

Please sign in to comment.