Skip to content

Commit da04180

Browse files
authored
Merge pull request #183 from sevein/dev/issue-173-multiprocessing-unbound
Wait for pool to finish in validation
2 parents 56a7900 + c451b24 commit da04180

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

bagit.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,12 @@ def _validate_entries(self, processes):
882882
if processes == 1:
883883
hash_results = [_calc_hashes(i) for i in args]
884884
else:
885-
try:
886-
pool = multiprocessing.Pool(
887-
processes if processes else None, initializer=worker_init
888-
)
889-
hash_results = pool.map(_calc_hashes, args)
890-
finally:
891-
pool.terminate()
885+
pool = multiprocessing.Pool(
886+
processes if processes else None, initializer=worker_init
887+
)
888+
hash_results = pool.map(_calc_hashes, args)
889+
pool.close()
890+
pool.join()
892891

893892
# Any unhandled exceptions are probably fatal
894893
except:

test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@ def validate(self, bag, *args, **kwargs):
458458
bag, *args, processes=2, **kwargs
459459
)
460460

461+
@mock.patch("bagit.multiprocessing.Pool")
462+
def test_validate_pool_error(self, pool):
463+
# Simulate the Pool constructor raising a RuntimeError.
464+
pool.side_effect = RuntimeError
465+
bag = bagit.make_bag(self.tmpdir)
466+
# Previously, this raised UnboundLocalError if uninitialized.
467+
with self.assertRaises(RuntimeError):
468+
self.validate(bag)
469+
461470

462471
@mock.patch(
463472
"bagit.VERSION", new="1.5.4"

0 commit comments

Comments
 (0)