nats-io/nats-server · error

other session with client ID %q is in the process of connect

Error message

other session with client ID %q is in the process of connecting

What it means

MQTT session-takeover path: another session with the same client ID is mid-connect (present in the sessLocked map), so the new connection cannot evict it yet. The server retries briefly and, if the lock never clears, fails the new connection instead of the old one.

Source

Thrown at server/mqtt.go:4078

			// not wait for the CONNACK and sends other protocols, the
			// server would not have a fully setup client and may panic.
			asm.mu.Unlock()
			select {
			case <-s.quitCh:
			case <-time.After(mqttSessJailDur):
			}
			c.closeConnection(DuplicateClientID)
			return ErrConnectionClosed
		}
	}
	// If an existing session is in the process of processing some packet, we can't
	// evict the old client just yet. So try again to see if the state clears, but
	// if it does not, then we have no choice but to fail the new client instead of
	// the old one.
	if _, ok := asm.sessLocked[cid]; ok {
		asm.mu.Unlock()
		if locked++; locked == 10 {
			return fmt.Errorf("other session with client ID %q is in the process of connecting", cid)
		}
		time.Sleep(100 * time.Millisecond)
		goto CHECK
	}

	// Register this client ID the "locked" map for the duration if this function.
	asm.sessLocked[cid] = struct{}{}
	// And remove it on exit, regardless of error or not.
	defer func() {
		asm.mu.Lock()
		delete(asm.sessLocked, cid)
		asm.mu.Unlock()
	}()

	// Is the client requesting a clean session or not.
	cleanSess := cp.flags&mqttConnFlagCleanSession != 0
	// Session present? Assume false, will be set to true only when applicable.
	sessp := false

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Retry the connect after the competing connection finishes (or a short backoff)
  2. Use a unique client ID per connection
  3. Ensure the previous client disconnects cleanly before reconnecting with the same ID
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at server/mqtt.go:4078 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/44abd630461f4528. Report an issue: GitHub.