private boolean release0(int decrement) { for (;;) { int refCnt = this.refCnt; if (refCnt < decrement) { throw new IllegalReferenceCountException(refCnt, -decrement); } if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) { if (refCnt == decrement) { deallocate(); return true; } return false; } } }
@Override public final boolean release(int decrement) { if (decrement <= 0) { throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)"); } for (;;) { int refCnt = this.refCnt; if (refCnt < decrement) { throw new IllegalReferenceCountException(refCnt, -decrement); } if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) { if (refCnt == decrement) { deallocate(); return true; } return false; } } }
@Override public ByteBuf content() { final ByteBuf data = (ByteBuf) super.payload(); if (data.refCnt() <= 0) { throw new IllegalReferenceCountException(data.refCnt()); } return data; }
/** * {@inheritDoc} * @see io.netty.channel.ChannelInboundHandlerAdapter#exceptionCaught(io.netty.channel.ChannelHandlerContext, java.lang.Throwable) */ @Override public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception { final Channel chan = ctx.channel(); if (cause instanceof ClosedChannelException) { exceptions_closed.inc(); log.warn("Attempt to write to closed channel " + chan); return; } else if(cause instanceof IllegalReferenceCountException) { log.warn("BadRefCount: [{}]", cause.getMessage()); } else if (cause instanceof IOException) { final String message = cause.getMessage(); if ("Connection reset by peer".equals(message)) { exceptions_reset.inc(); return; } else if ("Connection timed out".equals(message)) { exceptions_timeout.inc(); // Do nothing. A client disconnecting isn't really our problem. Oh, // and I'm not kidding you, there's no better way to detect ECONNRESET // in Java. Like, people have been bitching about errno for years, // and Java managed to do something *far* worse. That's quite a feat. return; } } // FIXME: not sure what the netty 4 is for this //if (cause instanceof CodecEmbedderException) { // // payload was not compressed as it was announced to be // log.warn("Http codec error : " + cause.getMessage()); // e.getChannel().close(); // return; //} exceptions_unknown.inc(); log.error("Unexpected exception from downstream for " + chan, cause); chan.close(); }
@Override public ByteBuf content() { if (data.refCnt() <= 0) { throw new IllegalReferenceCountException(data.refCnt()); } return data; }
@Test public void testArrayAfterRelease() { ByteBuf buf = releasedBuffer(); if (buf.hasArray()) { try { buf.array(); fail(); } catch (IllegalReferenceCountException e) { // expected } } }
@Test public void testMemoryAddressAfterRelease() { ByteBuf buf = releasedBuffer(); if (buf.hasMemoryAddress()) { try { buf.memoryAddress(); fail(); } catch (IllegalReferenceCountException e) { // expected } } }
private void assertAccessible() { if (refCnt() == 0) throw new IllegalReferenceCountException(0); }
private void assertValid() { if (refCnt() <= 0) throw new IllegalReferenceCountException("Packet deallocated"); }
@Test(expected = IllegalReferenceCountException.class) public void testGetBytesAfterRelease7() throws IOException { releasedBuffer().getBytes(0, new ByteArrayOutputStream(), 1); }
@Test(expected = IllegalReferenceCountException.class) public void testReadUnsignedMediumAfterRelease() { releasedBuffer().readUnsignedMedium(); }
@Test(expected = IllegalReferenceCountException.class) public void testEnsureWritableAfterRelease() { releasedBuffer().ensureWritable(16); }
@Test(expected = IllegalReferenceCountException.class) public void testSetBytesAfterRelease() { releasedBuffer().setBytes(0, releaseLater(buffer())); }
@Test(expected = IllegalReferenceCountException.class) public void testWriteFloatAfterRelease() { releasedBuffer().writeFloat(1); }
@Test(expected = IllegalReferenceCountException.class) public void testGetUnsignedByteAfterRelease() { releasedBuffer().getUnsignedByte(0); }
@Test(expected = IllegalReferenceCountException.class) public void testGetBytesAfterRelease4() { releasedBuffer().getBytes(0, new byte[8]); }
@Test(expected = IllegalReferenceCountException.class) public void testDiscardSomeReadBytesAfterRelease() { releasedBuffer().discardSomeReadBytes(); }
@Test(expected = IllegalReferenceCountException.class) public void testGetBooleanAfterRelease() { releasedBuffer().getBoolean(0); }
@Test(expected = IllegalReferenceCountException.class) public void testReadLongAfterRelease() { releasedBuffer().readLong(); }
@Test(expected = IllegalReferenceCountException.class) public void testWriteByteAfterRelease() { releasedBuffer().writeByte(1); }
@Test(expected = IllegalReferenceCountException.class) public void testGetShortAfterRelease() { releasedBuffer().getShort(0); }
@Test(expected = IllegalReferenceCountException.class) public void testNioBuffersAfterRelease() { releasedBuffer().nioBuffers(); }
@Test(expected = IllegalReferenceCountException.class) public void testWriteBytesAfterRelease2() { releasedBuffer().writeBytes(releaseLater(copiedBuffer(new byte[8])), 1); }
@Test(expected = IllegalReferenceCountException.class) public void testGetIntAfterRelease() { releasedBuffer().getInt(0); }
@Test(expected = IllegalReferenceCountException.class) public void testReadBytesAfterRelease3() { releasedBuffer().readBytes(releaseLater(buffer(8), 1)); }
@Test(expected = IllegalReferenceCountException.class) public void testGetLongAfterRelease() { releasedBuffer().getLong(0); }
@Test(expected = IllegalReferenceCountException.class) public void testGetCharAfterRelease() { releasedBuffer().getChar(0); }
@Test(expected = IllegalReferenceCountException.class) public void testWriteBooleanAfterRelease() { releasedBuffer().writeBoolean(true); }
@Test(expected = IllegalReferenceCountException.class) public void testCopyAfterRelease() { releasedBuffer().copy(); }
@Test(expected = IllegalReferenceCountException.class) public void testWriteBytesAfterRelease() { releasedBuffer().writeBytes(releaseLater(buffer(8))); }
@Test(expected = IllegalReferenceCountException.class) public void testGetBytesAfterRelease3() { releasedBuffer().getBytes(0, releaseLater(buffer()), 0, 1); }
@Test(expected = IllegalReferenceCountException.class) public void testSetBytesAfterRelease3() { releasedBuffer().setBytes(0, releaseLater(buffer()), 0, 1); }
@Test(expected = IllegalReferenceCountException.class) public void testSetBytesAfterRelease8() throws IOException { releasedBuffer().setBytes(0, new TestScatteringByteChannel(), 1); }
@Test(expected = IllegalReferenceCountException.class) public void testCopyAfterRelease1() { releasedBuffer().copy(); }
@Test(expected = IllegalReferenceCountException.class) public void testGetBytesAfterRelease8() throws IOException { releasedBuffer().getBytes(0, new DevNullGatheringByteChannel(), 1); }