nats-io/nats-server · error

sort by stop only valid on closed connections

Error message

sort by stop only valid on closed connections

What it means

Returned by Connz when the sort option is ByStop but the requested connection state filter is not ConnClosed. Stop timestamps only exist for closed connections, so sorting open connections by stop time is meaningless and rejected.

Source

Thrown at server/monitor.go:252

		acc = opts.Account
		mqttCID = opts.MQTTClient

		subs = opts.Subscriptions
		subsDet = opts.SubscriptionsDetail
		offset = opts.Offset
		if offset < 0 {
			offset = 0
		}
		limit = opts.Limit
		if limit <= 0 {
			limit = DefaultConnListSize
		}
		// state
		state = opts.State

		// ByStop only makes sense on closed connections
		if sortOpt == ByStop && state != ConnClosed {
			return nil, fmt.Errorf("sort by stop only valid on closed connections")
		}
		// ByReason is the same.
		if sortOpt == ByReason && state != ConnClosed {
			return nil, fmt.Errorf("sort by reason only valid on closed connections")
		}
		// If searching by CID
		if opts.CID > 0 {
			cid = opts.CID
			limit = 1
		}
		// If filtering by subject.
		if opts.FilterSubject != _EMPTY_ && opts.FilterSubject != fwcs {
			if acc == _EMPTY_ {
				return nil, fmt.Errorf("filter by subject only valid with account filtering")
			}
			filter = opts.FilterSubject
		}
	}

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Add state=closed (or omit state so it defaults appropriately) when sorting by stop
  2. Choose a sort field valid for open connections, such as cid or uptime
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at server/monitor.go:252 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/68441a8c26bed3cc. Report an issue: GitHub.