-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improve][broker] Fix thread safety issue in ManagedCursorImpl.removeProperty #25104
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
base: master
Are you sure you want to change the base?
Conversation
| case 0: // Put operation | ||
| Long randomValue = random.nextLong(); | ||
| cursor.putProperty(randomKey, randomValue); | ||
| records.put(randomKey, randomValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the 2 separate operations aren't atomic together so I don't see how the behavior could be validated this way.
It's possible to have a valid data race for example between the put and remove where the records becomes inconsistent even though there's no problem.
t1
cursor.putProperty(randomKey, randomValue);
records.put(randomKey, randomValue);
t2
cursor.removeProperty(randomKey);
records.remove(randomKey);
For example with the order of operations:
t1 - cursor.putProperty(randomKey, randomValue);
t2 - cursor.removeProperty(randomKey);
t2 - records.remove(randomKey);
t1 - records.put(randomKey, randomValue);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lhotari Thanks for the catch! You're right about the atomicity issue. I've removed the tracking map and now the test just checks for thread safety issues directly - no exceptions and no data corruption
lhotari
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the comment about the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a critical thread safety issue in the ManagedCursorImpl.removeProperty method where direct modification of a shared Map could lead to ConcurrentModificationException and data corruption. The fix aligns the implementation with the existing thread-safe pattern used in putProperty by creating a new Map copy instead of modifying the original.
Key Changes:
- Modified
removePropertyto create a new HashMap copy and return a newMarkDeleteEntryinstance, ensuring thread-safe updates through the atomic field updater - Added a concurrent stress test with 100 threads performing mixed put/remove/get operations to validate the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java | Fixed removeProperty method to use copy-on-write pattern instead of direct Map modification |
| managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java | Added concurrency test testConcurrentPropertyOperationsThreadSafety with new imports for ConcurrentModificationException and Random |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
Outdated
Show resolved
Hide resolved
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
Outdated
Show resolved
Hide resolved
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
Show resolved
Hide resolved
lhotari
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes #25103
Motivation
The
ManagedCursorImpl.removePropertymethod contains a thread safety vulnerability where it directly modifies the sharedpropertiesMap without proper synchronization. This can lead to:putPropertycreates a new HashMap copy whileremovePropertymodifies the original MapModifications
Fixed
removePropertymethod inManagedCursorImpl.java:properties.remove(key)) to creating a new Map copyputPropertyLAST_MARK_DELETE_ENTRY_UPDATER.updateAndGet()Added concurrency test in
ManagedCursorTest.java:ConcurrentModificationExceptionor data inconsistenciesVerifying this change
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: 3pacccccc#37