Skip to content

fix(core): stop dialling a session the tunnel service abandoned - #182

Merged
robinbraemer merged 3 commits into
mainfrom
fix/tunnel-abandoned-session-dial
Nov 24, 2026
Merged

robinbraemer merged 3 commits into
mainfrom
fix/tunnel-abandoned-session-dial

Conversation

@minekube-ai-engineer

Copy link
Copy Markdown

Root cause

A connector can issue more than one tunnel dial for one session id: an edge re-proposal after
its answer window expires, or a watch fail-over re-publish. When such a dial lands after the edge
has already given up on the session — the tunnel service answers
404 session <id> not found … pendingState=released — the pre-fix connector

  • reported the outcome to the edge as an internal error (UNKNOWN "local connection closed")
    instead of UNAVAILABLE, and
  • dialled the same session again on the next delivery.

Change

  • SessionDialGuard (new, bound @Singleton): a bounded per-session-id registry. tryAcceptDelivery
    claims an id once, abandon latches “the tunnel service no longer knows this session”. Retention 30 s
    (the edge's 2 × 3 s answer budget + the connector's 10 s connect timeout + the dial), max 8192 entries,
    oldest-first eviction. No clock-based heuristic.
  • WatcherRegister.onProposal claims the id before opening a LocalSession. A latched id is
    rejected with UNAVAILABLE and never dialled; a repeat delivery of an id that is already dialling is
    dropped, not rejected — a rejection would answer for the delivery that is still dialling.
  • Tunneler.tunnel(…) throws SessionAbandonedException instead of dialling a latched id, so the
    invariant is enforced at the dial site, not only at intake.
  • WebSocketTunnelTransport maps the tunnel service's 404 (pendingState=released) and 410 to the
    new TunnelConn.Handler.onSessionAbandoned — no onError, no retry. The Netty local channel then
    rejects and closes instead of dialling, and the player-facing rejection is UNAVAILABLE.
  • CommonModule binds the guard so register and tunneler see the same latch.

Scope and limits

  • This is the connector half. The connector cannot know a session was abandoned before it dials; that
    needs an edge-side bounded “session abandoned” notice (or 410 + X-Connect-Session-State), tracked
    separately. The first dial of a released entry is still issued, so this change alone does not eliminate
    the released + pendingAge > 3 s edge log class.
  • The libp2p path builds its own Tunneler per offer and keeps no cross-offer abandonment memory.
    Production runs the WebSocket path; flagged, not changed.
  • isSessionAbandoned keys on any 404/410 without parsing the body, so the tunnel service's other
    404 causes (expired entry, duplicate tunnel, wrong instance) also latch the id for 30 s. All of those
    already ended the session pre-fix, so no working path is lost.
  • The in-flight claim is not released on rejection, so a re-proposal of the same id inside the 30 s
    window is dropped silently (intended: one session id, one local session, one tunnel).
  • Session ids are single use and the map is capped, so retention cannot grow in a long-running proxy.

Verification

Item Result
:core:test 394 tests, 0 failures, 0 skipped
TunnelDialAfterAbandonTest RED on pristine 5f2eb27 with only the test file: 3/3 failed (0 vs 1 dial, 1 vs 2 dials, code 2 UNKNOWN vs 14 UNAVAILABLE) → GREEN 3/3 at this head
SessionDialGuardTest 7/7
./gradlew build BUILD SUCCESSFUL on Temurin 17.0.20.1 (73 tasks) and Temurin 21.0.12 (79 then 73 tasks), checkstyle/check included
Review independently reproduced in copies with no shared Git state: patch applies to pristine base and yields a byte-identical tree; RED/GREEN recomputed from the JUnit XML; premise re-read in the tunnel service (404 is emitted only on the missing-pending-entry path); SessionProposal.reject is a one-shot CAS, so UNAVAILABLE cannot be overwritten by the later close reject and no live tunnel can be rejected

The two new test classes run inside ./gradlew build, so the JDK 17 + 21 matrix exercises them.

Follow-ups (not in this diff)

  1. Edge-side bounded “session abandoned” signal so the connector can also skip the first dial of a
    released entry.
  2. AGENTS.md invariant section — docs-only, proposed for a maintainer to apply:
## Tunnel dials are per session id (paste-ready)
## Tunnel dials are per session id

- One session id carries one local session and one tunnel. WatcherRegister.WatcherImpl.onProposal claims the id
  in SessionDialGuard before opening a LocalSession; a repeat delivery (edge re-proposal after
  proposalAnswerTimeout, watch fail-over re-publish) is dropped, not rejected - a rejection would answer for the
  delivery that is still dialling.
- A session the tunnel service already gave up on is never dialled again. WebSocketTunnelTransport maps the
  tunnel service's "404 session <id> not found ... pendingState=released" (and 410) to
  TunnelConn.Handler.onSessionAbandoned, which latches the id, rejects the proposal with UNAVAILABLE (never
  INTERNAL/UNKNOWN) and tears the local session down; Tunneler.tunnel(...) throws SessionAbandonedException
  instead of dialling a latched id. Every transport needs that mapping.
- Do not replace this with a connector-side clock ("don't dial after N seconds"): the edge does not re-arm a
  released pending entry, so the only correct pre-dial signal is the edge's own answer.
- Guarded by core/.../register/TunnelDialAfterAbandonTest (RED before the fix with two dials and an UNKNOWN
  rejection) and core/.../tunnel/SessionDialGuardTest.

Do not call production fixed from this release alone: the released jar has to be in the hub image, the hub
pod has to log the plugin version, and the edge has to accept a tunnel with that same connectorVersion.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 138d406e-e979-46a4-b731-ed9d38b126d3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

minekube-ai-engineer Bot and others added 2 commits November 24, 2026 23:20
One session id must carry one local session and one tunnel. WatcherRegister now
claims the id in a bounded SessionDialGuard before it opens the LocalSession, so
a repeat delivery of the same id (the edge re-proposes it after
proposalAnswerTimeout, a watch fail-over re-publishes it) is absorbed instead of
dialled a second time, and the dial already in flight still decides the session.

Also classify the tunnel service's "session <id> not found ... pendingState=
released" answer (and 410) as an abandonment instead of a transport error:
WebSocketTunnelTransport reports it through TunnelConn.Handler.onSessionAbandoned,
LocalChannelInboundHandler latches the id, rejects the proposal with UNAVAILABLE
instead of UNKNOWN "local connection closed" and tears the local session down,
and Tunneler throws SessionAbandonedException instead of dialling a latched id.

RED before the fix (core:...register.TunnelDialAfterAbandonTest): two dials for
one session id, a dial for an abandoned session, and rejection code 2 UNKNOWN
instead of 14 UNAVAILABLE. GREEN after, with SessionDialGuardTest pinning the
bounded retention/eviction of the latch.
…iled claims

Dedupe repeat deliveries in WatchClient before the admission coordinator
stages a new admission, so a dropped repeat can no longer supersede the
first delivery's admission. Release the in-flight claim when the delivery
is rejected, latch abandonment only on 410 or a 404 reporting a released
pending entry, and keep the guard's pruning O(expired).
@robinbraemer
robinbraemer force-pushed the fix/tunnel-abandoned-session-dial branch from e2ce961 to 345c388 Compare November 24, 2026 22:24
…sed' 404, not its static causes text

The edge's 404 body always ends with 'likely causes: expired session, released session (...)', so matching 'not found' plus 'released' latched every tunnel-service 404, including wrong-instance and expired ones. Match the authoritative 'pending metadata released' field of moxy's missing-session response and use that exact body in the fixtures.
@robinbraemer
robinbraemer marked this pull request as ready for review November 24, 2026 22:33
@robinbraemer
robinbraemer merged commit b7c236c into main Nov 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant