Skip to content

Commit

Permalink
* fix zeromq#207: poller.remove(raw_fd) in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Orekhov authored and Pavel Orekhov committed Jan 31, 2018
1 parent b6632bf commit 23d979e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/tests/test_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ BOOST_AUTO_TEST_CASE(remove_fd_in_handler)

int test1 = 0;
loop.add(pipefd[0], [&](){
char buffer[10];
BOOST_CHECK_EQUAL(4, read(pipefd[0],buffer,10));
test1 = 1;
loop.remove(pipefd[0]);
return true;
Expand All @@ -294,7 +296,9 @@ BOOST_AUTO_TEST_CASE(remove_invalid_fd_in_handler)
BOOST_CHECK_EQUAL(0, pipe(pipefd));

int test1 = 0;
loop.add(pipefd[0], [&test1,&loop](){
loop.add(pipefd[0], [&](){
char buffer[10];
BOOST_CHECK_EQUAL(4, read(pipefd[0],buffer,10));
test1 = 1;
loop.remove(STDIN_FILENO);
return true;
Expand Down
23 changes: 20 additions & 3 deletions src/zmqpp/poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,26 @@ void poller::remove(socket_t const& socket)

void poller::remove(raw_socket_t const descriptor)
{
zmq_pollitem_t const item{ nullptr, descriptor, 0, 0 };
if (descriptor != invalid_fd)
remove(item);
if (descriptor == invalid_fd)
return;

auto found = _fdindex.find(descriptor);
if (_fdindex.end() == found) { return; }

if ( _items.size() - 1 == found->second )
{
_items.pop_back();
_fdindex.erase(found);
return;
}

std::swap(_items[found->second], _items.back());
_items.pop_back();

auto index = found->second;
_fdindex.erase(found);

reindex( index );
}

void poller::remove(zmq_pollitem_t const& item)
Expand Down

0 comments on commit 23d979e

Please sign in to comment.