0

I am trying to cancel a TCP acceptor by the programs keeps blocked in that line.

One thread waits for connections like this:

boost::system::error_code ec;
acceptor_ = new boost::asio::ip::tcp::acceptor(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
acceptor_->accept(socket_, ec);  // socket_ is a boost::asio::ip::tcp:socket

The thread that must close the acceptor does:

boost::system::error_code ec;
acceptor_->close(ec);

Is there something I am missing? I am not using any async operation, so may I do something with the io_service object?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
  • You could try using `acceptor_->cancel();` before closing it. I'm using that in my program and it works fine. However I think `close()` should also work. Are you sure you are working on the same acceptor object? – Matthias247 Aug 05 '14 at 10:58
  • Yes, same acceptor of course. My code works on Windows but not on Linux, any ideas? – Didac Perez Parera Aug 05 '14 at 13:28
  • What is the stack trace when `close()` blocks? Why are you canceling from [another thread](http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/ip__tcp/acceptor.html)? – Sam Miller Aug 05 '14 at 17:22
  • Why can not cancel the acceptor from another thread? I mean, if accept is a blocking operation, how can I cancel it from the same thread? – Didac Perez Parera Aug 06 '14 at 07:27

1 Answers1

1

In short, you cannot cancel most of async operations using asio function and methods.

The problem is underlying accept, read etc are system calls. They cannot be interrupted using io_service::stop or cancel since accepting thread is blocked inside syscall, not asio event loop. They only can be interrupted by killing thread or sending a signal.

I'd prefer to make this acceptor asyncronous, so problem will gone. Also see this Q on SO about this

Community
  • 1
  • 1
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50