t8y2/dbx · error · java.lang.IllegalStateException

Agent session already exists: <sessionId>

Error message

Agent session already exists: <sessionId>

What it means

openSession registers a new Session with putIfAbsent and, if the sessionId already exists, closes the freshly created client and throws IllegalStateException("Agent session already exists: <sessionId>"). Session ids are unique keys, so duplicates are rejected to avoid clobbering a live connection.

Source

Thrown at agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java:2020

                response.add("result", GSON.toJsonTree(result));
            } catch (Exception error) {
                JsonObject rpcError = new JsonObject();
                rpcError.addProperty("code", -1);
                rpcError.addProperty("message", error.getMessage() == null ? "Unknown error" : error.getMessage());
                response.add("error", rpcError);
            }
            return GSON.toJson(response);
        }

        private Object openSession(String sessionId, JsonObject params) {
            if (sessions.size() >= MAX_SESSIONS && !sessions.containsKey(sessionId)) {
                throw new IllegalStateException("Agent session limit reached: " + MAX_SESSIONS);
            }
            Session created = new Session(openClient(params));
            Session existing = sessions.putIfAbsent(sessionId, created);
            if (existing != null) {
                created.close();
                throw new IllegalStateException("Agent session already exists: " + sessionId);
            }
            return Collections.singletonMap("ok", true);
        }

        private Object closeSession(String sessionId) {
            Session removed = sessions.remove(sessionId);
            if (removed != null) {
                removed.close();
            }
            return Collections.singletonMap("ok", true);
        }

        private Session session(String sessionId) {
            Session session = sessions.get(sessionId);
            if (session == null) {
                throw new IllegalStateException("Agent session not found: " + sessionId);
            }
            return session;

View on GitHub (pinned to c0390bff16)

Solutions

  1. Check session existence (or attempt closeSession) before openSession with that id.
  2. Generate unique session ids (UUID) per logical connection.
  3. Treat this error as 'session already ready' and reuse the existing session instead of failing.
  4. Make init scripts idempotent: open only if the session is not found.

Example fix

// before
openSession("main", params); // called on every startup
// after
try { openSession("main", params); }
catch (IllegalStateException e) { if (!e.getMessage().contains("already exists")) throw e; }
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: make opening idempotent client-side
if (!openedSessions.contains(sessionId)) {
    agent.openSession(sessionId, params);
    openedSessions.add(sessionId);
}

Try / catch

try { agent.openSession(sessionId, params); }
catch (IllegalStateException e) {
  if (e.getMessage().startsWith("Agent session already exists")) {
    // session is already live; proceed using it
  } else throw e;
}

Prevention

When it happens

Trigger: Calling openSession twice with the same sessionId without closing it first; retrying a request after a timeout when the first attempt actually succeeded; two workers racing to open the same named session.

Common situations: Idempotency mistakes in retry logic; config files assigning the same static session id to multiple workers; re-running an init script that opens sessions unconditionally.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/c0cc7b692d43bd07. Report an issue: GitHub.