From aab77a85cad493d4c81da94697f39eea0d5eb93f Mon Sep 17 00:00:00 2001 From: Bastien Chatelard Date: Thu, 5 Nov 2026 20:34:10 +0100 Subject: [PATCH 1/2] fix(examples): keep 13_background_processes' processes alive until killed Process 2 ran for 5 s and was killed after a 2 s sleep plus a few API round trips; when those took over 3 s it had already ended, and the kill failed with 400 "process is not running (status: completed)". The two "sleep 5" processes killed by kill_all_processes had the same race. Let them run for 60 s: every one is killed, so the example takes no longer. --- examples/13_background_processes.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/13_background_processes.py b/examples/13_background_processes.py index a68c32f8..2f263570 100755 --- a/examples/13_background_processes.py +++ b/examples/13_background_processes.py @@ -35,9 +35,10 @@ def main(): ) print(f"Launched process 1: {process_id_1}") - # Launch another process with a different command + # Launch another process with a different command; it outlives the + # round trips below, so it is still running when it gets killed process_id_2 = sandbox.launch_process( - "python3 -c 'import time; [print(f\"Process 2: {i}\") or time.sleep(1) for i in range(5)]'" + "python3 -c 'import time; [print(f\"Process 2: {i}\") or time.sleep(1) for i in range(60)]'" ) print(f"Launched process 2: {process_id_2}") @@ -75,8 +76,8 @@ def main(): print() # Launch a few more processes - process_id_3 = sandbox.launch_process("sleep 5") - process_id_4 = sandbox.launch_process("sleep 5") + process_id_3 = sandbox.launch_process("sleep 60") + process_id_4 = sandbox.launch_process("sleep 60") print(f"Launched processes 3 and 4: {process_id_3}, {process_id_4}") # Wait a bit From 90063df02bbba1ed37f40682d3b1bf68bafec204 Mon Sep 17 00:00:00 2001 From: Bastien Chatelard Date: Thu, 5 Nov 2026 20:34:11 +0100 Subject: [PATCH 2/2] fix(examples): wait for the image's own file in 19_entrypoint_and_command A sandbox is ready once its executor answers; the image's own command (the custom command or entrypoint under test) may not have written its file yet, so the first read could come back empty. Wait up to 10 s for the file before reading it. --- examples/19_entrypoint_and_command.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/19_entrypoint_and_command.py b/examples/19_entrypoint_and_command.py index cbb6b6e2..09f04523 100644 --- a/examples/19_entrypoint_and_command.py +++ b/examples/19_entrypoint_and_command.py @@ -17,6 +17,11 @@ def main(): suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8)) + # A ready sandbox has a running executor; the image's own command may not + # have run yet. Wait up to 10 s for the file it writes before reading it. + def cat_when_present(path): + return f"for i in $(seq 50); do [ -f {path} ] && break; sleep 0.2; done; cat {path}" + # Example 1: Custom command with args print("=== Example 1: Custom command ===") sandbox = None @@ -32,7 +37,9 @@ def main(): env={"LOG_LEVEL": "DEBUG"}, timeout=300, ) - result = sandbox.exec("cat /tmp/command-was-here && echo 'File exists'") + result = sandbox.exec( + f"{cat_when_present('/tmp/command-was-here')} && echo 'File exists'" + ) print(f" {result.stdout.strip()}") assert result.exit_code == 0, "Expected /tmp/command-was-here to exist" print(" OK: custom command created the file") @@ -62,7 +69,7 @@ def main(): timeout=300, ) # The file was created by python3 (the entrypoint), not bash - result = sandbox.exec("cat /tmp/started-by-python") + result = sandbox.exec(cat_when_present("/tmp/started-by-python")) content = result.stdout.strip() print(f" Marker content: {content}") assert content == "yes", f"Expected 'yes', got '{content}'"