Skip to content

No execution deadline on tool calls (and why Promise.race won't work) #2

Closed as not planned

Description

@wiverson

Problem

There is no execution deadline on MCP tool calls. A pathological call has no upper bound on how long it occupies the server.

Why a simple async wrapper does not work

The obvious fix — Promise.race([handler(), timeout]) — cannot work for our handlers, and it is worth recording why so nobody tries it.

Our vault and DB operations are synchronous: fs.readFileSync, better-sqlite3. While a synchronous handler runs, Node's event loop is blocked, so the setTimeout backing the race can never fire. The timeout only resolves after the work completes, which is exactly when it is no longer useful.

Options

  1. Pre-flight bounds (largely done). Size caps, pagination, read windows, walk-depth limits — prevent long operations rather than interrupting them. This is the current defence and covers the known hot paths.
  2. Chunked async. Yield to the event loop periodically (as indexer.rebuild() now does), which makes a deadline enforceable between chunks. Requires rewriting each path individually.
  3. Worker threads. Move vault/DB work off the main thread and kill the worker on timeout. Real isolation, but needs a separate SQLite connection per worker and significant restructuring — likely overkill for a single-user server.
  4. Instrument first. Log per-tool duration and only chunk what actually shows up as slow.

Recommendation

Do (4), then (2) for anything it flags. The remaining synchronous work (listNotes tree walk, resolveAllLinks) measures in milliseconds on a 2,600-note vault, so there is currently no evidence justifying worker threads.

Activity

  1. added
    future
    on Aug 6, 2026
  2. wiverson commented on Aug 6, 2026

    @wiverson
    ContributorAuthor

    Closing as won't fix, pending a protocol change.

    MCP tool calls are request/response: the client blocks on a result, so moving the work to a queue only changes where it waits. Progress notifications report on work in flight; they do not let the server abandon it.

    And the deadline is not really blocked by the protocol — it is blocked by our handlers being synchronous (fs.readFileSync, better-sqlite3). While one runs, nothing else in the process executes, including the timer meant to interrupt it. That leaves chunk-and-yield per handler, or worker threads with a killable child, both of which are substantial for a single-user server whose remaining synchronous work measures in milliseconds across 2,600 notes.

    Current defence stays: pre-flight bounds — size caps, pagination, read windows, walk-depth limits.

    Reopen if MCP grows an async/callback tool-call shape, or if instrumentation shows a handler that actually runs long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    future

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions