-
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
Conversation
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
Outdated
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.
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
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #25104 +/- ##
============================================
+ Coverage 74.42% 74.44% +0.01%
+ Complexity 34053 33655 -398
============================================
Files 1899 1899
Lines 149659 149663 +4
Branches 17394 17394
============================================
+ Hits 111389 111410 +21
+ Misses 29396 29381 -15
+ Partials 8874 8872 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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