From c451b244991d58bb2011ba0c2bd54f1d9fc04f2b Mon Sep 17 00:00:00 2001 From: "Douglas Cerna (Soy Douglas)" Date: Sun, 25 Feb 2024 17:11:12 +0100 Subject: [PATCH] Wait for validation Pool to finish MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jesús García Crespo --- bagit.py | 13 ++++++------- test.py | 9 +++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bagit.py b/bagit.py index 0a5c0f3..69ea9ab 100755 --- a/bagit.py +++ b/bagit.py @@ -882,13 +882,12 @@ def _validate_entries(self, processes): if processes == 1: hash_results = [_calc_hashes(i) for i in args] else: - try: - pool = multiprocessing.Pool( - processes if processes else None, initializer=worker_init - ) - hash_results = pool.map(_calc_hashes, args) - finally: - pool.terminate() + pool = multiprocessing.Pool( + processes if processes else None, initializer=worker_init + ) + hash_results = pool.map(_calc_hashes, args) + pool.close() + pool.join() # Any unhandled exceptions are probably fatal except: diff --git a/test.py b/test.py index 1e7c22f..735bd73 100644 --- a/test.py +++ b/test.py @@ -458,6 +458,15 @@ def validate(self, bag, *args, **kwargs): bag, *args, processes=2, **kwargs ) + @mock.patch("bagit.multiprocessing.Pool") + def test_validate_pool_error(self, pool): + # Simulate the Pool constructor raising a RuntimeError. + pool.side_effect = RuntimeError + bag = bagit.make_bag(self.tmpdir) + # Previously, this raised UnboundLocalError if uninitialized. + with self.assertRaises(RuntimeError): + self.validate(bag) + @mock.patch( "bagit.VERSION", new="1.5.4"