Permalink
Browse files

Merge pull request #427 from pietern/attach-detach

Fix crash on attach/detach in the same tick
  • Loading branch information...
2 parents a6ad6e4 + 4e73943 commit 7f5e231b756220d19a2ecae49095a32788832e42 @tmm1 tmm1 committed Mar 26, 2013
Showing with 23 additions and 0 deletions.
  1. +8 −0 ext/em.cpp
  2. +15 −0 tests/test_epoll.rb
View
@@ -1399,6 +1399,14 @@ int EventMachine_t::DetachFD (EventableDescriptor *ed)
// Prevent the descriptor from being modified, in case DetachFD was called from a timer or next_tick
ModifiedDescriptors.erase (ed);
+ // Prevent the descriptor from being added, in case DetachFD was called in the same tick as AttachFD
+ for (size_t i = 0; i < NewDescriptors.size(); i++) {
+ if (ed == NewDescriptors[i]) {
+ NewDescriptors.erase(NewDescriptors.begin() + i);
+ break;
+ }
+ }
+
// Set MySocket = INVALID_SOCKET so ShouldDelete() is true (and the descriptor gets deleted and removed),
// and also to prevent anyone from calling close() on the detached fd
ed->SetSocketInvalid();
View
@@ -126,5 +126,20 @@ def _test_unix_domain
File.unlink(fn) if File.exist?(fn)
end
+ def test_attach_detach
+ EM.epoll
+ EM.run {
+ EM.add_timer(0.01) { EM.stop }
+
+ r, w = IO.pipe
+
+ # This tests a regression where detach in the same tick as attach crashes EM
+ EM.watch(r) do |connection|
+ connection.detach
+ end
+ }
+
+ assert true
+ end
end

0 comments on commit 7f5e231

Please sign in to comment.