Skip to content

Conversation

@Frando
Copy link
Member

@Frando Frando commented Oct 23, 2025

This adds Connection::on_close, which returns a future that resolves to (ConnectionError, ConnectionStats) without keeping the connection alive. I.e. if all other clones of a Connection are dropped, the connection will be closed implicitly, and the future will resolve.

Motivation: There's a few situations in which you might want to await a connection being closed without keeping the connection alive:

  • A connection manager might hand out connections to other tasks, and also keep some local state about the connections. It might not want to own a Connection because that would keep the connection alive. But to clean up its local state, it wants to get a notification when the connection is closed. This PR allows for that.
  • Users may want to track stats for all connections. With this change, on_closed can be called right after the connection is opened or accepted from a central place, and the future can be sent to a stats tracker. Without this change, all tasks that deal with connections would need to be aware of the stats tracker to sent the final connection stats after the connection is closed to the stats tracker. This is possible, but does not allow for easy and centralized stats tracker as it needs support from all code that deals with connections.

Copy link
Collaborator

@flub flub left a comment

Choose a reason for hiding this comment

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

Seems fine. We should probably submit this upstream too?

/// Returns the reason why the connection was closed and the final [`ConnectionStats`].
pub fn on_closed(
&self,
) -> impl Future<Output = (ConnectionError, ConnectionStats)> + Send + Sync + 'static {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the Quinn convention is to name the returned futures? IIRC the main difference is that they can be stored in structs easily when named, instead of needing boxing. I don't really know how strong this convention is. But looking at Connection this now seems like the odd one out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it to make the future public

@flub
Copy link
Collaborator

flub commented Oct 28, 2025

Could you add a little more motivation to the description though? Where do we need this that we can't do any existing way?

@Frando
Copy link
Member Author

Frando commented Oct 28, 2025

We should probably submit this upstream too?

Sure. It depends on the WeakConnection though which hasn't been upstreamed either AFAIK.

@Frando
Copy link
Member Author

Frando commented Oct 28, 2025

Added some motivation. I think it could also be useful for the endoint state actor in iroh on the multipath branch? To clean up state once a connection is closed.

@flub
Copy link
Collaborator

flub commented Oct 28, 2025

We should probably submit this upstream too?

Sure. It depends on the WeakConnection though which hasn't been upstreamed either AFAIK.

Oh right. That's true. Never mind then.

@flub flub changed the base branch from multipath-quinn-0.11.x to main-iroh October 29, 2025 12:04
@n0bot n0bot bot added this to iroh Oct 30, 2025
@github-project-automation github-project-automation bot moved this to 🏗 In progress in iroh Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🏗 In progress

Development

Successfully merging this pull request may close these issues.

3 participants