Skip to content

Commit 7516811

Browse files
bobbyiBobby Impollonia
andauthored
fix(setup_helpers): ensure ThreadPool is closed (#3548)
* Ensure ThreadPool is closed in setup_helpers The ParallelCompile setup helper using a ThreadPool to enable its parallelism. It does not properly close the pool when it is done with it. This can lead to a "Exception ignored in: <function Pool.__del__..." message with traceback being printed at shutdown. Use pool.terminate() instead of context manager for Python 2.7 compatibility * Add note to remove code that supports Python 2 Co-authored-by: Bobby Impollonia <[email protected]>
1 parent 59aa998 commit 7516811

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pybind11/setup_helpers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,14 @@ def _single_compile(obj):
466466
threads = 1
467467

468468
if threads > 1:
469-
for _ in ThreadPool(threads).imap_unordered(_single_compile, objects):
470-
pass
469+
pool = ThreadPool(threads)
470+
# In Python 2, ThreadPool can't be used as a context manager.
471+
# Once we are no longer supporting it, this can be 'with pool:'
472+
try:
473+
for _ in pool.imap_unordered(_single_compile, objects):
474+
pass
475+
finally:
476+
pool.terminate()
471477
else:
472478
for ob in objects:
473479
_single_compile(ob)

0 commit comments

Comments
 (0)