Skip to content

Commit

Permalink
Wait for validation Pool to finish
Browse files Browse the repository at this point in the history
Co-authored-by: Jesús García Crespo <[email protected]>
  • Loading branch information
replaceafill and sevein committed Oct 16, 2024
1 parent 56a7900 commit c451b24
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 c451b24

Please sign in to comment.