`acceptor.accept` cannot be cancelled by `acceptor.close` in Linux
Hi,
I'm experiencing problem with boost.asio. And it can be reproduced by asio as well. Is it the right place to report the issue?
Probem Description
I'm building a multithread sync tcp server, and find that I cannot gracefully shutdown the server with acceptor.close.
The description of acceptor.close is Any asynchronous accept operations will be cancelled immediately.
So I'm wondering whether it is by design or a bug for synchronous accept operations.
If it is by design, is there any suggestion to gracefully shutdown a sync server? It would be great if I don't need to mix sync operations with async operations for the server.
Reproduce Code
I make a small code to reproduce it with both sync operations and async operations. The async code works well while the sync code can only works in MacOS. (I build it using bazel with boost.asio, version = "1.87.0.bcr.1" or asio, version = "1.32.0")
Here are some of my tests:
- Async: all OS can be cancelled with code
Operation aborted. - Sync:
a. MacOS - arm64: it can be cancelled while the code is
Bad file descriptorb. AWS ec2 - Ubuntu 24.04 / 22.04 - arm64 / x86: it cannot be cancelled c. Docker - Debian 11 - arm64: it cannot be cancelled
#include <iostream>
// #include "boost/asio.hpp"
#include "asio.hpp"
std::atomic_bool running = true;
asio::io_context context;
asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), 45678);
asio::ip::tcp::acceptor accepter(context, endpoint);
void AcceptAsync() {
std::cout << "accepting socket" << std::endl;
accepter.async_accept([](asio::error_code err, asio::ip::tcp::socket socket) {
std::cout << "accepted socket" << std::endl;
if (err) {
std::cerr << "accept failed: " << err.message() << std::endl;
} else {
AcceptAsync();
socket.close(err);
if (err) {
std::cerr << "socket close " << err.message() << std::endl;
} else {
std::cout << "socket close" << std::endl;
}
}
});
}
void RunAsync() {
AcceptAsync();
while (running) {
context.run_one();
}
}
void RunSync() {
asio::error_code err;
while (running) {
asio::ip::tcp::socket socket(context);
std::cout << "accepting socket" << std::endl;
accepter.accept(socket, err);
std::cout << "accepted socket" << std::endl;
if (err) {
std::cerr << "accept failed: " << err.message() << std::endl;
continue;
}
socket.close(err);
if (err) {
std::cerr << "socket close " << err.message() << std::endl;
} else {
std::cout << "socket close" << std::endl;
}
}
}
int main() {
std::thread t(RunSync);
for (int i = 0; i < 10; ++i) {
asio::io_context context;
asio::ip::tcp::socket socket(context);
socket.connect(endpoint);
}
std::this_thread::sleep_for(std::chrono::seconds(2));
running = false;
accepter.close();
std::cerr << "accepter close" << std::endl;
t.join();
return 0;
}
I believe that asio should add a shutdown function for the acceptor to address this issue.
it's worth noting that close may only release the resources held by a file descriptor (fd), provided there are no other references to it, so calling close does not necessarily close the socket immediately. In contrast, shutdown truly closes both read and write operations.