Skip to content

The agent that grows with you

fix(environments): scrub staged stdin when cancel lands during the upload

kshitijk4poore4f51c654e26
diff --git a/tools/environments/daytona.py b/tools/environments/daytona.py
index 44a9ee0bac9..8722d37c9ce 100644
--- a/tools/environments/daytona.py
+++ b/tools/environments/daytona.py
@@ -145,20 +145,27 @@ class DaytonaEnvironment(BaseEnvironment):
# has taken ownership of (opened + unlinked) the staged stdin file.
state = {"cancelled": False, "staged": None, "dispatched": False}
+ def scrub_staged(): # caller holds ``lock``
+ if state["staged"] and not state["dispatched"]:
+ # Uploaded but never dispatched: nothing else will unlink it.
+ # Once dispatched the user shell rm's it before running cmd.
+ with contextlib.suppress(Exception):
+ sandbox.fs.delete_file(state["staged"])
+ state["staged"] = None
+
def cancel():
with lock:
state["cancelled"] = True
- if state["staged"] and not state["dispatched"]:
- # Uploaded but never dispatched: nothing else will unlink it.
- # Once dispatched the user shell rm's it before running cmd.
- with contextlib.suppress(Exception):
- sandbox.fs.delete_file(state["staged"])
+ scrub_staged()
with contextlib.suppress(Exception):
sandbox.stop()
def exec_fn() -> tuple[str, int]:
command = cmd_string
if stdin_data is not None:
+ with lock:
+ if state["cancelled"]:
+ return ("", 130)
remote_stdin = self._staged_stdin_path()
sandbox.fs.upload_file(stdin_data.encode("utf-8", "surrogateescape"), remote_stdin)
with lock:
@@ -168,6 +175,8 @@ class DaytonaEnvironment(BaseEnvironment):
shell_cmd = f"bash {'-l ' if login else ''}-c {shlex.quote(command)}"
with lock:
if state["cancelled"]:
+ # cancel() may have run mid-upload, before ``staged`` was set.
+ scrub_staged()
return ("", 130)
state["dispatched"] = True
response = sandbox.process.exec(shell_cmd, timeout=timeout)
diff --git a/tools/environments/vercel_sandbox.py b/tools/environments/vercel_sandbox.py
index 49066c7f2a8..716c10e47fc 100644
--- a/tools/environments/vercel_sandbox.py
+++ b/tools/environments/vercel_sandbox.py
@@ -348,20 +348,27 @@ class VercelSandboxEnvironment(BaseEnvironment):
# has taken ownership of (opened + unlinked) the staged stdin file.
state = {"cancelled": False, "staged": None, "dispatched": False}
+ def scrub_staged() -> None: # caller holds ``lock``
+ if state["staged"] and not state["dispatched"]:
+ # Staged but never dispatched: scrub the payload (may hold a
+ # sudo password). Once dispatched the user shell unlinks it
+ # itself, so kill() sends no extra command.
+ with contextlib.suppress(Exception):
+ sandbox.write_files([{"path": state["staged"], "content": b"", "mode": 0o600}])
+ state["staged"] = None
+
def cancel() -> None:
with lock:
state["cancelled"] = True
- if state["staged"] and not state["dispatched"]:
- # Staged but never dispatched: scrub the payload (may hold a
- # sudo password) before stopping. Once dispatched the user
- # shell unlinks it itself, so kill() sends no extra command.
- with contextlib.suppress(Exception):
- sandbox.write_files([{"path": state["staged"], "content": b"", "mode": 0o600}])
+ scrub_staged()
self._stop_sandbox(sandbox)
def exec_fn() -> tuple[str, int]:
command = cmd_string
if stdin_data is not None:
+ with lock:
+ if state["cancelled"]:
+ return ("", 130)
remote_stdin = self._staged_stdin_path()
_retry_vercel_call(
"stdin upload",
@@ -377,6 +384,8 @@ class VercelSandboxEnvironment(BaseEnvironment):
state["staged"] = remote_stdin
with lock:
if state["cancelled"]:
+ # cancel() may have run mid-upload, before ``staged`` was set.
+ scrub_staged()
return ("", 130)
state["dispatched"] = True
return _result_parts(sandbox.run_command(