Prevent double release in TcpTransport if send listener throws an exception (#20880)
today we might release a bytes array more than once if the send listener throws an exception but already has released the array. Yet, this is already fixed in the BytesArray class we use in production to ensure 3rd party users don't release twice but our mocks still enforce it.
This commit is contained in:
parent
608c7eb9fa
commit
f96ea58db8
|
@ -24,6 +24,7 @@ import org.apache.lucene.util.IOUtils;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/** Utility methods to work with {@link Releasable}s. */
|
/** Utility methods to work with {@link Releasable}s. */
|
||||||
public enum Releasables {
|
public enum Releasables {
|
||||||
|
@ -93,4 +94,16 @@ public enum Releasables {
|
||||||
public static Releasable wrap(final Releasable... releasables) {
|
public static Releasable wrap(final Releasable... releasables) {
|
||||||
return () -> close(releasables);
|
return () -> close(releasables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to {@link #wrap(Releasable...)} but can be called multiple times without double releasing.
|
||||||
|
*/
|
||||||
|
public static Releasable releaseOnce(final Releasable... releasables) {
|
||||||
|
final AtomicBoolean released = new AtomicBoolean(false);
|
||||||
|
return () -> {
|
||||||
|
if (released.compareAndSet(false, true)) {
|
||||||
|
close(releasables);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -924,6 +924,9 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
byte status = 0;
|
byte status = 0;
|
||||||
status = TransportStatus.setRequest(status);
|
status = TransportStatus.setRequest(status);
|
||||||
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
|
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
|
||||||
|
// we wrap this in a release once since if the onRequestSent callback throws an exception
|
||||||
|
// we might release things twice and this should be prevented
|
||||||
|
final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
|
||||||
boolean addedReleaseListener = false;
|
boolean addedReleaseListener = false;
|
||||||
StreamOutput stream = bStream;
|
StreamOutput stream = bStream;
|
||||||
try {
|
try {
|
||||||
|
@ -944,9 +947,9 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
stream.writeString(action);
|
stream.writeString(action);
|
||||||
BytesReference message = buildMessage(requestId, status, node.getVersion(), request, stream, bStream);
|
BytesReference message = buildMessage(requestId, status, node.getVersion(), request, stream, bStream);
|
||||||
final TransportRequestOptions finalOptions = options;
|
final TransportRequestOptions finalOptions = options;
|
||||||
Runnable onRequestSent = () -> {
|
Runnable onRequestSent = () -> { // this might be called in a different thread
|
||||||
try {
|
try {
|
||||||
Releasables.close(bStream.bytes());
|
toRelease.close();
|
||||||
} finally {
|
} finally {
|
||||||
transportServiceAdapter.onRequestSent(node, requestId, action, request, finalOptions);
|
transportServiceAdapter.onRequestSent(node, requestId, action, request, finalOptions);
|
||||||
}
|
}
|
||||||
|
@ -955,7 +958,7 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
} finally {
|
} finally {
|
||||||
IOUtils.close(stream);
|
IOUtils.close(stream);
|
||||||
if (!addedReleaseListener) {
|
if (!addedReleaseListener) {
|
||||||
Releasables.close(bStream.bytes());
|
toRelease.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1018,6 +1021,9 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
byte status = 0;
|
byte status = 0;
|
||||||
status = TransportStatus.setResponse(status); // TODO share some code with sendRequest
|
status = TransportStatus.setResponse(status); // TODO share some code with sendRequest
|
||||||
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
|
ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
|
||||||
|
// we wrap this in a release once since if the onRequestSent callback throws an exception
|
||||||
|
// we might release things twice and this should be prevented
|
||||||
|
final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
|
||||||
boolean addedReleaseListener = false;
|
boolean addedReleaseListener = false;
|
||||||
StreamOutput stream = bStream;
|
StreamOutput stream = bStream;
|
||||||
try {
|
try {
|
||||||
|
@ -1030,9 +1036,9 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
BytesReference reference = buildMessage(requestId, status, nodeVersion, response, stream, bStream);
|
BytesReference reference = buildMessage(requestId, status, nodeVersion, response, stream, bStream);
|
||||||
|
|
||||||
final TransportResponseOptions finalOptions = options;
|
final TransportResponseOptions finalOptions = options;
|
||||||
Runnable onRequestSent = () -> {
|
Runnable onRequestSent = () -> { // this might be called in a different thread
|
||||||
try {
|
try {
|
||||||
Releasables.close(bStream.bytes());
|
toRelease.close();
|
||||||
} finally {
|
} finally {
|
||||||
transportServiceAdapter.onResponseSent(requestId, action, response, finalOptions);
|
transportServiceAdapter.onResponseSent(requestId, action, response, finalOptions);
|
||||||
}
|
}
|
||||||
|
@ -1043,7 +1049,8 @@ public abstract class TcpTransport<Channel> extends AbstractLifecycleComponent i
|
||||||
IOUtils.close(stream);
|
IOUtils.close(stream);
|
||||||
} finally {
|
} finally {
|
||||||
if (!addedReleaseListener) {
|
if (!addedReleaseListener) {
|
||||||
Releasables.close(bStream.bytes());
|
|
||||||
|
toRelease.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch licenses this file to you under
|
||||||
|
* the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.elasticsearch.common;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.lease.Releasable;
|
||||||
|
import org.elasticsearch.common.lease.Releasables;
|
||||||
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class ReleasablesTests extends ESTestCase {
|
||||||
|
|
||||||
|
public void testReleaseOnce() {
|
||||||
|
AtomicInteger count = new AtomicInteger(0);
|
||||||
|
Releasable releasable = Releasables.releaseOnce(count::incrementAndGet, count::incrementAndGet);
|
||||||
|
assertEquals(0, count.get());
|
||||||
|
releasable.close();
|
||||||
|
assertEquals(2, count.get());
|
||||||
|
releasable.close();
|
||||||
|
assertEquals(2, count.get());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue