From b33e51144e679d71a080bc54e8f71e0c9ccc48b2 Mon Sep 17 00:00:00 2001 From: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:00:07 +0000 Subject: [PATCH] Stop shell_command from swallowing exceptions in docker-to-squash.py The return statement in the finally clause of shell_command discards every exception raised in the try/except blocks, so raise_on_error=True never raises: a failing command returns its return code and a command that cannot be started returns (None, None, -1). Move the return below the try/except/finally so exceptions propagate and only the timer cleanup stays in finally. Call sites that pass raise_on_error=True either ignore the result or already expect an exception (check_dependencies wraps each call in try/except and could never detect a missing dependency before this change). Update test_shell_command to assert that failures raise. Signed-off-by: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com> --- bin/docker-to-squash.py | 3 +-- bin/test_docker_to_squash.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/docker-to-squash.py b/bin/docker-to-squash.py index 4ea3a371f6b..c35848d5e70 100755 --- a/bin/docker-to-squash.py +++ b/bin/docker-to-squash.py @@ -90,10 +90,9 @@ def shell_command(command, print_stdout, print_stderr, raise_on_error, timeout_s process.kill() raise Exception("Popen failure, " + str(ex)) finally: - # Note that finally clause executes even when there is no exception, hence the "return" statement if timer: timer.cancel() - return out, err, (process.returncode if process else -1) + return out, err, (process.returncode if process else -1) def process_timeout(process): diff --git a/bin/test_docker_to_squash.py b/bin/test_docker_to_squash.py index d7a44e95b35..ed23928b847 100644 --- a/bin/test_docker_to_squash.py +++ b/bin/test_docker_to_squash.py @@ -33,10 +33,16 @@ def test_shell_command(self): out, err, rc = dtsq.shell_command(cmd, True, True, True, timeout_sec=10) self.assertEqual(0, rc, f"Failed cmd={cmd}\nrc={rc}\nout={out}\nerr={err}") - # expect failure + # expect failure: with raise_on_error set, a command that cannot be + # started raises instead of returning a return code cmd = ["badcmd", "-l"] - out, err, rc = dtsq.shell_command(cmd, True, True, True, timeout_sec=10) - self.assertNotEqual(0, rc, f"Expected to fail cmd={cmd}\nrc={rc}\nout={out}\nerr={err}") + with self.assertRaises(Exception): + dtsq.shell_command(cmd, True, True, True, timeout_sec=10) + + # expect failure: with raise_on_error set, a non-zero exit code raises + cmd = ["ls", "/nope"] + with self.assertRaises(Exception): + dtsq.shell_command(cmd, True, True, True, timeout_sec=10) # TODO: # def test_read_image_tag_to_hash(self):