Ticket #7552 (closed Bugs: fixed)

Opened 3 years ago

Last modified 3 years ago

win_iocp_io_service hangs due to a race condition

Reported by: danielg@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.49.0 Severity: Regression
Keywords: asio windows hang Cc:


The fix to #6321 introduced a bug that can cause the io_service to hang.

Example code (hangs eventually):

int main()
	for (;;)
		boost::asio::io_service ios;[]{ std::cout << '.'; });

		boost::thread_group threads;
		for (size_t i = 0; i < 2; ++i)
			threads.create_thread([&]{; });

Suppose that one of the threads read the posted operation, completed it and got to the work_finished function where it decremented outstanding_work to 0 and entered the stop function. Now the second thread starts. In the run function it sees that outstanding_work is 0 so it sets stopped to 1 and returns. Back in the first thread PostQueuedCompletionStatus?(iocp_.handle, 0, 0, 0) isn't called since stopped is already 1. do_one returns 1 and is called again. GetQueuedCompletionStatus? returns WAIT_TIMEOUT because the operation queue is empty, which causes the loop to continue to the next iteration ad infinitum.


asiohang.patch Download (590 bytes) - added by danielg@… 3 years ago.
Proposed fix for this bug.

Change History

Changed 3 years ago by danielg@…

Proposed fix for this bug.

comment:1 Changed 3 years ago by chris_kohlhoff

  • Status changed from new to closed
  • Resolution set to fixed

(In [82290]) Merge from trunk:

  • Fix some 64-to-32-bit conversion warnings. Fixes #7459
  • Fix typos in comments. Fixes #7761
  • Fix error in example embedded in basic_socket::get_option's documentation. Fixes #7562
  • Use long rather than int for SSL_CTX options, to match OpenSSL. Fixes #7209
  • Use _snwprintf to address a compile error due to the changed swprintf signature in recent versions of MinGW. Fixes #7373
  • Fix deadlock that can occur on Windows when shutting down a pool of io_service threads due to running out of work. Fixes #7552
  • Enable noexcept qualifier for error categories. Fixes #7797
  • Treat errors from accept as non-fatal. Fixes #7488
  • Add a small block recycling optimisation.
  • Version bump.
  • Regenerate documentation.

comment:2 Changed 3 years ago by anonymous

Is this fixed in boost 1.53.0 ?

I've noticed there are changes in win_iocp_io_service.ipp but not in the area pointed in the proposed patch, namely inside do_one() to prevent the infinite loop.

comment:3 Changed 3 years ago by chris_kohlhoff


Add a comment

Modify Ticket

Change Properties
<Author field>
as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.