Skip to content

Commit

Permalink
Merge pull request #183 from sevein/dev/issue-173-multiprocessing-unb…
Browse files Browse the repository at this point in the history
…ound

Wait for pool to finish in validation
  • Loading branch information
acdha authored Oct 16, 2024
2 parents 56a7900 + c451b24 commit da04180
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 6 additions & 7 deletions bagit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit da04180

Please sign in to comment.