{"record":{"id":"a71f3a37abf5d8a9","repo":"nats-io/nats-server","slug":"empty-will-topic-not-allowed","errorCode":null,"errorMessage":"empty Will topic not allowed","messagePattern":"empty Will topic not allowed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/mqtt.go","lineNumber":239,"sourceCode":"\tmqttRetainedCacheTTL = mqttDefaultRetainedCacheTTL\n)\n\nvar (\n\terrMQTTNotWebsocketPort           = errors.New(\"MQTT clients over websocket must connect to the Websocket port, not the MQTT port\")\n\terrMQTTTopicFilterCannotBeEmpty   = errors.New(\"topic filter cannot be empty\")\n\terrMQTTMalformedVarInt            = errors.New(\"malformed variable int\")\n\terrMQTTSecondConnectPacket        = errors.New(\"received a second CONNECT packet\")\n\terrMQTTServerNameMustBeSet        = errors.New(\"mqtt requires server name to be explicitly set\")\n\terrMQTTUserMixWithUsersNKeys      = errors.New(\"mqtt authentication username not compatible with presence of users/nkeys\")\n\terrMQTTTokenMixWIthUsersNKeys     = errors.New(\"mqtt authentication token not compatible with presence of users/nkeys\")\n\terrMQTTAckWaitMustBePositive      = errors.New(\"ack wait must be a positive value\")\n\terrMQTTJSAPITimeoutMustBePositive = errors.New(\"JS API timeout must be a positive value\")\n\terrMQTTStandaloneNeedsJetStream   = errors.New(\"mqtt requires JetStream to be enabled if running in standalone mode\")\n\terrMQTTConnFlagReserved           = errors.New(\"connect flags reserved bit not set to 0\")\n\terrMQTTWillAndRetainFlag          = errors.New(\"if Will flag is set to 0, Will Retain flag must be 0 too\")\n\terrMQTTPasswordFlagAndNoUser      = errors.New(\"password flag set but username flag is not\")\n\terrMQTTCIDEmptyNeedsCleanFlag     = errors.New(\"when client ID is empty, clean session flag must be set to 1\")\n\terrMQTTEmptyWillTopic             = errors.New(\"empty Will topic not allowed\")\n\terrMQTTEmptyUsername              = errors.New(\"empty user name not allowed\")\n\terrMQTTTopicIsEmpty               = errors.New(\"topic cannot be empty\")\n\terrMQTTPacketIdentifierIsZero     = errors.New(\"packet identifier cannot be 0\")\n\terrMQTTUnsupportedCharacters      = errors.New(\"character not supported for MQTT topics\")\n\terrMQTTInvalidSession             = errors.New(\"invalid MQTT session\")\n\terrMQTTInvalidRetainFlags         = errors.New(\"invalid retained message flags\")\n\terrMQTTInvalidRetainedMessage     = errors.New(\"invalid retained message\")\n\terrMQTTSessionCollision           = errors.New(\"stored session does not match client ID\")\n\terrMQTTInvalidPublishLength       = errors.New(\"invalid publish message, variable header exceeds remaining length\")\n\terrMQTTAckPipelineStopped         = errors.New(\"QoS1 PUBACK pipeline has shut down while admitting a message, \" +\n\t\t\"abandoning the wait for its JetStream ack; failing the connection, \" +\n\t\t\"the client will re-send unacknowledged PUBLISH packets on reconnect\")\n)\n\ntype srvMQTT struct {\n\tlistener     net.Listener\n\tlistenerErr  error\n\tauthOverride bool","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/mqtt.go#L221-L257","documentation":"If a CONNECT packet declares a Will (Will flag set), the Will topic must be present and non-empty. When parsing the will payload, a zero-length topic returns this error (server/mqtt.go:3908); the topic is then further validated for UTF-8 and wildcard characters.","triggerScenarios":"A CONNECT packet with mqttConnFlagWillFlag set but a zero-length Will topic string in the payload (test case: ...,0,0 after keep-alive), truncation of the packet before the topic, or a topic of only whitespace-free zero bytes.","commonSituations":"Hand-built CONNECT packets omitting the will topic, client library bugs where an empty will topic is passed through, or packet truncation by intermediaries.","solutions":["Configure the client with a valid, non-empty Will topic before connecting.","If no will message is needed, clear the Will flag so no will payload is parsed.","Inspect the CONNECT bytes for truncation; ensure length prefixes are correct."],"exampleFix":"// before\nopts.SetWill(\"\", []byte(\"offline\"), 1, false)\n// after\nopts.SetWill(\"clients/client-1/status\", []byte(\"offline\"), 1, false)","handlingStrategy":"validation","validationCode":"// client side, when configuring a will\nif willTopic == \"\" && willEnabled {\n    return fmt.Errorf(\"will topic must be non-empty when the will flag is set\")\n}","typeGuard":null,"tryCatchPattern":"// client side: on 'empty Will topic' rejection, set a valid will topic or disable the will and reconnect","preventionTips":["Configure will topic/message together via one library call.","Validate will topics (non-empty, no wildcards, valid UTF-8) at config load.","Disable the will flag entirely when no will is needed."],"tags":["mqtt","protocol","will","client-error"],"backgroundTag":"malformed-packet","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}