Skip to content

Commit 6ba7534

Browse files
committed
set and check thread id on publish acquisition
1 parent 0a1eceb commit 6ba7534

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public class PublishReturn {
2323
*/
2424
private long controlId;
2525

26+
/**
27+
* The threadID of the {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback.
28+
* This is set at the beginning of the callback and then used to verify
29+
* {@code acquirePublishAcknowledgementControl()} is being called from within.
30+
*/
31+
private long threadID;
32+
2633
/**
2734
* Set to true when {@code acquirePublishAcknowledgementControl()} is called, indicating that
2835
* the user has taken manual control of the publish acknowledgement. Native code reads this
@@ -60,7 +67,7 @@ public PublishPacket getPublishPacket() {
6067
* or called on a QoS 0 message.
6168
*/
6269
public synchronized Mqtt5PublishAcknowledgementControlHandle acquirePublishAcknowledgementControl() {
63-
if (controlId == 0) {
70+
if (controlId == 0 || threadID != Thread.currentThread().getId()) {
6471
throw new IllegalStateException(
6572
"acquirePublishAcknowledgementControl() must be called within the onMessageReceived callback and may only be called once.");
6673
}
@@ -110,5 +117,6 @@ private PublishReturn(PublishPacket newPublishPacket, long controlId) {
110117
this.publishPacket = newPublishPacket;
111118
this.controlId = controlId;
112119
this.controlAcquired = false;
120+
this.threadID = Thread.currentThread().getId();
113121
}
114122
}

0 commit comments

Comments
 (0)