Skip to content

Commit

Permalink
Do not close attached sockets (fixes eventmachine#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Oct 5, 2011
1 parent 43d63bc commit 6d1cdc5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ext/ed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ EventableDescriptor::EventableDescriptor (int sd, EventMachine_t *em):
bCloseNow (false),
bCloseAfterWriting (false),
MySocket (sd),
bAttached (false),
bWatchOnly (false),
EventCallback (NULL),
bCallbackUnbind (true),
Expand Down Expand Up @@ -180,7 +181,7 @@ void EventableDescriptor::Close()
MyEventMachine->Deregister (this);

// Do not close STDIN, STDOUT, STDERR
if (MySocket > 2 && !bWatchOnly) {
if (MySocket > 2 && !bAttached) {
shutdown (MySocket, 1);
close (MySocket);
}
Expand Down Expand Up @@ -459,6 +460,16 @@ void ConnectionDescriptor::SetConnectPending(bool f)
}


/**********************************
ConnectionDescriptor::SetAttached
***********************************/

void ConnectionDescriptor::SetAttached(bool state)
{
bAttached = state;
}


/**********************************
ConnectionDescriptor::SetWatchOnly
***********************************/
Expand Down
2 changes: 2 additions & 0 deletions ext/ed.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class EventableDescriptor: public Bindable_t

protected:
int MySocket;
bool bAttached;
bool bWatchOnly;

EMCallback EventCallback;
Expand Down Expand Up @@ -169,6 +170,7 @@ class ConnectionDescriptor: public EventableDescriptor

void SetNotifyReadable (bool);
void SetNotifyWritable (bool);
void SetAttached (bool);
void SetWatchOnly (bool);

bool Pause();
Expand Down
1 change: 1 addition & 0 deletions ext/em.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ const unsigned long EventMachine_t::AttachFD (int fd, bool watch_mode)
if (!cd)
throw std::runtime_error ("no connection allocated");

cd->SetAttached(true);
cd->SetWatchOnly(watch_mode);
cd->SetConnectPending (false);

Expand Down
9 changes: 9 additions & 0 deletions lib/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ def EventMachine::attach_io io, watch_mode, handler=nil, *args
c = klass.new s, *args

c.instance_variable_set(:@io, io)
c.instance_variable_set(:@watch_mode, watch_mode)
c.instance_variable_set(:@fd, fd)

@conns[s] = c
Expand Down Expand Up @@ -1418,6 +1419,14 @@ def self.event_callback conn_binding, opcode, data
else
c.unbind
end
# If this is an attached (but not watched) connection, close the underlying io object.
if c.instance_variable_defined?(:@io) and !c.instance_variable_get(:@watch_mode)
io = c.instance_variable_get(:@io)
begin
io.close
rescue Errno::EBADF, IOError
end
end
rescue
@wrapped_exception = $!
stop
Expand Down

0 comments on commit 6d1cdc5

Please sign in to comment.