Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 841: Incorrect reference used in compareAndSet in CTrie.cleanTomb. #842

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 0.18-SNAPSHOT:
[fix] Incorrect reference used in compareAndSet in CTrie.cleanTomb. (#841)
[feature] Implement response-information property for request-response flow. (#840)
[fix] Optimised page file opening for disk-based queues. (#837)
[feature] Manage payload format indicator property, when set verify payload format. (#826)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public void add(INode newINode) {
}
}

public void remove(INode node) {
public INode remove(INode node) {
int idx = findIndexForToken(node.mainNode().token);
this.children.remove(idx);
return this.children.remove(idx);
}

private List<Subscription> sharedSubscriptions() {
Expand Down
19 changes: 12 additions & 7 deletions broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,7 @@ private Action remove(String clientId, Topic topic, INode inode, INode iParent,
}
}
if (cnode instanceof TNode) {
// this inode is a tomb, has no clients and should be cleaned up
// Because we implemented cleanTomb below, this should be rare, but possible
// Consider calling cleanTomb here too
return Action.OK;
return cleanTomb(inode, iParent);
}
if (cnode.containsOnly(clientId) && topic.isEmpty() && cnode.allChildren().isEmpty()) {
// last client to leave this node, AND there are no downstream children, remove via TNode tomb
Expand Down Expand Up @@ -393,9 +390,17 @@ private Action remove(String clientId, Topic topic, INode inode, INode iParent,
* @return REPEAT if this method wasn't successful or OK.
*/
private Action cleanTomb(INode inode, INode iParent) {
CNode updatedCnode = iParent.mainNode().copy();
updatedCnode.remove(inode);
return iParent.compareAndSet(iParent.mainNode(), updatedCnode) ? Action.OK : Action.REPEAT;
CNode origCnode = iParent.mainNode();
CNode updatedCnode = origCnode.copy();
INode removed = updatedCnode.remove(inode);
if (removed == inode) {
return iParent.compareAndSet(origCnode, updatedCnode) ? Action.OK : Action.REPEAT;
} else {
// The node removed (from the copy!) was not the node we expected to remove.
// Probably because another thread replaced the TNode with a live node, so
// we don't need to clean it and can return success.
return Action.OK;
}
}

public int size() {
Expand Down
Loading