Skip to content

Commit 9987dc7

Browse files
committed
gh-107219: Fix concurrent.futures terminate_broken()
Fix a race condition in concurrent.futures. When a process in the process pool was terminated abruptly (while the future was running or pending), close the connection write end. If the call queue is blocked on sending bytes to a worker process, closing the connection write end interrupts the send, so the queue can be closed. Changes: * _ExecutorManagerThread.terminate_broken() now closes call_queue._writer. * multiprocessing PipeConnection.close() now interrupts WaitForMultipleObjects() in _send_bytes() by cancelling the overlapped operation.
1 parent e55aab9 commit 9987dc7

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Lib/concurrent/futures/process.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ def terminate_broken(self, cause):
510510
# https://github.com/python/cpython/issues/94777
511511
self.call_queue._reader.close()
512512

513+
# gh-107219: Close the connection writer which can unblock
514+
# Queue._feed() if it was stuck in send_bytes().
515+
self.call_queue._writer.close()
516+
513517
# clean up resources
514518
self.join_executor_internals()
515519

Lib/multiprocessing/connection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
__all__ = [ 'Client', 'Listener', 'Pipe', 'wait' ]
1111

12+
import errno
1213
import io
1314
import os
1415
import sys
@@ -41,6 +42,7 @@
4142
BUFSIZE = 8192
4243
# A very generous timeout when it comes to local connections...
4344
CONNECTION_TIMEOUT = 20.
45+
WSA_OPERATION_ABORTED = 995
4446

4547
_mmap_counter = itertools.count()
4648

@@ -272,11 +274,24 @@ class PipeConnection(_ConnectionBase):
272274
"""
273275
_got_empty_message = False
274276

277+
def __init__(self, *args, **kwargs):
278+
super().__init__(*args, **kwargs)
279+
self._send_ov = None
280+
275281
def _close(self, _CloseHandle=_winapi.CloseHandle):
282+
ov = self._send_ov
283+
if ov is not None:
284+
# Interrupt WaitForMultipleObjects() in _send_bytes()
285+
ov.cancel()
276286
_CloseHandle(self._handle)
277287

278288
def _send_bytes(self, buf):
289+
if self._send_ov is not None:
290+
# A connection should only be used by a single thread
291+
raise ValueError("concurrent send_bytes() calls "
292+
"are not supported")
279293
ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
294+
self._send_ov = ov
280295
try:
281296
if err == _winapi.ERROR_IO_PENDING:
282297
waitres = _winapi.WaitForMultipleObjects(
@@ -286,7 +301,13 @@ def _send_bytes(self, buf):
286301
ov.cancel()
287302
raise
288303
finally:
304+
self._send_ov = None
289305
nwritten, err = ov.GetOverlappedResult(True)
306+
if err == WSA_OPERATION_ABORTED:
307+
# close() was called by another thread while
308+
# WaitForMultipleObjects() was waiting for the overlapped
309+
# operation.
310+
raise OSError(errno.EPIPE, "handle is closed")
290311
assert err == 0
291312
assert nwritten == len(buf)
292313

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a race condition in ``concurrent.futures``. When a process in the
2+
process pool was terminated abruptly (while the future was running or
3+
pending), close the connection write end. If the call queue is blocked on
4+
sending bytes to a worker process, closing the connection write end interrupts
5+
the send, so the queue can be closed. Patch by Victor Stinner.

0 commit comments

Comments
 (0)