Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

fix(python): close stdin for one-shot executions (#2703)

kingslayer-opsdd54c9076b2d
diff --git a/docs/contributing/README.md b/docs/contributing/README.md
index e3810602e..f18c9b6ae 100644
--- a/docs/contributing/README.md
+++ b/docs/contributing/README.md
@@ -45,6 +45,8 @@ Dated root-cause analyses and design studies, newest first.
- 2026-11-13 · [Filestore lookup prerequisites](investigations/2026-11-13-filestore-lookup-prerequisites.md): automatic Filestore IAM and API prerequisites, cross-project configuration, and diagnostic error preservation.
- 2026-11-13 · [Filestore instance discovery](investigations/2026-11-13-filestore-instance-discovery.md): API-resolved NFS targets, explicit backend selection, and deterministic instance assignment.
- 2026-11-11 · [CI resource labels](investigations/2026-11-11-ci-resource-labels.md): job-selected Box resources, quota enforcement and original-operation recovery.
+
+- 2026-11-10 · [One-shot Python stdin](investigations/2026-11-10-python-one-shot-stdin-eof.md): input ownership, EOF delivery, and streaming controls.
- 2026-11-10 · [Desktop left navigation](investigations/2026-11-10-desktop-left-navigation.md): sidebar layout, hover preview and navigation behavior.
- 2026-11-10 · [Desktop visual refinement](investigations/2026-11-10-desktop-visual-refinement.md): resource view consistency and desktop review findings.
- 2026-11-09 · [Desktop dashboard consistency](investigations/2026-11-09-desktop-review-corrections.md): shared resource presentation, compact usage summaries and desktop screenshot evidence.
diff --git a/docs/contributing/investigations/2026-11-10-python-one-shot-stdin-eof.md b/docs/contributing/investigations/2026-11-10-python-one-shot-stdin-eof.md
new file mode 100644
index 000000000..02596c0d9
--- /dev/null
+++ b/docs/contributing/investigations/2026-11-10-python-one-shot-stdin-eof.md
@@ -0,0 +1,45 @@
+# Close stdin in one-shot Python exec
+
+**Status:** Implemented
+
+The one-shot Python wrappers own stdin and close it before collecting output.
+
+## Problem and scope
+
+[Issue #1978](https://github.com/boxlite-ai/boxlite/issues/1978) reproduces EOF
+readers hanging across SDKs. Python SimpleBox.exec starts a command and waits for
+its output without releasing its input writer. A command such as wc -c waits for
+EOF, so both sides wait.
+
+This change covers SimpleBox.exec and SyncSimpleBox.exec. Their streaming Box.exec
+and SyncBox.exec counterparts expose the execution handle to the caller, who
+continues to write input and close it explicitly.
+
+## Related work and decisions
+
+| Method | Evidence | Decision |
+| --- | --- | --- |
+| Close wrapper-owned stdin | [Go PR #2025](https://github.com/boxlite-ai/boxlite/pull/2025) applies it to one-shot commands | Reuse the existing Python native close operation in both wrappers |
+| End a pipe by closing its writers | [Linux pipe semantics](https://man7.org/linux/man-pages/man7/pipe.7.html) define EOF | Close before waiting for stdout or process status |
+| Add a core input flag | Issue #1978 tracks this as future work | Keep this patch at the Python wrapper boundary |
+
+## How it works
+
+A private helper takes and closes stdin after successful command launch.
+The native binding raises RuntimeError when no stdin stream is available.
+That documented absence permits output and status collection to proceed.
+Errors from an acquired stream's close operation propagate.
+
+The low-level Execution and input writer keep their current ownership.
+For example, sending hello through the low-level writer and then closing it
+must make wc -c return 5.
+
+## Validation
+
+Run synchronous and asynchronous wc -c in a real VM. Both must exit 0 and print
+0. With the Python production change reverted, both must reach the Guest timeout
+and fail the same exit-code assertion.
+
+Keep a streaming write-and-close test. Use a dedicated synchronous runtime context
+so its event loop is closed before the next asynchronous test. Run the complete
+Python unit suite and the combined timeout/EOF regressions.
diff --git a/docs/reference/python/README.md b/docs/reference/python/README.md
index 5730bbdb9..67af74501 100644
--- a/docs/reference/python/README.md
+++ b/docs/reference/python/README.md
@@ -694,6 +694,11 @@ Result of a completed execution.
**Note:** For higher-level APIs (`SimpleBox.exec()`), the result also includes `stdout` and `stderr` strings.
+`SimpleBox.exec()` and `SyncSimpleBox.exec()` close stdin before collecting output.
+Commands that read stdin therefore receive EOF. To send input, use the
+lower-level `Box.exec()` or `SyncBox.exec()` and write through
+`Execution.stdin()` before closing it.
+
---
## Box types
diff --git a/sdks/python/boxlite/exec.py b/sdks/python/boxlite/exec.py
index 576dcd4d6..e1e4d67ae 100644
--- a/sdks/python/boxlite/exec.py
+++ b/sdks/python/boxlite/exec.py
@@ -5,6 +5,10 @@ Provides Docker-like API for executing commands in boxes.
"""
from dataclasses import dataclass
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from .boxlite import Execution
__all__ = [
"ExecResult",
@@ -28,3 +32,12 @@ class ExecResult:
stdout: str
stderr: str
error_message: str | None = None
+
+
+async def _close_exec_stdin(execution: "Execution") -> None:
+ try:
+ stdin = execution.stdin()
+ except RuntimeError:
+ # The native binding raises when the execution has no stdin stream.
+ return
+ await stdin.close()
diff --git a/sdks/python/boxlite/simplebox.py b/sdks/python/boxlite/simplebox.py
index b49351e8e..9c1117686 100644
--- a/sdks/python/boxlite/simplebox.py
+++ b/sdks/python/boxlite/simplebox.py
@@ -10,7 +10,7 @@ from enum import IntEnum
from typing import TYPE_CHECKING, Optional
from .agents import Agents
-from .exec import ExecResult
+from .exec import ExecResult, _close_exec_stdin
if TYPE_CHECKING:
from .boxlite import (
@@ -379,6 +379,8 @@ class SimpleBox:
cmd, arg_list, env_list, user=user, timeout_secs=timeout, cwd=cwd
)
+ await _close_exec_stdin(execution)
+
# Get streams from Rust execution
try:
stdout = execution.stdout()
diff --git a/sdks/python/boxlite/sync_api/_simplebox.py b/sdks/python/boxlite/sync_api/_simplebox.py
index e85a8c789..6aca4631e 100644
--- a/sdks/python/boxlite/sync_api/_simplebox.py
+++ b/sdks/python/boxlite/sync_api/_simplebox.py
@@ -9,7 +9,7 @@ import asyncio
import logging
from typing import TYPE_CHECKING, Optional
-from ..exec import ExecResult
+from ..exec import ExecResult, _close_exec_stdin
from ._agents import SyncAgents
if TYPE_CHECKING:
@@ -206,6 +206,8 @@ class SyncSimpleBox:
cmd, arg_list, env_list, user=user, timeout_secs=timeout, cwd=cwd
)
+ await _close_exec_stdin(execution)
+
stdout_lines = []
stderr_lines = []
diff --git a/sdks/python/tests/test_codebox_timeout_native_boundary.py b/sdks/python/tests/test_codebox_timeout_native_boundary.py
index 9d645c3b8..2bdd7e668 100644
--- a/sdks/python/tests/test_codebox_timeout_native_boundary.py
+++ b/sdks/python/tests/test_codebox_timeout_native_boundary.py
@@ -29,6 +29,9 @@ from boxlite.codebox import CodeBox
class _FakeExecution:
"""Stands in for the PyO3 ``Execution`` handle returned by Rust."""
+ def stdin(self):
+ raise RuntimeError("stdin stream not available")
+
def stdout(self):
return None
diff --git a/sdks/python/tests/test_exec_stdin_eof.py b/sdks/python/tests/test_exec_stdin_eof.py
new file mode 100644
index 000000000..0a905680c
--- /dev/null
+++ b/sdks/python/tests/test_exec_stdin_eof.py
@@ -0,0 +1,47 @@
+import pytest
+
+import boxlite
+
+pytestmark = pytest.mark.integration
+
+
+async def test_one_shot_async_exec_closes_stdin(shared_runtime):
+ async with boxlite.SimpleBox(image="alpine:latest", runtime=shared_runtime) as box:
+ result = await box.exec("wc", "-c", timeout=3)
+
+ assert result.exit_code == 0, "wc did not exit after stdin EOF"
+ assert result.stdout.strip() == "0"
+
+
+def test_one_shot_sync_exec_closes_stdin(test_registries, tmp_path):
+ if not hasattr(boxlite, "SyncBoxlite"):
+ pytest.skip("greenlet not installed")
+ options = boxlite.Options(home_dir=str(tmp_path), image_registries=test_registries)
+ with (
+ boxlite.SyncBoxlite(options) as runtime,
+ boxlite.SyncSimpleBox(image="alpine:latest", runtime=runtime) as box,
+ ):
+ result = box.exec("wc", "-c", timeout=3)
+
+ assert result.exit_code == 0, "wc did not exit after stdin EOF"
+ assert result.stdout.strip() == "0"
+
+
+async def test_streaming_exec_keeps_stdin_open(shared_runtime):
+ box = await shared_runtime.create(
+ boxlite.BoxOptions(image="alpine:latest", auto_remove=True)
+ )
+ try:
+ execution = await box.exec("wc", ["-c"], timeout_secs=3)
+ stdin = execution.stdin()
+ await stdin.send_input(b"hello")
+ await stdin.close()
+ output = [line async for line in execution.stdout()]
+ result = await execution.wait()
+ finally:
+ await box.stop()
+
+ assert result.exit_code == 0
+ assert "".join(output).strip() == "5"