[client] fix writeLac memory leak and thread safety issue#4713
[client] fix writeLac memory leak and thread safety issue#4713wenbingshen wants to merge 1 commit intoapache:masterfrom
Conversation
2490683 to
13bf105
Compare
|
|
||
| if (receivedResponseSet.isEmpty()){ | ||
| completed = true; | ||
| ReferenceCountUtil.release(toSend); |
There was a problem hiding this comment.
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);
}
}
There was a problem hiding this comment.
@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.
13bf105 to
38cdacb
Compare
38cdacb to
aeff4ec
Compare
aeff4ec to
fc199fc
Compare
| void initiate(ByteBufList toSend) { | ||
| this.toSend = toSend; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
Motivation
When I set
explicitLacIntervaland enable netty memory leak detection, I find thatPendingWriteLacOphas a memory leak, as shown below:Changes
When
PendingWriteLacOpcompletes, executeReferenceCountUtil.release(toSend)to releasetoSend.After applying this PR, netty detecter will no longer detect leaks during local verification.