mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

Client State Is Not Discarded After Session Present 0

Open LiD0209 opened this issue 7 months ago • 1 comments

Client State Is Not Discarded After Session Present 0

Conclusion

When libmosquitto reconnects and receives a successful CONNACK with Session Present = 0, it can still retransmit old unacknowledged QoS state instead of discarding local Session State.

This is the same concrete client-side MQTT 5 receive-path issue already proven earlier and reused here because it directly governs items 573 and 577.

MQTT Standard Requirement

The rule is:

If the Client does have Session State and receives Session Present set
to 0 it MUST discard its Session State if it continues with the
Network Connection.

That means:

  • the client must inspect the successful CONNACK
  • if the server says there is no resumed session, the client must clear old local state
  • this must happen before the connection continues normal use

Relevant Code

libmosquitto Checks A Related But Narrower Invariant

lib/handle_connack.c rejects Session Present=1 only in the special case where Clean Start=1:

if((mosq->protocol == mosq_p_mqtt311 || mosq->protocol == mosq_p_mqtt5) && (connect_flags & 0xFE)){
	...
	return MOSQ_ERR_PROTOCOL;
}
if(mosq->clean_start && connect_flags){
	log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK with session present when clean start was set", mosq->id);
	return MOSQ_ERR_PROTOCOL;
}

But there is no corresponding branch for:

client has local Session State
and CONNACK says Session Present = 0

Successful CONNACK Still Triggers The Retry Path

After property handling, the client resets quota and marks the connection active:

mosq->msgs_out.inflight_quota = mosq->msgs_out.inflight_maximum;
message__reconnect_reset(mosq, true);
...
case 0:
	...
	message__retry_check(mosq);
	return MOSQ_ERR_SUCCESS;

So the exact path after successful CONNACK is:

CONNACK success
-> message__reconnect_reset(mosq, true)
-> connection becomes active
-> message__retry_check(mosq)

There is no discard step conditioned on Session Present = 0.

That means:

  • old local QoS state survives reconnect preparation
  • the retry engine can still reuse it
  • the mandatory discard rule is missing from the success path

Runtime Evidence

Reused artifact:

test-mosquitto/v500/001-050/repro_connack_sp0_client_state

Observed result from the reused reproducer:

first:  command=0x32  # QoS1 PUBLISH, DUP=0
second: command=0x3a  # QoS1 PUBLISH, DUP=1
RESULT: reproduced - client resent old PUBLISH after CONNACK Session Present=0

Test idea:

1. Client sends QoS1 PUBLISH.
2. Server closes before PUBACK.
3. Client reconnects.
4. Server sends successful CONNACK with Session Present = 0.
5. Observe whether the client discards or retransmits the old message.

That means:

  • the client still had local session state from the earlier unfinished QoS flow
  • the server explicitly said no session was resumed
  • libmosquitto still retransmitted the old PUBLISH with DUP=1
  • the required discard step therefore did not happen

Why This Is Partial

Implemented:

  • broker-side Session Present generation is broadly correct
  • libmosquitto does reject one related inconsistency (Clean Start=1 with Session Present=1)

Missing:

  • libmosquitto does not discard local Session State after successful CONNACK with Session Present=0

So this is a concrete receive-side compliance gap, not a total absence of Session Present handling.

LiD0209 avatar May 03 '26 08:05 LiD0209

I have signed the contributor agreement, I would like to work on this and submit a PR. Can you assign it to me?

LiD0209 avatar May 21 '26 02:05 LiD0209