From 7516811315c76321e847cf28955937195c577d2f Mon Sep 17 00:00:00 2001 From: Bobby Impollonia Date: Mon, 13 Dec 2021 12:45:33 -0500 Subject: [PATCH] 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: --- pybind11/setup_helpers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index d6d84f67..5b7c9aab 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -466,8 +466,14 @@ class ParallelCompile(object): threads = 1 if threads > 1: - for _ in ThreadPool(threads).imap_unordered(_single_compile, objects): - pass + pool = ThreadPool(threads) + # In Python 2, ThreadPool can't be used as a context manager. + # Once we are no longer supporting it, this can be 'with pool:' + try: + for _ in pool.imap_unordered(_single_compile, objects): + pass + finally: + pool.terminate() else: for ob in objects: _single_compile(ob)