Permalink
Browse files

Do not close attached sockets (fixes #200)

  • Loading branch information...
1 parent 43d63bc commit 6d1cdc534fe36317b4dad766ae6122308be489cd @tmm1 tmm1 committed Oct 5, 2011
Showing with 24 additions and 1 deletion.
  1. +12 −1 ext/ed.cpp
  2. +2 −0 ext/ed.h
  3. +1 −0 ext/em.cpp
  4. +9 −0 lib/eventmachine.rb
View
@@ -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),
@@ -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);
}
@@ -460,6 +461,16 @@ void ConnectionDescriptor::SetConnectPending(bool f)
/**********************************
+ConnectionDescriptor::SetAttached
+***********************************/
+
+void ConnectionDescriptor::SetAttached(bool state)
+{
+ bAttached = state;
+}
+
+
+/**********************************
ConnectionDescriptor::SetWatchOnly
***********************************/
View
@@ -103,6 +103,7 @@ class EventableDescriptor: public Bindable_t
protected:
int MySocket;
+ bool bAttached;
bool bWatchOnly;
EMCallback EventCallback;
@@ -169,6 +170,7 @@ class ConnectionDescriptor: public EventableDescriptor
void SetNotifyReadable (bool);
void SetNotifyWritable (bool);
+ void SetAttached (bool);
void SetWatchOnly (bool);
bool Pause();
View
@@ -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);
View
@@ -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
@@ -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

0 comments on commit 6d1cdc5

Please sign in to comment.