Skip to content

Commit 174dfb6

Browse files
committed
fix error logging and except ordering
1 parent 6981086 commit 174dfb6

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

blockcopy.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ def main():
134134

135135
logger.debug('Done')
136136
except CollectedExceptions as exc:
137-
logger.error('Failed: %r', args.command, exc)
137+
logger.error('Failed: %r', exc)
138138
sys.exit(f'ERROR ({args.command}): {exc}')
139139
except Exception as exc:
140-
logger.exception('Failed: %r', args.command, exc)
140+
logger.exception('Failed: %r', exc)
141141
sys.exit(f'ERROR ({args.command}): {exc}')
142142

143143

@@ -250,14 +250,14 @@ def read_worker():
250250
# Probably `[Errno 29] Illegal seek` when reading from pipe e.g. from pv command
251251
source_end_offset = None
252252

253+
except Exception as exc:
254+
logger.exception('do_checksum read_worker failed: %r', exc)
255+
exception_collector.collect_exception(exc)
253256
except BaseException as exc:
254257
# not sure what the exception could be, but let's log it and re-raise it
255258
logger.exception('do_checksum read_worker failed (BaseException): %r', exc)
256259
exception_collector.collect_exception(exc)
257260
raise exc
258-
except Exception as exc:
259-
logger.exception('do_checksum read_worker failed: %r', exc)
260-
exception_collector.collect_exception(exc)
261261
finally:
262262
for _ in range(worker_count):
263263
block_queue.put(None)
@@ -320,14 +320,14 @@ def send_worker():
320320
hash_output_stream.write(block_hash)
321321
finally:
322322
send_queue.task_done()
323+
except Exception as exc:
324+
logger.exception('do_checksum send_worker failed: %r', exc)
325+
exception_collector.collect_exception(exc)
323326
except BaseException as exc:
324327
# not sure what the exception could be, but let's log it and re-raise it
325328
logger.exception('do_checksum send_worker failed (BaseException): %r', exc)
326329
exception_collector.collect_exception(exc)
327330
raise exc
328-
except Exception as exc:
329-
logger.exception('do_checksum send_worker failed: %r', exc)
330-
exception_collector.collect_exception(exc)
331331

332332
futures = [
333333
executor.submit(read_worker),
@@ -513,14 +513,14 @@ def flush_hash_batch():
513513
# Do not trigger the exception collector - it would make other threads terminate.
514514
# But do set some flag that the whole workflow is not running successfully.
515515
encountered_incomplete_read = exc
516+
except Exception as exc:
517+
logger.exception('do_retrieve read_worker failed: %r', exc)
518+
exception_collector.collect_exception(exc)
516519
except BaseException as exc:
517520
# not sure what the exception could be, but let's log it and re-raise it
518521
logger.exception('do_retrieve read_worker failed (BaseException): %r', exc)
519522
exception_collector.collect_exception(exc)
520523
raise exc
521-
except Exception as exc:
522-
logger.exception('do_retrieve read_worker failed: %r', exc)
523-
exception_collector.collect_exception(exc)
524524
finally:
525525
for _ in range(worker_count):
526526
hash_queue.put(None)
@@ -592,14 +592,14 @@ def send_worker():
592592
block_output_stream.flush()
593593
finally:
594594
send_queue.task_done()
595+
except Exception as exc:
596+
logger.exception('do_retrieve send_worker failed: %r', exc)
597+
exception_collector.collect_exception(exc)
595598
except BaseException as exc:
596599
# not sure what the exception could be, but let's log it and re-raise it
597600
logger.exception('do_retrieve send_worker failed (BaseException): %r', exc)
598601
exception_collector.collect_exception(exc)
599602
raise exc
600-
except Exception as exc:
601-
logger.exception('do_retrieve send_worker failed: %r', exc)
602-
exception_collector.collect_exception(exc)
603603

604604
futures = [
605605
executor.submit(read_worker),

tests/test_copy.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ def test_copy_tiny_incomplete_hash_stream(tmp_path, script_path):
5555
dst_path = tmp_path / 'dst_file'
5656
dst_path.write_bytes(b'-' * len(test_content))
5757

58-
cmd1 = [executable, script_path, '-v', 'checksum', str(dst_path)]
59-
cmd2 = [executable, script_path, '-v', 'retrieve', str(src_path)]
60-
cmd3 = [executable, script_path, '-v', 'save', str(dst_path)]
58+
cmd1 = [executable, script_path, 'checksum', str(dst_path)]
59+
cmd2 = [executable, script_path, 'retrieve', str(src_path)]
60+
cmd3 = [executable, script_path, 'save', str(dst_path)]
6161

6262
with ExitStack() as stack:
63-
print('Running: ', cmd1)
64-
p1 = stack.enter_context(Popen(cmd1, stdin=DEVNULL, stdout=PIPE))
65-
p1_output = p1.stdout.read()
63+
p1 = stack.enter_context(Popen(cmd1, stdin=DEVNULL, stdout=PIPE, stderr=PIPE))
64+
p1_output, _ = p1.communicate(timeout=5)
6665
assert p1.wait() == 0
6766
assert p1_output == (
6867
b'Hash'
@@ -77,14 +76,12 @@ def test_copy_tiny_incomplete_hash_stream(tmp_path, script_path):
7776
# remove the `done` command
7877
p1_output = p1_output[:-4]
7978

80-
print('Running: ', cmd2)
81-
p2 = stack.enter_context(Popen(cmd2, stdin=PIPE, stdout=PIPE))
79+
p2 = stack.enter_context(Popen(cmd2, stdin=PIPE, stdout=PIPE, stderr=PIPE))
8280
p2_output, _ = p2.communicate(input=p1_output, timeout=5)
8381
assert p2.wait() == 1
8482
assert p2_output == b'data\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0cHello World!'
8583

86-
print('Running: ', cmd3)
87-
p3 = stack.enter_context(Popen(cmd3, stdin=PIPE, stdout=PIPE))
84+
p3 = stack.enter_context(Popen(cmd3, stdin=PIPE, stdout=PIPE, stderr=PIPE))
8885
p3_output, _ = p3.communicate(input=p2_output, timeout=5)
8986
assert p3.wait() == 1
9087
assert p3_output == b''

0 commit comments

Comments
 (0)