nats-io/nats-server · error

JS API timeout must be a positive value

Error message

JS API timeout must be a positive value

What it means

Options.MQTT.JSAPITimeout bounds how long the server waits for JetStream API responses (publish/consumer requests) while serving MQTT clients. Negative durations fail option validation at server/mqtt.go:728 before startup. Zero is accepted and falls back to the default timeout.

Source

Thrown at server/mqtt.go:233

var (
	mqttPingResponse     = []byte{mqttPacketPingResp, 0x0}
	mqttProtoName        = []byte("MQTT")
	mqttOldProtoName     = []byte("MQIsdp")
	mqttSessJailDur      = mqttSessFlappingJailDur
	mqttFlapCleanItvl    = mqttSessFlappingCleanupInterval
	mqttRetainedCacheTTL = mqttDefaultRetainedCacheTTL
)

var (
	errMQTTNotWebsocketPort           = errors.New("MQTT clients over websocket must connect to the Websocket port, not the MQTT port")
	errMQTTTopicFilterCannotBeEmpty   = errors.New("topic filter cannot be empty")
	errMQTTMalformedVarInt            = errors.New("malformed variable int")
	errMQTTSecondConnectPacket        = errors.New("received a second CONNECT packet")
	errMQTTServerNameMustBeSet        = errors.New("mqtt requires server name to be explicitly set")
	errMQTTUserMixWithUsersNKeys      = errors.New("mqtt authentication username not compatible with presence of users/nkeys")
	errMQTTTokenMixWIthUsersNKeys     = errors.New("mqtt authentication token not compatible with presence of users/nkeys")
	errMQTTAckWaitMustBePositive      = errors.New("ack wait must be a positive value")
	errMQTTJSAPITimeoutMustBePositive = errors.New("JS API timeout must be a positive value")
	errMQTTStandaloneNeedsJetStream   = errors.New("mqtt requires JetStream to be enabled if running in standalone mode")
	errMQTTConnFlagReserved           = errors.New("connect flags reserved bit not set to 0")
	errMQTTWillAndRetainFlag          = errors.New("if Will flag is set to 0, Will Retain flag must be 0 too")
	errMQTTPasswordFlagAndNoUser      = errors.New("password flag set but username flag is not")
	errMQTTCIDEmptyNeedsCleanFlag     = errors.New("when client ID is empty, clean session flag must be set to 1")
	errMQTTEmptyWillTopic             = errors.New("empty Will topic not allowed")
	errMQTTEmptyUsername              = errors.New("empty user name not allowed")
	errMQTTTopicIsEmpty               = errors.New("topic cannot be empty")
	errMQTTPacketIdentifierIsZero     = errors.New("packet identifier cannot be 0")
	errMQTTUnsupportedCharacters      = errors.New("character not supported for MQTT topics")
	errMQTTInvalidSession             = errors.New("invalid MQTT session")
	errMQTTInvalidRetainFlags         = errors.New("invalid retained message flags")
	errMQTTInvalidRetainedMessage     = errors.New("invalid retained message")
	errMQTTSessionCollision           = errors.New("stored session does not match client ID")
	errMQTTInvalidPublishLength       = errors.New("invalid publish message, variable header exceeds remaining length")
	errMQTTAckPipelineStopped         = errors.New("QoS1 PUBACK pipeline has shut down while admitting a message, " +
		"abandoning the wait for its JetStream ack; failing the connection, " +
		"the client will re-send unacknowledged PUBLISH packets on reconnect")

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Set MQTT.JSAPITimeout to a positive duration (e.g. 5*time.Second) or zero for the default.
  2. Clamp dynamic computations before passing Options to NewServer.
  3. Review config files for negative duration strings and correct them.

Example fix

// before
mo.JSAPITimeout = -10 * time.Second
// after
mo.JSAPITimeout = 5 * time.Second
Defensive patterns

Strategy: validation

Validate before calling

if mo.JSAPITimeout < 0 {
    return fmt.Errorf("JSAPITimeout must be >= 0, got %v", mo.JSAPITimeout)
}

Try / catch

if err := opts.Process(); err != nil {
    if strings.Contains(err.Error(), "JS API timeout must be") {
        opts.MQTT.JSAPITimeout = 0 // revert to default
    }
}

Prevention

When it happens

Trigger: Assigning opts.MQTT.JSAPITimeout a negative duration such as -10*time.Second and starting the server; the same validation block as AckWait in ProcessOptions.

Common situations: Same class of mistakes as AckWait: sign typos in literals, arithmetic on clocks yielding negative durations, or misreading '-1 as default' conventions from other frameworks.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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