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"