Skip to content

[client] fix writeLac memory leak and thread safety issue#4713

Open
wenbingshen wants to merge 1 commit intoapache:masterfrom
wenbingshen:wenbing/fix_writeLac_memoryLeak
Open

[client] fix writeLac memory leak and thread safety issue#4713
wenbingshen wants to merge 1 commit intoapache:masterfrom
wenbingshen:wenbing/fix_writeLac_memoryLeak

Conversation

@wenbingshen
Copy link
Member

@wenbingshen wenbingshen commented Feb 13, 2026

Motivation

When I set explicitLacInterval and enable netty memory leak detection, I find that PendingWriteLacOp has a memory leak, as shown below:

Clipboard_Screenshot_1770968282

Changes

When PendingWriteLacOp completes, execute ReferenceCountUtil.release(toSend) to release toSend.

After applying this PR, netty detecter will no longer detect leaks during local verification.

@wenbingshen wenbingshen self-assigned this Feb 13, 2026
@wenbingshen wenbingshen force-pushed the wenbing/fix_writeLac_memoryLeak branch from 2490683 to 13bf105 Compare February 13, 2026 08:04
Copy link
Member

@horizonzy horizonzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.


if (receivedResponseSet.isEmpty()){
completed = true;
ReferenceCountUtil.release(toSend);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we may need to check completed, only completed is false, then trigger callback.

The codes should be

        if (receivedResponseSet.isEmpty()){
            ReferenceCountUtil.release(toSend);
            toSend = null;
            if (!completed) {
                completed = true;
                cb.addLacComplete(lastSeenError, lh, ctx);
            }
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@horizonzy After reviewing the implementation of PendingWriteLacOp, I found that the implementation of writeLacComplete did not meet the requirements of multi-threaded concurrency safety, so I revised the PR. PTAL.

@wenbingshen wenbingshen force-pushed the wenbing/fix_writeLac_memoryLeak branch from 13bf105 to 38cdacb Compare March 2, 2026 08:47
@wenbingshen wenbingshen requested a review from horizonzy March 2, 2026 08:50
@wenbingshen wenbingshen force-pushed the wenbing/fix_writeLac_memoryLeak branch from 38cdacb to aeff4ec Compare March 2, 2026 08:56
@wenbingshen wenbingshen force-pushed the wenbing/fix_writeLac_memoryLeak branch from aeff4ec to fc199fc Compare March 2, 2026 09:12
@lhotari lhotari changed the title [client] fix writeLac memory leak [client] fix writeLac memory leak and thread safety issue Mar 5, 2026
Comment on lines 81 to 82
void initiate(ByteBufList toSend) {
this.toSend = toSend;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering if initiate should also be made synchronized or toSend volatile. Since this method is only called once, it's most likely not necessary if the reference of PendingWriteLacOp is passed across threads using ways where happens-before is ensured (such as passing the reference via an Executor, ConcurrentHashMap or other solutions that already ensure happens-before).

if (rc == BKException.Code.OK) {
if (ackSet.completeBookieAndCheck(bookieIndex) && !completed) {
completed = true;
cb.addLacComplete(rc, lh, ctx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to execute callbacks outside of the synchronized block? Perhaps not in this case. In general it's a recommended pattern to avoid dead locks by limiting the scope of synchronization when possible so that outgoing calls don't end up causing dead locks by locking other resources. In this case there's probably not other threads that could hold those required locks while it calls this writeLacComplete method and would cause a deadlock.

@lhotari lhotari requested review from Copilot and horizonzy and removed request for horizonzy March 5, 2026 18:26
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants