The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(langchain): shell output multithreading race condition (#34333)
Mason Daugherty54241f4d0694
diff --git a/libs/langchain_v1/langchain/agents/middleware/shell_tool.py b/libs/langchain_v1/langchain/agents/middleware/shell_tool.pyindex a8b0737b2e..7efe48c719 100644--- a/libs/langchain_v1/langchain/agents/middleware/shell_tool.py+++ b/libs/langchain_v1/langchain/agents/middleware/shell_tool.py@@ -248,6 +248,10 @@ class ShellSession:if source == "stdout" and data.startswith(marker):_, _, status = data.partition(" ")exit_code = self._safe_int(status.strip())+ # Drain any remaining stderr that may have arrived concurrently.+ # The stderr reader thread runs independently, so output might+ # still be in flight when the stdout marker arrives.+ self._drain_remaining_stderr(collected, deadline)breaktotal_lines += 1@@ -323,6 +327,37 @@ class ShellSession:except queue.Empty:break+ def _drain_remaining_stderr(+ self, collected: list[str], deadline: float, drain_timeout: float = 0.05+ ) -> None:+ """Drain any stderr output that arrived concurrently with the done marker.++ The stdout and stderr reader threads run independently. When a command writes to+ stderr just before exiting, the stderr output may still be in transit when the+ done marker arrives on stdout. This method briefly polls the queue to capture+ such output.++ Args:+ collected: The list to append collected stderr lines to.+ deadline: The original command deadline (used as an upper bound).+ drain_timeout: Maximum time to wait for additional stderr output.+ """+ drain_deadline = min(time.monotonic() + drain_timeout, deadline)+ while True:+ remaining = drain_deadline - time.monotonic()+ if remaining <= 0:+ break+ try:+ source, data = self._queue.get(timeout=remaining)+ except queue.Empty:+ break+ if data is None or source != "stderr":+ continue+ stripped = data.rstrip("\n")+ collected.append(f"[stderr] {stripped}")+ if data.endswith("\n"):+ collected.append("\n")+@staticmethoddef _safe_int(value: str) -> int | None:with contextlib.suppress(ValueError):diff --git a/libs/langchain_v1/uv.lock b/libs/langchain_v1/uv.lockindex 42fd5ec20c..dc40e93ff4 100644--- a/libs/langchain_v1/uv.lock+++ b/libs/langchain_v1/uv.lock@@ -2102,7 +2102,7 @@ wheels = [[[package]]name = "langchain-core"-version = "1.1.3"+version = "1.2.0"source = { editable = "../core" }dependencies = [{ name = "jsonpatch" },@@ -2283,7 +2283,7 @@ wheels = [[[package]]name = "langchain-openai"-version = "1.1.2"+version = "1.1.3"source = { editable = "../partners/openai" }dependencies = [{ name = "langchain-core" },