From 946bba86cc9de9f7647e54b4980437e7b242aaf1 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 16 Feb 2024 13:56:10 +0100 Subject: [PATCH] Allow to cancel connect() operations when using io_uring Motivation: Whe using io_uring we can in fact cancel a connect() operation by just closing the underyling fd. Modifications: Don't mark the promise as uncancellable before issue the non-blocking connect() call Add comment to explain code Result: Related to https://github.com/netty/netty/issues/13843 and https://github.com/netty/netty/pull/13849 --- .../incubator/channel/uring/AbstractIOUringChannel.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/AbstractIOUringChannel.java b/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/AbstractIOUringChannel.java index 734d0b3f..da09d2b8 100644 --- a/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/AbstractIOUringChannel.java +++ b/transport-classes-io_uring/src/main/java/io/netty/incubator/channel/uring/AbstractIOUringChannel.java @@ -680,7 +680,9 @@ void connectComplete(int res) { @Override public void connect( final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { - if (!promise.setUncancellable() || !ensureOpen(promise)) { + // Don't mark the connect promise as uncancellable as in fact we can cancel it as it is using + // non-blocking io. + if (promise.isDone() || !ensureOpen(promise)) { return; } @@ -760,6 +762,8 @@ public void run() { promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { + // If the connect future is cancelled we also cancel the timeout and close the + // underlying socket. if (future.isCancelled()) { cancelConnectTimeoutFuture(); connectPromise = null;