Skip to content

Commit f03c1d6

Browse files
committed
Don't allow acquire after the callback returns. Add more logging
1 parent d9e8a1e commit f03c1d6

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,13 @@ public interface PublishEvents {
347347
/**
348348
* Called when an MQTT PUBLISH packet is received by the client.
349349
*
350-
* <p>To take manual control of the PUBACK for a QoS 1 message, call
350+
* <p>To take manual control of the publish acknowledgement for a QoS 1 message, call
351351
* {@link PublishReturn#acquirePublishAcknowledgementControl()} within this callback. If you do so,
352-
* the client will NOT automatically send the PUBACK; you are responsible for calling
352+
* the client will NOT automatically send the publish acknowledgement. You are responsible for calling
353353
* {@link Mqtt5Client#invokePublishAcknowledgement(Mqtt5PublishAcknowledgementControlHandle)} later.</p>
354354
*
355-
* <p>If you do not call {@code acquirePublishAcknowledgementControl()}, the client will automatically
356-
* send the PUBACK after this callback returns.</p>
355+
* <p>If you do not call {@code acquirePublishAcknowledgementControl()} within the callback ,
356+
* the client will automatically send the publish acknowledgement after this callback returns.</p>
357357
*
358358
* @param client The client that has received the message
359359
* @param publishReturn All of the data that was received from the server

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ boolean wasControlAcquired() {
8989
return controlAcquired;
9090
}
9191

92+
/**
93+
* Called by native/JNI code after the {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived}
94+
* callback returns to prevent post-callback use of
95+
* {@link #acquirePublishAcknowledgementControl()}.
96+
*
97+
* <p>Zeroes out {@code controlId} so that any saved reference to this {@code PublishReturn}
98+
* cannot call {@code acquirePublishAcknowledgementControl()} after the callback has returned.</p>
99+
*/
100+
synchronized void invalidateAfterCallback() {
101+
controlId = 0;
102+
}
103+
92104
/**
93105
* This is only called in JNI to make a new PublishReturn with a PUBLISH packet.
94106
* The controlId is the already-acquired publish acknowledgement control ID (eagerly acquired

src/native/java_class_ids.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2256,13 +2256,20 @@ static void s_cache_mqtt5_publish_return(JNIEnv *env) {
22562256
"(Lsoftware/amazon/awssdk/crt/mqtt5/packets/PublishPacket;J)V");
22572257
AWS_FATAL_ASSERT(mqtt5_publish_return_properties.return_constructor_id);
22582258
/*
2259-
* wasControlAcquired() - called by native code after onMessageReceived returns to determine
2259+
* wasControlAcquired() called by native code after onMessageReceived returns to determine
22602260
* whether the user called acquirePublishAcknowledgementControl() during the callback.
22612261
* Returns true if the user took manual control of the PUBACK, false otherwise.
22622262
*/
22632263
mqtt5_publish_return_properties.return_was_control_acquired_id =
22642264
(*env)->GetMethodID(env, mqtt5_publish_return_properties.return_class, "wasControlAcquired", "()Z");
22652265
AWS_FATAL_ASSERT(mqtt5_publish_return_properties.return_was_control_acquired_id);
2266+
/*
2267+
* invalidateAfterCallback() called by native code after onMessageReceived returns to zero
2268+
* out controlId, preventing post-callback calls to acquirePublishAcknowledgementControl().
2269+
*/
2270+
mqtt5_publish_return_properties.return_invalidate_after_callback_id =
2271+
(*env)->GetMethodID(env, mqtt5_publish_return_properties.return_class, "invalidateAfterCallback", "()V");
2272+
AWS_FATAL_ASSERT(mqtt5_publish_return_properties.return_invalidate_after_callback_id);
22662273
}
22672274

22682275
struct java_aws_mqtt5_on_stopped_return_properties mqtt5_on_stopped_return_properties;

src/native/java_class_ids.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,9 @@ extern struct java_aws_mqtt5_publish_result_properties mqtt5_publish_result_prop
926926
/* mqtt5.PublishReturn */
927927
struct java_aws_mqtt5_publish_return_properties {
928928
jclass return_class;
929-
jmethodID return_constructor_id;
930-
jmethodID return_was_control_acquired_id;
929+
jmethodID return_constructor_id; /* (PublishPacket, long) */
930+
jmethodID return_was_control_acquired_id; /* ()Z true if user called acquirePublishAcknowledgementControl() */
931+
jmethodID return_invalidate_after_callback_id; /* ()V zeroes controlId to prevent post-callback use */
931932
};
932933
extern struct java_aws_mqtt5_publish_return_properties mqtt5_publish_return_properties;
933934

0 commit comments

Comments
 (0)