use of io.netty.util.IllegalReferenceCountException in project netty by netty.
the class SslHandlerTest method newHandler.
private static ChannelHandler newHandler(final SslContext sslCtx, final Promise<Void> promise) {
return new ChannelInitializer() {
@Override
protected void initChannel(final Channel ch) {
final SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
sslHandler.setHandshakeTimeoutMillis(1000);
ch.pipeline().addFirst(sslHandler);
sslHandler.handshakeFuture().addListener(new FutureListener<Channel>() {
@Override
public void operationComplete(final Future<Channel> future) {
ch.pipeline().remove(sslHandler);
// Schedule the close so removal has time to propagate exception if any.
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
ch.close();
}
});
}
});
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof CodecException) {
cause = cause.getCause();
}
if (cause instanceof IllegalReferenceCountException) {
promise.setFailure(cause);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
promise.trySuccess(null);
}
});
}
};
}
use of io.netty.util.IllegalReferenceCountException in project netty by netty.
the class ReferenceCountUpdater method retryRelease0.
private boolean retryRelease0(T instance, int decrement) {
for (; ; ) {
int rawCnt = updater().get(instance), realCnt = toLiveRealRefCnt(rawCnt, decrement);
if (decrement == realCnt) {
if (tryFinalRelease0(instance, rawCnt)) {
return true;
}
} else if (decrement < realCnt) {
// all changes to the raw count are 2x the "real" change
if (updater().compareAndSet(instance, rawCnt, rawCnt - (decrement << 1))) {
return false;
}
} else {
throw new IllegalReferenceCountException(realCnt, -decrement);
}
// this benefits throughput under high contention
Thread.yield();
}
}
use of io.netty.util.IllegalReferenceCountException in project bookkeeper by apache.
the class BookKeeperTest method testReadEntryReleaseByteBufs.
@Test
public void testReadEntryReleaseByteBufs() throws Exception {
ClientConfiguration confWriter = new ClientConfiguration();
confWriter.setZkServers(zkUtil.getZooKeeperConnectString());
int numEntries = 10;
byte[] data = "foobar".getBytes();
long ledgerId;
try (BookKeeper bkc = new BookKeeper(confWriter)) {
try (LedgerHandle lh = bkc.createLedger(digestType, "testPasswd".getBytes())) {
ledgerId = lh.getId();
for (int i = 0; i < numEntries; i++) {
lh.addEntry(data);
}
}
}
// v2 protocol, using pooled buffers
ClientConfiguration confReader1 = new ClientConfiguration().setUseV2WireProtocol(true).setNettyUsePooledBuffers(true);
confReader1.setZkServers(zkUtil.getZooKeeperConnectString());
try (BookKeeper bkc = new BookKeeper(confReader1)) {
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
assertEquals(numEntries - 1, lh.readLastConfirmed());
for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
LedgerEntry entry = readEntries.nextElement();
try {
entry.data.release();
} catch (IllegalReferenceCountException ok) {
fail("ByteBuf already released");
}
}
}
}
// v2 protocol, not using pooled buffers
ClientConfiguration confReader2 = new ClientConfiguration().setUseV2WireProtocol(true).setNettyUsePooledBuffers(false);
confReader2.setZkServers(zkUtil.getZooKeeperConnectString());
try (BookKeeper bkc = new BookKeeper(confReader2)) {
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
assertEquals(numEntries - 1, lh.readLastConfirmed());
for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
LedgerEntry entry = readEntries.nextElement();
try {
entry.data.release();
} catch (IllegalReferenceCountException e) {
fail("ByteBuf already released");
}
}
}
}
// v3 protocol, not using pooled buffers
ClientConfiguration confReader3 = new ClientConfiguration().setUseV2WireProtocol(false).setNettyUsePooledBuffers(false);
confReader3.setZkServers(zkUtil.getZooKeeperConnectString());
try (BookKeeper bkc = new BookKeeper(confReader3)) {
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
assertEquals(numEntries - 1, lh.readLastConfirmed());
for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
LedgerEntry entry = readEntries.nextElement();
assertTrue("Can't release entry " + entry.getEntryId() + ": ref = " + entry.data.refCnt(), entry.data.release());
try {
assertFalse(entry.data.release());
fail("ByteBuf already released");
} catch (IllegalReferenceCountException ok) {
}
}
}
}
// v3 protocol, using pooled buffers
// v3 protocol from 4.5 always "wraps" buffers returned by protobuf
ClientConfiguration confReader4 = new ClientConfiguration().setUseV2WireProtocol(false).setNettyUsePooledBuffers(true);
confReader4.setZkServers(zkUtil.getZooKeeperConnectString());
try (BookKeeper bkc = new BookKeeper(confReader4)) {
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
assertEquals(numEntries - 1, lh.readLastConfirmed());
for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
LedgerEntry entry = readEntries.nextElement();
// ButeBufs not reference counter
assertTrue("Can't release entry " + entry.getEntryId() + ": ref = " + entry.data.refCnt(), entry.data.release());
try {
assertFalse(entry.data.release());
fail("ByteBuf already released");
} catch (IllegalReferenceCountException ok) {
}
}
}
}
// cannot read twice an entry
ClientConfiguration confReader5 = new ClientConfiguration();
confReader5.setZkServers(zkUtil.getZooKeeperConnectString());
try (BookKeeper bkc = new BookKeeper(confReader5)) {
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
assertEquals(numEntries - 1, lh.readLastConfirmed());
for (Enumeration<LedgerEntry> readEntries = lh.readEntries(0, numEntries - 1); readEntries.hasMoreElements(); ) {
LedgerEntry entry = readEntries.nextElement();
entry.getEntry();
try {
entry.getEntry();
fail("entry data accessed twice");
} catch (IllegalStateException ok) {
}
try {
entry.getEntryInputStream();
fail("entry data accessed twice");
} catch (IllegalStateException ok) {
}
}
}
}
}
use of io.netty.util.IllegalReferenceCountException in project flink by apache.
the class AbstractByteBufTest method assertDuplicateFailAfterRelease.
private static void assertDuplicateFailAfterRelease(ByteBuf... bufs) {
for (ByteBuf buf : bufs) {
if (buf.refCnt() > 0) {
buf.release();
}
}
for (ByteBuf buf : bufs) {
try {
assertEquals(0, buf.refCnt());
buf.duplicate();
fail();
} catch (IllegalReferenceCountException ignored) {
// as expected
}
}
}
use of io.netty.util.IllegalReferenceCountException in project flink by apache.
the class AbstractByteBufTest method testMemoryAddressAfterRelease.
@Test
public void testMemoryAddressAfterRelease() {
ByteBuf buf = releasedBuffer();
if (buf.hasMemoryAddress()) {
try {
buf.memoryAddress();
fail();
} catch (IllegalReferenceCountException e) {
// expected
}
}
}
Aggregations