DocsGPT icon indicating copy to clipboard operation
DocsGPT copied to clipboard

Infinite loop in _build_messages when a long message sheds all retrieved documents

Open sonu7089 opened this issue 1 month ago • 3 comments

What happens

A chat turn can hang forever at 100% CPU inside BaseAgent._build_messages (docsgpt/agents/base.py, the while loop around line 1600). The loop runs before any LLM call, so the request never streams, never errors, and never times out — the worker thread just spins.

Where

while (
    document_block
    and num_tokens_from_string(self._compose_user_turn(document_block, query))
    > max_query_tokens - reserved
):
    self.retrieved_docs = self.retrieved_docs[:-1]
    document_block = self._build_document_block()

Why it loops forever

The loop shrinks the document block by dropping retrieved documents one at a time, assuming each pass makes the block smaller. That assumption breaks once retrieved_docs runs out: when a search ran but found nothing (or all hits have been shed), _build_document_block() returns the fixed, non-empty EMPTY_RETRIEVAL_NOTE instead of an empty string:

formatted = format_docs_for_prompt(getattr(self, "retrieved_docs", None))
if not formatted:
    ...
    searched = getattr(self, "sources_were_searched", False)
    return self.EMPTY_RETRIEVAL_NOTE if searched else ""

From that point on:

  • self.retrieved_docs[:-1] on an empty list is still []
  • _build_document_block() returns the same fixed-size note on every pass
  • the while condition keeps seeing the same value, so it never exits

When it triggers

All three at once:

  1. The user's message is just under its token budget (it passes the earlier query_tokens > max_query_tokens check in the same function).
  2. Sources were searched (sources_were_searched=True, i.e. the normal case for a conversation with sources attached) and retrieval returned nothing.
  3. The note + message together still exceed what's left of the budget (max_query_tokens - reserved). reserved includes the attachment manifest (~150 tokens) whenever any file is listed, which widens the window.

With a 128k-token model this means a message of roughly 91k tokens (~370 KB of pasted text) — unusual by hand, but easy for a /v1 client to produce on purpose.

How it was found

Reading the shedding loop and noticing EMPTY_RETRIEVAL_NOTE is a constant that survives the shedding, then confirming with a small script that calls the real BaseAgent._build_messages (2,000-token window, a query 10 tokens under its budget, stubbed LLM):

search ran, retrieval empty:                   does not return
search ran, one 500-token hit:                 25,856 _build_document_block() calls in 8s, still spinning
control — search did NOT run, same query/doc:  returns normally after 2 calls

Only the sources_were_searched=True cases hang. The control case sheds down to an empty block and exits cleanly, which isolates the note as the cause.

Impact

  • Each such request pins a worker thread indefinitely at full CPU; nothing is logged and no tokens are spent, so nothing surfaces the problem.
  • Reachable from the chat endpoints and from scheduled/background agent runs (they call gen() → _build_messages too).
  • A few of these requests exhaust the worker thread pool.

sonu7089 avatar Oct 14 '26 17:10 sonu7089

Hi @arc53-machine, could you please assign this issue to me.

sonu7089 avatar Oct 14 '26 17:10 sonu7089

Thanks. Please report vulnerabilities privately through https://github.com/arc53/DocsGPT/security (Report a vulnerability) or [email protected] so the details stay private.

Automated triage. A maintainer will follow up.

arc53-machine avatar Oct 14 '26 17:10 arc53-machine

Understood — thank you for the pointer. Closing this so the details stay private, and re-reporting through [email protected] as requested. The public report can be deleted/managed per your disclosure process; happy to provide the reproduction script and help validate a fix once triaged.

sonu7089 avatar Oct 14 '26 17:10 sonu7089