GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Revert "[#5028] Fix re-entrance issue with channelWritabilityChanged(… · pulllock/netty@8019447

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel
      • AbstractChannelHandlerContext.java
      • ChannelOutboundBuffer.java
      • PendingWriteQueue.java
    • test/java/io/netty/channel
      • BaseChannelTest.java
      • ChannelOutboundBufferTest.java
      • ReentrantChannelTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java
+2-6Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,7 @@ protected static void init(AbstractWriteTask task, AbstractChannelHandlerContext
869869
// Check for null as it may be set to null if the channel is closed already
870870
if (buffer != null) {
871871
task.size = ((AbstractChannel) ctx.channel()).estimatorHandle().size(msg) + WRITE_TASK_OVERHEAD;
872-
// We increment the pending bytes but NOT call fireChannelWritabilityChanged() because this
873-
// will be done automaticaly once we add the message to the ChannelOutboundBuffer.
874-
buffer.incrementPendingOutboundBytes(task.size, false);
872+
buffer.incrementPendingOutboundBytes(task.size);
875873
} else {
876874
task.size = 0;
877875
}
@@ -886,9 +884,7 @@ public final void run() {
886884
ChannelOutboundBuffer buffer = ctx.channel().unsafe().outboundBuffer();
887885
// Check for null as it may be set to null if the channel is closed already
888886
if (ESTIMATE_TASK_SIZE_ON_SUBMIT && buffer != null) {
889-
// We decrement the pending bytes but NOT call fireChannelWritabilityChanged() because this
890-
// will be done automaticaly once we pick up the messages out of the buffer to actually write these.
891-
buffer.decrementPendingOutboundBytes(size, false);
887+
buffer.decrementPendingOutboundBytes(size);
892888
}
893889
write(ctx, msg, promise);
894890
} finally {
Collapse file

‎transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java
+44-46Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected ByteBuffer[] initialValue() throws Exception {
8989
@SuppressWarnings("UnusedDeclaration")
9090
private volatile int unwritable;
9191

92-
private final Runnable fireChannelWritabilityChangedTask;
92+
private volatile Runnable fireChannelWritabilityChangedTask;
9393

9494
static {
9595
AtomicIntegerFieldUpdater<ChannelOutboundBuffer> unwritableUpdater =
@@ -107,9 +107,8 @@ protected ByteBuffer[] initialValue() throws Exception {
107107
TOTAL_PENDING_SIZE_UPDATER = pendingSizeUpdater;
108108
}
109109

110-
ChannelOutboundBuffer(final AbstractChannel channel) {
110+
ChannelOutboundBuffer(AbstractChannel channel) {
111111
this.channel = channel;
112-
fireChannelWritabilityChangedTask = new ChannelWritabilityChangedTask(channel);
113112
}
114113

115114
/**
@@ -132,7 +131,7 @@ public void addMessage(Object msg, int size, ChannelPromise promise) {
132131

133132
// increment pending bytes after adding message to the unflushed arrays.
134133
// See https://github.com/netty/netty/issues/1619
135-
incrementPendingOutboundBytes(size, true);
134+
incrementPendingOutboundBytes(size, false);
136135
}
137136

138137
/**
@@ -155,7 +154,7 @@ public void addFlush() {
155154
if (!entry.promise.setUncancellable()) {
156155
// Was cancelled so make sure we free up memory and notify about the freed bytes
157156
int pending = entry.cancel();
158-
decrementPendingOutboundBytes(pending, true);
157+
decrementPendingOutboundBytes(pending, false, true);
159158
}
160159
entry = entry.next;
161160
} while (entry != null);
@@ -169,30 +168,38 @@ public void addFlush() {
169168
* Increment the pending bytes which will be written at some point.
170169
* This method is thread-safe!
171170
*/
172-
void incrementPendingOutboundBytes(long size, boolean notifyWritability) {
171+
void incrementPendingOutboundBytes(long size) {
172+
incrementPendingOutboundBytes(size, true);
173+
}
174+
175+
private void incrementPendingOutboundBytes(long size, boolean invokeLater) {
173176
if (size == 0) {
174177
return;
175178
}
176179

177180
long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, size);
178181
if (newWriteBufferSize >= channel.config().getWriteBufferHighWaterMark()) {
179-
setUnwritable(notifyWritability);
182+
setUnwritable(invokeLater);
180183
}
181184
}
182185

183186
/**
184187
* Decrement the pending bytes which will be written at some point.
185188
* This method is thread-safe!
186189
*/
187-
void decrementPendingOutboundBytes(long size, boolean notifyWritability) {
190+
void decrementPendingOutboundBytes(long size) {
191+
decrementPendingOutboundBytes(size, true, true);
192+
}
193+
194+
private void decrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability) {
188195
if (size == 0) {
189196
return;
190197
}
191198

192199
long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -size);
193-
if (newWriteBufferSize == 0
194-
|| newWriteBufferSize <= channel.config().getWriteBufferLowWaterMark()) {
195-
setWritable(notifyWritability);
200+
if (notifyWritability && (newWriteBufferSize == 0
201+
|| newWriteBufferSize <= channel.config().getWriteBufferLowWaterMark())) {
202+
setWritable(invokeLater);
196203
}
197204
}
198205

@@ -257,7 +264,7 @@ public boolean remove() {
257264
// only release message, notify and decrement if it was not canceled before.
258265
ReferenceCountUtil.safeRelease(msg);
259266
safeSuccess(promise);
260-
decrementPendingOutboundBytes(size, true);
267+
decrementPendingOutboundBytes(size, false, true);
261268
}
262269

263270
// recycle the entry
@@ -293,7 +300,7 @@ private boolean remove0(Throwable cause, boolean notifyWritability) {
293300
ReferenceCountUtil.safeRelease(msg);
294301

295302
safeFail(promise, cause);
296-
decrementPendingOutboundBytes(size, notifyWritability);
303+
decrementPendingOutboundBytes(size, false, notifyWritability);
297304
}
298305

299306
// recycle the entry
@@ -515,7 +522,7 @@ private void setUserDefinedWritability(int index) {
515522
final int newValue = oldValue & mask;
516523
if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) {
517524
if (oldValue != 0 && newValue == 0) {
518-
fireChannelWritabilityChanged();
525+
fireChannelWritabilityChanged(true);
519526
}
520527
break;
521528
}
@@ -529,7 +536,7 @@ private void clearUserDefinedWritability(int index) {
529536
final int newValue = oldValue | mask;
530537
if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) {
531538
if (oldValue == 0 && newValue != 0) {
532-
fireChannelWritabilityChanged();
539+
fireChannelWritabilityChanged(true);
533540
}
534541
break;
535542
}
@@ -543,36 +550,48 @@ private static int writabilityMask(int index) {
543550
return 1 << index;
544551
}
545552

546-
private void setWritable(boolean notify) {
553+
private void setWritable(boolean invokeLater) {
547554
for (;;) {
548555
final int oldValue = unwritable;
549556
final int newValue = oldValue & ~1;
550557
if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) {
551-
if (notify && oldValue != 0 && newValue == 0) {
552-
fireChannelWritabilityChanged();
558+
if (oldValue != 0 && newValue == 0) {
559+
fireChannelWritabilityChanged(invokeLater);
553560
}
554561
break;
555562
}
556563
}
557564
}
558565

559-
private void setUnwritable(boolean notify) {
566+
private void setUnwritable(boolean invokeLater) {
560567
for (;;) {
561568
final int oldValue = unwritable;
562569
final int newValue = oldValue | 1;
563570
if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) {
564-
if (notify && oldValue == 0 && newValue != 0) {
565-
fireChannelWritabilityChanged();
571+
if (oldValue == 0 && newValue != 0) {
572+
fireChannelWritabilityChanged(invokeLater);
566573
}
567574
break;
568575
}
569576
}
570577
}
571578

572-
private void fireChannelWritabilityChanged() {
573-
// Always invoke it later to prevent re-entrance bug.
574-
// See https://github.com/netty/netty/issues/5028
575-
channel.eventLoop().execute(fireChannelWritabilityChangedTask);
579+
private void fireChannelWritabilityChanged(boolean invokeLater) {
580+
final ChannelPipeline pipeline = channel.pipeline();
581+
if (invokeLater) {
582+
Runnable task = fireChannelWritabilityChangedTask;
583+
if (task == null) {
584+
fireChannelWritabilityChangedTask = task = new Runnable() {
585+
@Override
586+
public void run() {
587+
pipeline.fireChannelWritabilityChanged();
588+
}
589+
};
590+
}
591+
channel.eventLoop().execute(task);
592+
} else {
593+
pipeline.fireChannelWritabilityChanged();
594+
}
576595
}
577596

578597
/**
@@ -843,25 +862,4 @@ Entry recycleAndGetNext() {
843862
return next;
844863
}
845864
}
846-
847-
private static final class ChannelWritabilityChangedTask implements Runnable {
848-
private final Channel channel;
849-
private boolean writable = true;
850-
851-
ChannelWritabilityChangedTask(Channel channel) {
852-
this.channel = channel;
853-
}
854-
855-
@Override
856-
public void run() {
857-
if (channel.isActive()) {
858-
boolean newWritable = channel.isWritable();
859-
860-
if (writable != newWritable) {
861-
writable = newWritable;
862-
channel.pipeline().fireChannelWritabilityChanged();
863-
}
864-
}
865-
}
866-
}
867865
}
Collapse file

‎transport/src/main/java/io/netty/channel/PendingWriteQueue.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/PendingWriteQueue.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void add(Object msg, ChannelPromise promise) {
9494
// if the channel was already closed when constructing the PendingWriteQueue.
9595
// See https://github.com/netty/netty/issues/3967
9696
if (buffer != null) {
97-
buffer.incrementPendingOutboundBytes(write.size, true);
97+
buffer.incrementPendingOutboundBytes(write.size);
9898
}
9999
}
100100

@@ -264,7 +264,7 @@ private void recycle(PendingWrite write, boolean update) {
264264
// if the channel was already closed when constructing the PendingWriteQueue.
265265
// See https://github.com/netty/netty/issues/3967
266266
if (buffer != null) {
267-
buffer.decrementPendingOutboundBytes(writeSize, true);
267+
buffer.decrementPendingOutboundBytes(writeSize);
268268
}
269269
}
270270

Collapse file

‎transport/src/test/java/io/netty/channel/BaseChannelTest.java‎

Copy file name to clipboard
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.channel;
17+
18+
19+
import io.netty.bootstrap.Bootstrap;
20+
import io.netty.bootstrap.ServerBootstrap;
21+
import io.netty.buffer.ByteBuf;
22+
import io.netty.buffer.Unpooled;
23+
import io.netty.channel.local.LocalChannel;
24+
import io.netty.channel.local.LocalEventLoopGroup;
25+
import io.netty.channel.local.LocalServerChannel;
26+
27+
import static org.junit.Assert.*;
28+
29+
class BaseChannelTest {
30+
31+
private final LoggingHandler loggingHandler;
32+
33+
BaseChannelTest() {
34+
loggingHandler = new LoggingHandler();
35+
}
36+
37+
ServerBootstrap getLocalServerBootstrap() {
38+
EventLoopGroup serverGroup = new LocalEventLoopGroup();
39+
ServerBootstrap sb = new ServerBootstrap();
40+
sb.group(serverGroup);
41+
sb.channel(LocalServerChannel.class);
42+
sb.childHandler(new ChannelInitializer<LocalChannel>() {
43+
@Override
44+
public void initChannel(LocalChannel ch) throws Exception {
45+
}
46+
});
47+
48+
return sb;
49+
}
50+
51+
Bootstrap getLocalClientBootstrap() {
52+
EventLoopGroup clientGroup = new LocalEventLoopGroup();
53+
Bootstrap cb = new Bootstrap();
54+
cb.channel(LocalChannel.class);
55+
cb.group(clientGroup);
56+
57+
cb.handler(loggingHandler);
58+
59+
return cb;
60+
}
61+
62+
static ByteBuf createTestBuf(int len) {
63+
ByteBuf buf = Unpooled.buffer(len, len);
64+
buf.setIndex(0, len);
65+
return buf;
66+
}
67+
68+
void assertLog(String firstExpected, String... otherExpected) {
69+
String actual = loggingHandler.getLog();
70+
if (firstExpected.equals(actual)) {
71+
return;
72+
}
73+
for (String e: otherExpected) {
74+
if (e.equals(actual)) {
75+
return;
76+
}
77+
}
78+
79+
// Let the comparison fail with the first expectation.
80+
assertEquals(firstExpected, actual);
81+
}
82+
83+
void clearLog() {
84+
loggingHandler.clear();
85+
}
86+
87+
void setInterest(LoggingHandler.Event... events) {
88+
loggingHandler.setInterest(events);
89+
}
90+
}
Collapse file

‎transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java
-9Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,11 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio
228228

229229
// Ensure exceeding the high watermark makes channel unwritable.
230230
ch.write(buffer().writeZero(128));
231-
assertThat(buf.toString(), is(""));
232-
233-
ch.runPendingTasks();
234231
assertThat(buf.toString(), is("false "));
235232

236233
// Ensure going down to the low watermark makes channel writable again by flushing the first write.
237234
assertThat(ch.unsafe().outboundBuffer().remove(), is(true));
238235
assertThat(ch.unsafe().outboundBuffer().totalPendingWriteBytes(), is(128L));
239-
assertThat(buf.toString(), is("false "));
240-
241-
ch.runPendingTasks();
242236
assertThat(buf.toString(), is("false true "));
243237

244238
safeClose(ch);
@@ -336,9 +330,6 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio
336330

337331
// Trigger channelWritabilityChanged() by writing a lot.
338332
ch.write(buffer().writeZero(256));
339-
assertThat(buf.toString(), is(""));
340-
341-
ch.runPendingTasks();
342333
assertThat(buf.toString(), is("false "));
343334

344335
// Ensure that setting a user-defined writability flag to false does not trigger channelWritabilityChanged()

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.