nats-io/nats-server · error

fetching jwt failed due to shutdown

Error message

fetching jwt failed due to shutdown

What it means

Returned by Connz (monitoring endpoint) when the ConnzOptions.Sort field is set to a string that is not a valid SortOpt (e.g. not cid, start, stop, reason, subs, pending, etc.). The endpoint rejects the request before building the connection list. This affects both the Go API (server.Connz(opts)) and the HTTP monitoring /connz endpoint.

Source

Thrown at server/accounts.go:4771

			return
		}
		// Use the first valid response if there is still interest or
		// one of the empty responses to signal that it was not found.
		if _, ok := replies[replySubj]; ok {
			select {
			case respC <- clone:
			default:
			}
		}
	}
	s.sendInternalMsg(accountLookupRequest, replySubj, nil, []byte{})
	quit := s.quitCh
	s.mu.Unlock()
	var err error
	var theJWT string
	select {
	case <-quit:
		err = errors.New("fetching jwt failed due to shutdown")
	case <-time.After(timeout):
		err = errors.New("fetching jwt timed out")
	case m := <-respC:
		if len(m) == 0 {
			err = errors.New("account jwt not found")
		} else if err = res.Store(name, string(m)); err == nil {
			theJWT = string(m)
		}
	}
	s.mu.Lock()
	delete(replies, replySubj)
	s.mu.Unlock()
	close(respC)
	return theJWT, err
}

func NewCacheDirAccResolver(path string, limit int64, ttl time.Duration, opts ...DirResOption) (*CacheDirAccResolver, error) {
	if limit <= 0 {

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Use a valid sort option from the SortOpt set (cid, start, idle, stop, reason, etc.)
  2. Validate user-supplied sort values against server.AllSortOptions before assigning
  3. Handle the returned error and surface a 400-level message to the dashboard user

Example fix

// before
connz, err := srv.Connz(&ConnzOptions{Sort: "uptime"})
// after
sortOpt := SortBy("uptime")
if !sortOpt.IsValid() {
    sortOpt = ByCid
}
connz, err := srv.Connz(&ConnzOptions{Sort: sortOpt})
Defensive patterns

Strategy: validation

Validate before calling

func validSortOpt(opt string) bool {
    for _, o := range server.AllSortOptions {
        if string(o) == opt { return true }
    }
    return false
}
var opts *server.ConnzOptions
if !validSortOpt(sortParam) {
    return http.StatusBadRequest
}
opts = &server.ConnzOptions{Sort: server.SortOpt(sortParam)}
connz, err := srv.Connz(opts)

Try / catch

connz, err := srv.Connz(&ConnzOptions{Sort: sortOpt})
if err != nil && strings.Contains(err.Error(), "invalid sorting option") {
    // fall back to default ByCid sort
    connz, err = srv.Connz(nil)
}

Prevention

When it happens

Trigger: Calling Connz with opts.Sort = "foo"; HTTP GET /connz?sort=invalid_option; typo'd sort field like "uptime" (deprecated alias not valid anymore) in tests or dashboards.

Common situations: Monitoring dashboards with hardcoded sort options after a server upgrade renamed/removed options; user-supplied query params passed straight into ConnzOptions.

Related errors


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