Skip to content

fix(example): always close the endpoint#4007

Merged
flub merged 5 commits intomainfrom
flub/transfer-endpoint-close
Mar 11, 2026
Merged

fix(example): always close the endpoint#4007
flub merged 5 commits intomainfrom
flub/transfer-endpoint-close

Conversation

@flub
Copy link
Copy Markdown
Contributor

@flub flub commented Mar 10, 2026

Description

Even if there's an error we want to gracefully close the endpoint.

I also added some extra logging.

Breaking Changes

n/a

Notes & open questions

Look, this is the code we're making everyone write now.

I appreciate this is just a quick fix, I did not check out the full structure of the example and try to organise it as nicely as possible. So this probably increases technical debt of this example a bit more.

Change checklist

  • Self-review.

Even if there's an error we want to gracefully close the endpoint.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 10, 2026

Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/4007/docs/iroh/

Last updated: 2026-03-11T10:18:47Z

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 10, 2026

Netsim report & logs for this PR have been generated and is available at: LOGS
This report will remain available for 3 days.

Last updated for commit: 824458d

@flub flub requested review from a team and Frando March 10, 2026 16:27
@n0bot n0bot bot added this to iroh Mar 10, 2026
Comment on lines -736 to 746
close_endpoint_with_timeout(&endpoint, output).await;
output.emit(PathStats::from_watcher(watcher));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think ideally you close the endpoint and only then emit path stats, otherwise you might be missing stats.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, this makes me very sad. our stats APIs are still terrible. Did some kind of miserable fix. Probably.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What?

It's a simple matter of fact that during Endpoint::close you generate traffic, thus path stats.

My proposal would be to just call Endpoint::close here. Calling Endpoint::close twice doesn't hurt anything.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Calling Endpoint::close twice is indeed clever, but also an ugly hack. There really should just be a way to wait for the connection be drained. There is no more activity on the connection after that.

I was thinking of using Connection::closed first, but then realised it doesn't actually tell you the connection is drained. Because you close it locally it would just be completed right away.

Maybe the right solution is to have a Connection::drained method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, so the reason I do things this way is because I want to emit the stats once the path is drained. And the only way to get that state is to go via the ConnectionInfo::closed thing. I think that is a shortcoming in our API design. But I'm not in a place to fix that here.

Is closing the endpoint twice better or worse than this hack? Both are hacks. To me this version expresses more the intent: emit the stats once the connection is drained.

@github-project-automation github-project-automation bot moved this to 🚑 Needs Triage in iroh Mar 10, 2026
@flub flub force-pushed the flub/transfer-endpoint-close branch from 180903e to b4cfedb Compare March 11, 2026 10:12
@flub flub requested a review from matheus23 March 11, 2026 10:18
@flub flub added this pull request to the merge queue Mar 11, 2026
Merged via the queue into main with commit afc1faa Mar 11, 2026
29 checks passed
@github-project-automation github-project-automation bot moved this from 🚑 Needs Triage to ✅ Done in iroh Mar 11, 2026
@matheus23 matheus23 deleted the flub/transfer-endpoint-close branch March 11, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants