AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ShellSession Incorrectly Times Out When Command Output Does Not End With A Newline

ShellSession uses marker detection with startswith, causing unterminated command output to be merged with completion marker, resulting in false timeout and session restart.

highConfidence 95%LangchainAffected V1.2.12

Origin Analysis

The _collect_output method in ShellSession detects completion by checking data.startswith(marker). When a command writes output without a trailing newline, the stdout readline() returns a single string containing the command output followed immediately by the completion marker and exit code. Since marker is not at the start, the detection fails, leading to timeout.
Run the provided Python script that uses ShellToolMiddleware to execute `printf 'hello-without-newline'`. Observe that the command is reported as timed out with output empty, exit_code None, and the session is restarted.

Fixing Code Block

Edge Case Audit

The fix assumes the marker does not appear legitimately in command output; use a unique UUID-based marker to minimize collision risk. The change should be tested with commands producing multi-line output, output containing marker-like strings, and on both POSIX and Windows line endings. Rollback: if issues arise, revert to previous startswith check but recommend ensuring commands always append newline. Ensure output truncation logic is applied to the prefix to avoid memory issues.

Ecosystem Topology