asio icon indicating copy to clipboard operation
asio copied to clipboard

High-frequency connect/close can crash the TCP server. 0xC0000005

Open kahotv opened this issue 7 months ago • 1 comments

tcp server : echo_server.cpp

build : vs2022,Release,x86,WinSDK10.0.22621.0, /std:c++20

asio : SHA-1: https://github.com/chriskohlhoff/asio/commit/ed6aa8a13d51dfc6c00ae453fc9fb7df5d6ea963 asio version 1.34.2 released

crash when using /Ob2 and /Od Will not crash when using /Ob2 and /O2

run ProxyServer.exe(examples\cpp20\coroutines\echo_server.cpp)(crash) run ConnectClient.exe

see https://github.com/kahotv/crush_destroy_coro_3.git

ConnectClient.exe :

awaitable<void> corosleep(int ms)
{
	asio::steady_timer timer(co_await this_coro::executor);
	timer.expires_from_now(std::chrono::milliseconds(ms));
	auto [ec] = co_await timer.async_wait(as_tuple(use_awaitable));
}
awaitable<void> work(int num, int port)
{
	auto ctx = co_await this_coro::executor;
	tcp::endpoint ep({ address_v4::loopback() }, port);
	char buf[] = "abcdefg";

	std::vector<tcp::socket> v;

	for (int i = 0; i < num; i++)
	{
		tcp::socket sock(ctx);
		auto [ec] = co_await sock.async_connect(ep, as_tuple(use_awaitable));
		if (!ec)
		{
			sock.async_send(asio::buffer(buf), as_tuple(use_awaitable));
		}
		v.push_back(std::move(sock));
	}

	// close all socket
	co_await corosleep(1000);
}

int main()
{
	int num = 100;
	int port = 55555;
	asio::io_context ctx;

	co_spawn(ctx, work(num, port), detached);

	ctx.run();
	ctx.stop();
	printf("test end\n");
	system("pause");
}

kahotv avatar Jun 17 '25 03:06 kahotv

This seems to be a bug in the msvc compiler, where some internal implementations of coroutines rely on the optimizer, especially features like symmetric transfer, It is recommended to always enable optimization.

Jackarain avatar Jul 05 '25 04:07 Jackarain