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 — Ensure ChannelPromise that are used by ChannelOutboundHandler will al… · netty/netty@c356652

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel
      • AbstractChannelHandlerContext.java
    • test/java/io/netty/channel
      • DefaultChannelPipelineTest.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
+40-35Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.netty.util.concurrent.AbstractEventExecutor;
2525
import io.netty.util.concurrent.EventExecutor;
2626
import io.netty.util.concurrent.OrderedEventExecutor;
27+
import io.netty.util.concurrent.PromiseNotifier;
2728
import io.netty.util.internal.ObjectPool.Handle;
2829
import io.netty.util.internal.ObjectUtil;
2930
import io.netty.util.internal.PromiseNotificationUtil;
@@ -457,8 +458,26 @@ public ChannelFuture deregister() {
457458
return deregister(newPromise());
458459
}
459460

461+
/**
462+
* If possible check if the given {@link ChannelPromise} is using the same {@link EventExecutor} as this
463+
* {@link ChannelHandlerContext} and if not return a new {@link ChannelPromise} that runs on the same
464+
* {@link EventExecutor} as this {@link ChannelHandlerContext}. The result of the new {@link ChannelPromise} is
465+
* cascaded to the old {@link ChannelPromise}.
466+
*
467+
* This is done to ensure that {@link ChannelFutureListener}s that are added to the {@link ChannelPromise} by an
468+
* {@link ChannelOutboundHandler} are executed in the same thread as the handler itself. By doing so we can
469+
* ensure that there are not issues even if fields etc that are stored in the handler are modified by the listener.
470+
*/
471+
private ChannelPromise ensurePromiseUseCorrectExecutor(ChannelPromise promise) {
472+
if (promise instanceof DefaultChannelPromise && !((DefaultChannelPromise) promise).executor().inEventLoop()) {
473+
ChannelPromise newPromise = newPromise();
474+
PromiseNotifier.cascade(newPromise, promise);
475+
return newPromise;
476+
}
477+
return promise;
478+
}
460479
@Override
461-
public ChannelFuture bind(final SocketAddress localAddress, final ChannelPromise promise) {
480+
public ChannelFuture bind(final SocketAddress localAddress, ChannelPromise promise) {
462481
ObjectUtil.checkNotNull(localAddress, "localAddress");
463482
if (isNotValidPromise(promise, false)) {
464483
// cancelled
@@ -469,6 +488,7 @@ public ChannelFuture bind(final SocketAddress localAddress, final ChannelPromise
469488
EventExecutor executor = next.executor();
470489
if (executor.inEventLoop()) {
471490
if (next.invokeHandler()) {
491+
promise = ensurePromiseUseCorrectExecutor(promise);
472492
try {
473493
// DON'T CHANGE
474494
// Duplex handlers implements both out/in interfaces causing a scalability issue
@@ -491,12 +511,8 @@ public ChannelFuture bind(final SocketAddress localAddress, final ChannelPromise
491511
next.bind(localAddress, promise);
492512
}
493513
} else {
494-
safeExecute(executor, new Runnable() {
495-
@Override
496-
public void run() {
497-
bind(localAddress, promise);
498-
}
499-
}, promise, null, false);
514+
final ChannelPromise p = promise;
515+
safeExecute(executor, () -> bind(localAddress, p), promise, null, false);
500516
}
501517
return promise;
502518
}
@@ -508,7 +524,7 @@ public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise
508524

509525
@Override
510526
public ChannelFuture connect(
511-
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
527+
final SocketAddress remoteAddress, final SocketAddress localAddress, ChannelPromise promise) {
512528
ObjectUtil.checkNotNull(remoteAddress, "remoteAddress");
513529

514530
if (isNotValidPromise(promise, false)) {
@@ -520,6 +536,7 @@ public ChannelFuture connect(
520536
EventExecutor executor = next.executor();
521537
if (executor.inEventLoop()) {
522538
if (next.invokeHandler()) {
539+
promise = ensurePromiseUseCorrectExecutor(promise);
523540
try {
524541
// DON'T CHANGE
525542
// Duplex handlers implements both out/in interfaces causing a scalability issue
@@ -542,18 +559,14 @@ public ChannelFuture connect(
542559
next.connect(remoteAddress, localAddress, promise);
543560
}
544561
} else {
545-
safeExecute(executor, new Runnable() {
546-
@Override
547-
public void run() {
548-
connect(remoteAddress, localAddress, promise);
549-
}
550-
}, promise, null, false);
562+
final ChannelPromise p = promise;
563+
safeExecute(executor, () -> connect(remoteAddress, localAddress, p), promise, null, false);
551564
}
552565
return promise;
553566
}
554567

555568
@Override
556-
public ChannelFuture disconnect(final ChannelPromise promise) {
569+
public ChannelFuture disconnect(ChannelPromise promise) {
557570
if (!channel().metadata().hasDisconnect()) {
558571
// Translate disconnect to close if the channel has no notion of disconnect-reconnect.
559572
// So far, UDP/IP is the only transport that has such behavior.
@@ -568,6 +581,7 @@ public ChannelFuture disconnect(final ChannelPromise promise) {
568581
EventExecutor executor = next.executor();
569582
if (executor.inEventLoop()) {
570583
if (next.invokeHandler()) {
584+
promise = ensurePromiseUseCorrectExecutor(promise);
571585
try {
572586
// DON'T CHANGE
573587
// Duplex handlers implements both out/in interfaces causing a scalability issue
@@ -590,18 +604,14 @@ public ChannelFuture disconnect(final ChannelPromise promise) {
590604
next.disconnect(promise);
591605
}
592606
} else {
593-
safeExecute(executor, new Runnable() {
594-
@Override
595-
public void run() {
596-
disconnect(promise);
597-
}
598-
}, promise, null, false);
607+
final ChannelPromise p = promise;
608+
safeExecute(executor, () -> disconnect(p), promise, null, false);
599609
}
600610
return promise;
601611
}
602612

603613
@Override
604-
public ChannelFuture close(final ChannelPromise promise) {
614+
public ChannelFuture close(ChannelPromise promise) {
605615
if (isNotValidPromise(promise, false)) {
606616
// cancelled
607617
return promise;
@@ -611,6 +621,7 @@ public ChannelFuture close(final ChannelPromise promise) {
611621
EventExecutor executor = next.executor();
612622
if (executor.inEventLoop()) {
613623
if (next.invokeHandler()) {
624+
promise = ensurePromiseUseCorrectExecutor(promise);
614625
try {
615626
// DON'T CHANGE
616627
// Duplex handlers implements both out/in interfaces causing a scalability issue
@@ -633,19 +644,15 @@ public ChannelFuture close(final ChannelPromise promise) {
633644
next.close(promise);
634645
}
635646
} else {
636-
safeExecute(executor, new Runnable() {
637-
@Override
638-
public void run() {
639-
close(promise);
640-
}
641-
}, promise, null, false);
647+
final ChannelPromise p = promise;
648+
safeExecute(executor, () -> close(p), promise, null, false);
642649
}
643650

644651
return promise;
645652
}
646653

647654
@Override
648-
public ChannelFuture deregister(final ChannelPromise promise) {
655+
public ChannelFuture deregister(ChannelPromise promise) {
649656
if (isNotValidPromise(promise, false)) {
650657
// cancelled
651658
return promise;
@@ -655,6 +662,7 @@ public ChannelFuture deregister(final ChannelPromise promise) {
655662
EventExecutor executor = next.executor();
656663
if (executor.inEventLoop()) {
657664
if (next.invokeHandler()) {
665+
promise = ensurePromiseUseCorrectExecutor(promise);
658666
try {
659667
// DON'T CHANGE
660668
// Duplex handlers implements both out/in interfaces causing a scalability issue
@@ -677,12 +685,8 @@ public ChannelFuture deregister(final ChannelPromise promise) {
677685
deregister(promise);
678686
}
679687
} else {
680-
safeExecute(executor, new Runnable() {
681-
@Override
682-
public void run() {
683-
deregister(promise);
684-
}
685-
}, promise, null, false);
688+
final ChannelPromise p = promise;
689+
safeExecute(executor, () -> deregister(p), promise, null, false);
686690
}
687691

688692
return promise;
@@ -780,6 +784,7 @@ void write(Object msg, boolean flush, ChannelPromise promise) {
780784
EventExecutor executor = next.executor();
781785
if (executor.inEventLoop()) {
782786
if (next.invokeHandler()) {
787+
promise = ensurePromiseUseCorrectExecutor(promise);
783788
try {
784789
// DON'T CHANGE
785790
// Duplex handlers implements both out/in interfaces causing a scalability issue
Collapse file

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

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java
+120Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.netty.util.ReferenceCountUtil;
3636
import io.netty.util.ReferenceCounted;
3737
import io.netty.util.concurrent.AbstractEventExecutor;
38+
import io.netty.util.concurrent.DefaultEventExecutor;
3839
import io.netty.util.concurrent.DefaultEventExecutorGroup;
3940
import io.netty.util.concurrent.EventExecutor;
4041
import io.netty.util.concurrent.EventExecutorGroup;
@@ -58,6 +59,7 @@
5859
import java.util.List;
5960
import java.util.NoSuchElementException;
6061
import java.util.Queue;
62+
import java.util.concurrent.ArrayBlockingQueue;
6163
import java.util.concurrent.BlockingQueue;
6264
import java.util.concurrent.CountDownLatch;
6365
import java.util.concurrent.ExecutorService;
@@ -916,6 +918,124 @@ public void execute() {
916918
}
917919
}
918920

921+
@Test
922+
public void testPromiseCorrectExecutor() throws Exception {
923+
final ChannelPipeline pipeline = new LocalChannel().pipeline();
924+
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<Boolean>(6);
925+
EventExecutor executor = new DefaultEventExecutor();
926+
try {
927+
pipeline.addLast(new ChannelOutboundHandlerAdapter() {
928+
@Override
929+
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) {
930+
promise.setSuccess();
931+
}
932+
933+
@Override
934+
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
935+
SocketAddress localAddress, ChannelPromise promise) {
936+
promise.setSuccess();
937+
}
938+
939+
@Override
940+
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
941+
promise.setSuccess();
942+
}
943+
944+
@Override
945+
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
946+
promise.setSuccess();
947+
}
948+
949+
@Override
950+
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
951+
promise.setSuccess();
952+
}
953+
954+
@Override
955+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
956+
promise.setSuccess();
957+
}
958+
}, new ChannelOutboundHandlerAdapter() {
959+
960+
ChannelFutureListener listener;
961+
962+
@Override
963+
public void handlerAdded(ChannelHandlerContext ctx) {
964+
listener = f -> {
965+
queue.add(ctx.executor().inEventLoop());
966+
};
967+
}
968+
969+
@Override
970+
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) {
971+
ctx.bind(localAddress, promise.addListener(listener));
972+
}
973+
974+
@Override
975+
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
976+
SocketAddress localAddress, ChannelPromise promise) {
977+
ctx.connect(remoteAddress, localAddress, promise.addListener(listener));
978+
}
979+
980+
@Override
981+
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
982+
ctx.disconnect(promise.addListener(listener));
983+
}
984+
985+
@Override
986+
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
987+
ctx.close(promise.addListener(listener));
988+
}
989+
990+
@Override
991+
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
992+
ctx.deregister(promise.addListener(listener));
993+
}
994+
995+
@Override
996+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
997+
ctx.write(msg, promise.addListener(listener));
998+
}
999+
});
1000+
group.register(pipeline.channel()).sync();
1001+
1002+
ChannelPromise promise = new DefaultChannelPromise(pipeline.channel(), executor);
1003+
pipeline.bind(new SocketAddress() { }, promise);
1004+
promise.sync();
1005+
1006+
promise = new DefaultChannelPromise(pipeline.channel(), executor);
1007+
pipeline.connect(new SocketAddress() { }, promise);
1008+
promise.sync();
1009+
1010+
promise = new DefaultChannelPromise(pipeline.channel(), executor);
1011+
pipeline.disconnect(promise);
1012+
promise.sync();
1013+
1014+
promise = new DefaultChannelPromise(pipeline.channel(), executor);
1015+
pipeline.close(promise);
1016+
promise.sync();
1017+
1018+
promise = new DefaultChannelPromise(pipeline.channel(), executor);
1019+
pipeline.deregister(promise);
1020+
promise.sync();
1021+
1022+
promise = new DefaultChannelPromise(pipeline.channel(), executor);
1023+
pipeline.write("", promise);
1024+
promise.sync();
1025+
} finally {
1026+
// Remove the handlers before closing so we don't intercept it.
1027+
while (pipeline.lastContext() != null) {
1028+
pipeline.removeLast();
1029+
}
1030+
pipeline.close();
1031+
executor.shutdownGracefully();
1032+
}
1033+
1034+
for (int i = 0; i < 6; i++) {
1035+
assertTrue(queue.take());
1036+
}
1037+
}
1038+
9191039
@Test
9201040
public void testUnexpectedVoidChannelPromise() throws Exception {
9211041
final ChannelPipeline pipeline = new LocalChannel().pipeline();

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.