forked from 1b-agent/opencadstudio-upstream
fix(io): cap line reads on the serve and MCP transports
Bug: The --serve automation loop (stdin/socket) and the MCP stdio loop read input with unbounded read_line/lines. A single line over 64 MiB - a huge paste or a hostile client - grows the buffer until the allocator aborts, the same class of failure as the HTTP path, on the non-HTTP transports. Reproduce: 1. Start --serve on a socket (or the MCP server on stdio). 2. Send one 65 MiB line with no terminating newline anywhere in it. 3. Before: RSS grows to the full line, then "memory allocation failed" (or a multi-second stall while the line is copied). 4. After: the read fails at the cap and the existing EOF arm closes the connection/stdio promptly. Fix: New shared src/io/line_read.rs module: lines_capped(reader, 64 MiB) yields Err for an over-cap line, which the serve and MCP loops already treat as EOF/close. rest.rs was refactored onto the same helpers so the cap logic lives in one place. A regression test streams a 65 MiB line at the serve socket and asserts the connection ends without a response.
This commit is contained in:
1 parent
6c3c266475
commit
be8a7b95c4
5 files changed
+197
-49
No files matched your search
+5
-2
@@ -9,7 +9,7 @@ use serde_json::{Map, Value, json};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
fs::{File, OpenOptions},
|
||||
io::{self, BufRead, BufReader, Read, Write},
|
||||
io::{self, BufReader, Read, Write},
|
||||
net::TcpStream,
|
||||
path::{Path, PathBuf},
|
||||
process::{Child, Command, Stdio},
|
||||
@@ -1171,7 +1171,10 @@ pub fn run() {
|
||||
let mut output = stdout.lock();
|
||||
let mut clients = HashMap::new();
|
||||
let mut tasks = TaskStore::default();
|
||||
for line in stdin.lock().lines() {
|
||||
for line in crate::io::line_read::lines_capped(
|
||||
stdin.lock(),
|
||||
crate::io::line_read::MAX_LINE_BYTES,
|
||||
) {
|
||||
let response = match line {
|
||||
Ok(line) if !line.trim().is_empty() => match serde_json::from_str::<Value>(&line) {
|
||||
Ok(message) => handle_message(message, &mut clients, &mut tasks),
|
||||
|
||||
Reference in new issue
Block a user