Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

harden(mcp): bound stderr back-pressure in the MCP subprocess logger (enqueue=True) (#5804)

LearningCircuitca589d5b9534
diff --git a/src/local_deep_research/mcp/server.py b/src/local_deep_research/mcp/server.py
index 70c6fd9060..8a1278e14a 100644
--- a/src/local_deep_research/mcp/server.py
+++ b/src/local_deep_research/mcp/server.py
@@ -1048,11 +1048,28 @@ def configure_mcp_logging(sink=None):
# default on would write them to the MCP client's stderr log on any
# failure. Companion to #4185 / config_logger's LDR_LOGURU_DIAGNOSE gate;
# the MCP subprocess has no debug mode, so the gate is unconditionally off.
+ #
+ # enqueue=True: mirrors config_logger's stderr sink (utilities/log_utils.py).
+ # Without it, this call blocks on stderr I/O while holding loguru's
+ # handler lock, and when stderr back-pressures every logging thread piles
+ # up behind that lock (#4431). The re-enable above (logger.enable(...))
+ # means every INFO+ record from the whole research pipeline now reaches
+ # this sink — that's exactly the condition #4431 was about, so this
+ # sink needs the same protection. Note it's a bounded grace window
+ # (roughly one queue pipe buffer, ~64KB on Linux), not immunity:
+ # SimpleQueue.put() still runs inside emit()'s handler lock and blocks
+ # once that buffer fills, so a stderr stall long enough to exhaust it
+ # reproduces the same pile-up. Safe with the control-char patcher above
+ # regardless: loguru applies core.patcher() to build the record and
+ # formats the sink's output string before handing it to the enqueue
+ # thread, so the message is already scrubbed by the time it's queued
+ # (verified in loguru._logger.Logger._log / loguru._handler.Handler.emit).
logger.add(
sink if sink is not None else sys.stderr,
level="INFO",
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} | {message}",
diagnose=False,
+ enqueue=True,
)
diff --git a/src/local_deep_research/utilities/log_utils.py b/src/local_deep_research/utilities/log_utils.py
index d9438ef1ce..07612587be 100644
--- a/src/local_deep_research/utilities/log_utils.py
+++ b/src/local_deep_research/utilities/log_utils.py
@@ -843,15 +843,23 @@ def config_logger(name: str, debug: bool = False) -> None:
# frame-locals leak, independent of per-site logging discipline
# (#4182).
#
- # enqueue=True on stderr: loguru emits to an in-memory queue and a
- # single background thread does the actual stderr write, so a log call
- # never blocks on stderr I/O while holding the handler's lock. Without
- # it, when stderr back-pressures (e.g. a slow/full `docker logs` pipe
- # in CI) the lock-holder blocks mid-write and ALL logging threads pile
- # up behind the lock, freezing the request pipeline for ~60s (#4431).
- # Captured forensically: 3/5 server threads parked in loguru's
- # _protected_lock under load. The database/progress sinks keep their
- # own emitting-thread context capture and are left synchronous.
+ # enqueue=True on stderr: loguru hands the formatted record to a
+ # multiprocessing.SimpleQueue and a dedicated background thread does the
+ # actual stderr write, decoupling a log call from stderr I/O for up to
+ # one queue pipe buffer (~64KB on Linux) rather than never blocking.
+ # Without it, when stderr back-pressures (e.g. a slow/full `docker logs`
+ # pipe in CI) the lock-holder blocks mid-write and ALL logging threads
+ # pile up behind the lock, freezing the request pipeline for ~60s
+ # (#4431). Captured forensically: 3/5 server threads parked in loguru's
+ # _protected_lock under load. This is a bounded grace window, not
+ # immunity: SimpleQueue.put() still runs inside Handler.emit's
+ # _protected_lock, and its OS-pipe send_bytes() blocks once that buffer
+ # fills — e.g. if the background writer thread itself stalls on a
+ # wedged stderr long enough to drain nothing, reproducing the same
+ # pile-up (loguru/_handler.py Handler.emit / _queued_writer,
+ # multiprocessing/queues.py SimpleQueue.put). The database/progress
+ # sinks keep their own emitting-thread context capture and are left
+ # synchronous.
logger.add(sys.stderr, level=sink_level, diagnose=diagnose, enqueue=True)
logger.add(database_sink, level=sink_level, diagnose=False)
logger.add(frontend_progress_sink, level=sink_level, diagnose=False)
diff --git a/tests/mcp/test_mcp_protocol.py b/tests/mcp/test_mcp_protocol.py
index 85be6b99f8..f4ecdff792 100644
--- a/tests/mcp/test_mcp_protocol.py
+++ b/tests/mcp/test_mcp_protocol.py
@@ -184,6 +184,86 @@ class TestMCPLogging:
assert stdout_output == "", f"Unexpected stdout output: {stdout_output}"
assert result["status"] == "error"
+ def test_run_server_installs_control_char_scrubber(self):
+ """run_server must install the control-char sink-scrubber AND
+ re-enable the "local_deep_research" logger namespace.
+
+ The MCP subprocess bypasses config_logger, so without an explicit
+ logger.configure(patcher=...) the raw user queries and exception
+ text logged by engines/strategies reach the MCP client's stderr
+ with C0/C1 control chars and Unicode format chars intact
+ (log-injection / forged-log-line). This asserts run_server wires
+ the same patcher config_logger installs for the web/CLI logger,
+ and that the patcher actually strips control chars from a record's
+ message using the shared strip_control_chars machinery.
+
+ It also asserts run_server re-enables the "local_deep_research"
+ logger namespace: local_deep_research/__init__.py calls
+ logger.disable("local_deep_research") at import time (so importing
+ the package doesn't spam a caller's logs before they've configured
+ sinks), and only config_logger() re-enables it elsewhere in the
+ codebase. Without an equivalent call here, EVERY app log record —
+ not just the ones the patcher above scrubs — is silently dropped
+ before reaching any sink, in this MCP subprocess: the installed
+ patcher above never runs, and the "Starting..." line below (emitted
+ from a module inside the still-disabled namespace) would itself be
+ discarded.
+ """
+ from local_deep_research.mcp.server import run_server
+ from local_deep_research.security.log_sanitizer import (
+ strip_control_chars,
+ )
+
+ configured = {}
+
+ def _fake_configure(*args, **kwargs):
+ configured["patcher"] = kwargs.get("patcher")
+
+ # Stop run_server after logging setup so we never launch the server
+ # or run the legacy-docstore migration (which needs real state).
+ class _StopHere(Exception):
+ pass
+
+ with (
+ patch("local_deep_research.mcp.server.logger.remove"),
+ patch("local_deep_research.mcp.server.logger.add"),
+ patch(
+ "local_deep_research.mcp.server.logger.enable"
+ ) as mock_enable,
+ patch(
+ "local_deep_research.mcp.server.logger.configure",
+ side_effect=_fake_configure,
+ ),
+ patch(
+ "local_deep_research.mcp.server.logger.info",
+ side_effect=_StopHere,
+ ),
+ ):
+ with pytest.raises(_StopHere):
+ run_server()
+
+ patcher = configured.get("patcher")
+ assert patcher is not None, (
+ "run_server did not call logger.configure(patcher=...) — MCP "
+ "stderr is not control-char scrubbed"
+ )
+
+ # The installed patcher must actually strip control chars, matching
+ # the shared sink-scrubber's behavior.
+ dirty = "user query\r\n2026-01-01 | FORGED admin login\x00\x1b[31m"
+ record = {"message": dirty}
+ patcher(record)
+ assert record["message"] == strip_control_chars(dirty)
+ assert "\r" not in record["message"]
+ assert "\n" not in record["message"]
+ assert "\x00" not in record["message"]
+ assert "\x1b" not in record["message"]
+
+ # Without this, the patcher above (and every other app log record)
+ # is inert: the "local_deep_research" namespace stays disabled for
+ # the lifetime of the MCP subprocess.
+ mock_enable.assert_called_once_with("local_deep_research")
+
class TestAvailableStrategies:
"""Tests for the get_available_strategies function.
diff --git a/tests/mcp/test_stderr_control_chars.py b/tests/mcp/test_stderr_control_chars.py
index 100f05281f..ba98263feb 100644
--- a/tests/mcp/test_stderr_control_chars.py
+++ b/tests/mcp/test_stderr_control_chars.py
@@ -63,6 +63,14 @@ def emit():
def _emit(call):
call()
+ # configure_mcp_logging's stderr sink now runs with enqueue=True
+ # (a back-pressure protection -- see the logger.add() comment in
+ # mcp/server.py), so the write happens on loguru's background
+ # queue-writer thread rather than inline in call() above.
+ # logger.complete() blocks until every enqueued message has been
+ # handed to the sink, so the read below stays deterministic
+ # instead of racing that thread.
+ logger.complete()
return sink.getvalue()
yield _emit