Skip to content

Commit

Permalink
Merge pull request eventmachine#427 from pietern/attach-detach
Browse files Browse the repository at this point in the history
Fix crash on attach/detach in the same tick
  • Loading branch information
tmm1 committed Mar 26, 2013
2 parents a6ad6e4 + 4e73943 commit 7f5e231
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ext/em.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions tests/test_epoll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.