Skip to content

Commit f3484fe

Browse files
committed
forgot to set the acquire puback to 0 for qos 0 publishes
1 parent f66b941 commit f3484fe

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

src/main/java/software/amazon/awssdk/crt/mqtt5/PublishReturn.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class PublishReturn {
1818
* Single-element long array holding the native manual PUBACK control context pointer.
1919
* Element [0] is the pointer value, valid only during the
2020
* {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback.
21+
* QoS 0 results in this being set to 0.
2122
* Native code sets [0] to 0 after the callback returns (via SetLongArrayRegion,
2223
* requiring no extra JNI method ID).
2324
*/

src/native/mqtt5_client.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,11 @@ static void s_aws_mqtt5_client_java_publish_received(
605605
context_ptr_holder = (*env)->NewLongArray(env, 1);
606606
/* If allocation failed, Java will throw IllegalStateException on acquirePubackControl(). */
607607
if (context_ptr_holder != NULL) {
608-
jlong context_ptr_value = (jlong)(uintptr_t)control_context;
608+
/*
609+
* Only expose the context pointer for QoS 1 publishes. For QoS 0, there is no PUBACK
610+
* to send, so acquirePubackControl() should throw IllegalStateException. We pass 0 (null).
611+
*/
612+
jlong context_ptr_value = (publish->qos == AWS_MQTT5_QOS_AT_MOST_ONCE) ? 0 : (jlong)(uintptr_t)control_context;
609613
/* Assigning the pointer to the allocated manual_puback_control_context to the single-element of the array */
610614
(*env)->SetLongArrayRegion(env, context_ptr_holder, 0, 1, &context_ptr_value);
611615
}

src/test/java/software/amazon/awssdk/crt/test/Mqtt5ClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,7 +2884,7 @@ private void doManualPuback_Qos0AcquireThrowsTest() {
28842884
@Override
28852885
public void onMessageReceived(Mqtt5Client client, PublishReturn publishReturn) {
28862886
// For QoS 0, acquirePubackControl() should throw IllegalStateException
2887-
// because there is no PUBACK context (nativeContextPtrHolder is null or 0)
2887+
// because the native layer passes a null context pointer for QoS 0 publishes.
28882888
try {
28892889
publishReturn.acquirePubackControl();
28902890
resultFuture.complete("no_error"); // Should not reach here
@@ -2904,7 +2904,7 @@ public void onMessageReceived(Mqtt5Client client, PublishReturn publishReturn) {
29042904
SubscribePacketBuilder subscribeBuilder = new SubscribePacketBuilder(testTopic, QOS.AT_LEAST_ONCE);
29052905
client.subscribe(subscribeBuilder.build()).get(OPERATION_TIMEOUT_TIME, TimeUnit.SECONDS);
29062906

2907-
// Publish at QoS 0, no PUBACK involved
2907+
// Publish at QoS 0, there is no PUBACK involved
29082908
PublishPacketBuilder publishBuilder = new PublishPacketBuilder(testTopic, QOS.AT_MOST_ONCE, payload);
29092909
client.publish(publishBuilder.build()).get(OPERATION_TIMEOUT_TIME, TimeUnit.SECONDS);
29102910

0 commit comments

Comments
 (0)