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 — Clean up Future/Promises API by normanmaurer · Pull Request #16039 · netty/netty

Conversation

@normanmaurer

@normanmaurer normanmaurer commented Dec 14, 2025

Copy link
Copy Markdown
Member

Motivation:
The generics for the existing futures, promises, and listeners are too complicated. This complication comes from the existence of ChannelPromise and ChannelFuture, which forces listeners to care about the particular type of future being listened on.

Modification:

  • Make the FutureListener, the default listener API, retiring the GenericFutureListener since we no longer need to abstract over the type of the future.
  • Change all uses of ChannelPromise to Promise<Void>.
  • Change all uses of ChannelFuture to Future<Void>.
  • Change all uses of GenericFutureListener to FutureListener as needed.
  • Remove ChannelFutureListener and GenericFutureListener.
  • Remove ChannelPromise and ChannelFuture — all usages now rely on the plain Future and Promise APIs.
  • Remove CompleteChannelFuture, SucceededChannelFuture, and FailedChannelFuture.

Result:
Cleaner generics and more straight forward code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

slandelle and MukjepScarlet reacted with hooray emoji
All reactions
  • 🎉 2 reactions
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Dec 14, 2025
@normanmaurer

normanmaurer commented Dec 14, 2025

Copy link
Copy Markdown
Member Author

I will add FutureContextListener as a followup

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer requested review from chrisvest and yawkat December 15, 2025 12:32
yawkat
yawkat reviewed Dec 15, 2025
View reviewed changes
Comment thread codec-base/src/main/java/io/netty/handler/codec/MessageAggregator.java
if (closeAfterWrite) {
handleIncompleteAggregateDuringClose = false;
future.addListener(ChannelFutureListener.CLOSE);
future.addListener(f -> {

@yawkat yawkat Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since i'm sure this is common, and this new pattern leads to lambda allocations, maybe it's worthwhile to cache these standard future listeners in the channel?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@slandelle slandelle Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep a built-in constant around, eg in ChannelContext?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yawkat yawkat Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be a constant because the listener doesn't know about the channel anymore

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 15, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes... we can do something similar once we bring back FutureContextListener but I wanted to keep it focused first and make it work

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yawkat and chrisvest reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
yawkat
yawkat reviewed Dec 15, 2025
View reviewed changes
Comment thread codec-http2/src/main/java/io/netty/handler/codec/http2/Http2MultiplexHandler.java
}

static void registerDone(ChannelFuture future) {
static void registerDone(Future<? extends Void> future, Channel childChannel) {

@yawkat yawkat Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the wildcard? Void is final

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 15, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because its what operationComplete(...) has as signature and so I don't need a cast

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
yawkat
yawkat reviewed Dec 15, 2025
View reviewed changes
Comment thread transport/src/main/java/io/netty/bootstrap/Bootstrap.java
@@ -146,7 +144,7 @@ public Bootstrap remoteAddress(InetAddress inetHost, int inetPort) {
/**
* Connect a {@link Channel} to the remote peer.
*/
public ChannelFuture connect() {
public Future<Channel> connect() {

@yawkat yawkat Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be worthwhile using wildcards here and elsewhere. Using Future<? extends Channel> should work just as well on the user side, and may make implementation easier. It's more correct imo.

On the other hand, the advantages of the wildcard are probably minor, and it makes the code more verbose for all users.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 15, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about that.. might make things a lot more verbose. On the other hand people could just use var future = anyway so it might not not be a problem . @chrisvest WDYT ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yawkat yawkat Dec 15, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be adding a <W super V> Future<W> downcast() { return (Future) this; } method, then conversions to the exact type becomes easy

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest Dec 15, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If people want to get at the concrete channel type, I think they will have to cast regardless.

Looking at how the approaches play out, I think Future<Channel> comes out ahead:

    private static Future<? extends Channel> wildcardChannel() { return null; }
    private static Future<Channel> justChannel() { return null; }
    
    private static void assigning() {
        Future<? extends Channel> wfc;
        Future<Channel> jfc;
        
        wfc = wildcardChannel();
        wfc = justChannel();
        jfc = wildcardChannel(); // ❌ error
        jfc = justChannel();
    }
    
    private static void wildcard() {
        Future<? extends Channel> fc = null;
        takeWildcard(fc);
        takeJust(fc); // ❌ error
        takeGenericFuture(fc); // ❌ error
        takeGenericWildcard(fc);
        takeGenericChannel(fc);
        Channel c = fc.getNow();
    }
    
    private static void just() {
        Future<Channel> fc = null;
        takeWildcard(fc);
        takeJust(fc);
        takeGenericFuture(fc);
        takeGenericWildcard(fc);
        takeGenericChannel(fc);
        Channel c = fc.getNow();
    }

    private static void subclass() {
        Future<NioDatagramChannel> fc = null;
        takeWildcard(fc);
        takeJust(fc); // ❌ error
        takeGenericFuture(fc); // ❌ error
        takeGenericWildcard(fc);
        takeGenericChannel(fc);
        Channel c = fc.getNow();
    }
    
    private static void takeWildcard(Future<? extends Channel> fc) {}
    private static void takeJust(Future<Channel> fc) {}
    private static <T extends Future<Channel>> void takeGenericFuture(T fc) {}
    private static <T extends Future<? extends Channel>> void takeGenericWildcard(T fc) {}
    private static <T extends Channel> void takeGenericChannel(Future<T> fc) {}

In the old main branch we also did Future<Channel>.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest Dec 15, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wildcards permit covariant return types, though, so if we need that, we have to use wildcards, or play the dance with generic superclasses:

    class A {
        Future<? extends Channel> wildcard() { return null; }
        Future<Channel> just() { return null; }
    }
    class B extends A {
        @Override
        Future<NioDatagramChannel> wildcard() { return null; }
        @Override
        Future<NioDatagramChannel> just() { return null; } // ❌ error
    }

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest Dec 16, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revising my earlier conclusion; if we want to make use of Future<SubclassOfChannel>, then we have to use wildcards in the generic places, to allow the variance.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. I think without wildcard might be better

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yawkat reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
yawkat
yawkat reviewed Dec 15, 2025
View reviewed changes
Comment thread transport/src/main/java/io/netty/bootstrap/Bootstrap.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest reviewed Dec 16, 2025
View reviewed changes
Comment thread codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicChannel.java Outdated

@Override
default ChannelFuture bind(SocketAddress localAddress) {
default Future<Void> bind(SocketAddress localAddress) {

@chrisvest chrisvest Dec 15, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind and connect should be Future<QuicChannel> or Future<Channel>

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisvest no... these are methods on the Channel itself and so should return Void. Only the one on *Bootstrap will return Future<Channel> (just as we did in the old main)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicChannel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HAProxyIntegrationTest.java Outdated
.handler(HAProxyMessageEncoder.INSTANCE)
.group(group)
.connect(localAddress).sync().channel();
.connect(localAddress).sync().getNow();

@chrisvest chrisvest Dec 15, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could replace a lot of these sync+getNow pattern instances with just get()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread codec-http/src/test/java/io/netty/handler/codec/http/HttpClientCodecTest.java
Future<Channel> ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel clientChannel = ccf.channel();
Channel clientChannel = ccf.getNow();

@chrisvest chrisvest Dec 15, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just do

Channel clientChannel = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port)).get();

An let any failure propagate from get() - we'd get more info than the assertTrue failing.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread codec-http2/src/test/java/io/netty/handler/codec/http2/DataCompressionHttp2Test.java
Future<Channel> ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
assertTrue(ccf.awaitUninterruptibly().isSuccess());
clientChannel = ccf.channel();
clientChannel = ccf.getNow();

@chrisvest chrisvest Dec 16, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think we could also just call get()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport/src/main/java/io/netty/channel/Channel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java

// This method is invoked before channelRegistered() is triggered. Give user handlers a chance to set up
// the pipeline in its channelRegistered() implementation.
channel.executor().execute(new Runnable() {
regFuture.executor().execute(new Runnable() {

@chrisvest chrisvest Dec 16, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we could probably attach a listener to regFuture to guarantee our callback runs after it completes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread transport/src/main/java/io/netty/bootstrap/Bootstrap.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport/src/main/java/io/netty/channel/IoTransport.java
*/
void register(ChannelPromise promise);
void register(Promise<Void> promise);

@chrisvest chrisvest Dec 16, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird now. Shouldn't this be a Promise<Channel> and take a target Channel parameter?

Same for bind, connect, disconnect, close, deregister and shutdown.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No... IoTransport is usually implemented by the Channel implementation anyway that is used by the ChannelPipeline and so the result of the promise is not really used at all.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is used by the head context of DefaultChannelPipeline).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
yawkat
yawkat reviewed Dec 16, 2025
View reviewed changes
Comment thread example/src/main/java/io/netty/example/discard/DiscardClientHandler.java
public void operationComplete(Future<? extends Void> f) {
if (f.isSuccess()) {
generateTraffic(ctx, this);
}

@yawkat yawkat Dec 16, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing failure case?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer Dec 16, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same as before... I just ported the code that's it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@normanmaurer
normanmaurer force-pushed the future_promise branch 2 times, most recently from 327d44e to a2bd174 Compare December 16, 2025 09:36
@normanmaurer
normanmaurer requested review from chrisvest, slandelle and yawkat December 16, 2025 12:01
This was referenced Dec 16, 2025
API: Change method signature to return void when promise is given #16051
Merged
Ensure ChannelPromise that are used by ChannelOutboundHandler will al… #16052
Merged
yawkat
yawkat approved these changes Dec 17, 2025
View reviewed changes
@normanmaurer
Clean up Future/Promises API
a1b7610
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.

Modification:
* Make the `FutureListener`, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to `FutureListener`  as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Remove `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.

Result:
Cleaner generics and more straight forward code.
@normanmaurer
normanmaurer force-pushed the future_promise branch from 9c183cd to a1b7610 Compare December 18, 2025 19:28
chrisvest
chrisvest approved these changes Dec 19, 2025
View reviewed changes
@normanmaurer
normanmaurer merged commit ce82678 into 5.0 Dec 19, 2025
@normanmaurer
normanmaurer deleted the future_promise branch December 19, 2025 06:55
@normanmaurer normanmaurer mentioned this pull request Sep 22, 2026
suspend race fix #17588
Closed
@normanmaurer normanmaurer mentioned this pull request Oct 20, 2026
fix iouring zerocopy iov overwrite #17936
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chrisvest chrisvest chrisvest approved these changes
@yawkat yawkat yawkat approved these changes
@slandelle slandelle Awaiting requested review from slandelle

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.0.0.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@normanmaurer @chrisvest @slandelle @yawkat
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.