Skip to content

sl_query intermittently fails with ECONNREFUSED to the local ktx-daemon #338

Open

Description

@andreybavt

Summary

sl_query (and other daemon-backed MCP calls) intermittently fail with:

connect ECONNREFUSED 127.0.0.1:

and occasionally fetch failed / connect ECONNREFUSED 127.0.0.1:<port>. The CLI/MCP layer tries to reach the managed ktx-daemon HTTP server and nothing is listening on the expected loopback port.

Impact

Seen across several anonymous installs (multiple distinct ephemeral ports) in usage telemetry. When it happens the tool call fails outright rather than degrading. Unlike the semantic-query rejections, this is a genuine fault: the daemon is not reachable when the tool needs it.

Root cause — needs investigation

Unknown; candidates:

  • daemon not started / not yet ready when the first sl_query fires (readiness race),
  • daemon crashed/exited, leaving a stale/closed port, or
  • a port/handshake mismatch between the CLI and the managed daemon.

Ask

Add readiness/lifecycle instrumentation to distinguish "never started" vs "crashed" vs "race", and make sl_query ensure/await a healthy daemon (or retry once against a freshly resolved port) before failing.

Activity

  1. added
    bug
    area:cli
    needs-triage
    on Jul 3, 2026
  2. ashwinkrkumar commented on Oct 16, 2026

    @ashwinkrkumar

    Hi @andreybavt, I’d like to take this issue on and contribute a fix.
    I’d start by tracing the daemon-backed request paths and adding regression coverage to distinguish startup/readiness failures from a daemon exiting or a cached endpoint becoming stale.
    My proposed scope is to strengthen the shared daemon lifecycle and HTTP client rather than add a workaround only to sl_query. This would include awaiting readiness, safely invalidating stale connection information, and retrying once against a freshly resolved healthy daemon when the initial connection is refused.
    I’d also cover concurrent calls so they don’t trigger competing daemon starts or restarts, and improve lifecycle diagnostics without logging query contents or credentials. Semantic-query rejections and other application errors would remain separate from connectivity failures and would not trigger retries.
    The PR would include tests for delayed readiness, daemon exit/restart with a different port, concurrent recovery, and recovery failure. I’ll confirm the actual failing path before treating any of the suspected causes as established.

  3. ashwinkrkumar commented on Oct 18, 2026

    @ashwinkrkumar

    Hi @andreybavt, quick update: I traced this and have a fix ready on ashwinkrkumar:fix/338-managed-daemon-recovery.

    Root cause (verified): the managed daemon URL was resolved once and cached for the life of the process. In a long-running MCP server, once the daemon was stopped or replaced (another ktx command needing a feature it lacked, a CLI version change, ktx runtime stop, or a crash), every later daemon-backed request hit the old port with connect ECONNREFUSED 127.0.0.1:<port> until restart. Each daemon start gets a new ephemeral port, which fits the distinct ports seen in telemetry. I reproduced this through the real MCP server with sql_execution.

    What the fix does:

    • Re resolves the daemon and retries once, only after a confirmed connection refusal. That means the request never reached the daemon, so replaying it is safe. Resets, timeouts, HTTP errors, bad responses and semantic query rejections are never retried.
    • Shares one start/recovery per daemon across concurrent requests within a process, so they don't race each other.
    • Hardens startup: spawn failures no longer crash the process, early exits are reported immediately, health checks have timeouts, a version mismatch fails fast, and a failed start no longer removes a daemon recorded by another start.
    • Logs recovery details to stderr only. No telemetry or Python changes.

    Tests: regression tests for stale endpoints, concurrency, retry classification, readiness failures, and an end to end MCP test. 28 of them fail on the old code and pass with the fix. The full CLI test suite, typ -check, build and dead code checks pass.

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

    area:clibugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions