## Description closes https://github.com/n0-computer/iroh/issues/3867 relies on https://github.com/n0-computer/iroh/pull/3924 Alot of finagaling needed to happen to ensure that when the Endpoint is dropped, it's underlying resources are cleaned up ASAP. First, added a custom `Runtime` that wraps spawned tasks in a `CancellationToken`. This allows for both graceful shutdown (which does not use the cancel token, but instead waits for tasks to finish naturally) and ungraceful abort (which uses the cancel token to stop all running tasks) Wrote a new `Handle::abort` method that is sync and does not wait for graceful clean up after cancelling tokens, before aborting any runtime tasks. In order to ensure that the `Endpoint` is only "aborted" on the last clone of the endpoint, `StaticConfig` was moved into the `Handle`, and it was renamed to `EndpointInner` that is now wrapped in an `Arc`. When the `Arc` is finally dropped, the `Drop` impl on `EndpointInner` is called, which calls `Handle::abort`, if the `Endpoint` was not gracefully closed before dropping. This PR also makes sure that we drop any references to address lookup services on close. ## Breaking Changes New behavioral change! Then `Endpoint` does not do ANY best-effort to close connections gracefully before dropping. This makes it even more important to call `Endpoint::close` in all cases. This change (that prevents the underlying `quinn::Endpoint` to do it's best to wait/send close frames before shutting down) has already highlighted in tests how much more brittle it is to drop the endpoint without calling `close` first. ## Change checklist <!-- Remove any that are not relevant. --> - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. Co-authored-by: “ramfox” <“kasey@n0.computer”> Co-authored-by: Franz Heinzmann <frando@unbiskant.org>
less net work for networks
What is iroh?
Iroh gives you an API for dialing by public key. You say “connect to that phone”, iroh will find & maintain the fastest connection for you, regardless of where it is.
Hole-punching
The fastest route is a direct connection, so if necessary, iroh tries to hole-punch. Should this fail, it can fall back to an open ecosystem of public relay servers. To ensure these connections are as fast as possible, we continuously measure iroh.
Built on QUIC
Iroh uses Quinn to establish QUIC connections between endpoints. This way you get authenticated encryption, concurrent streams with stream priorities, a datagram transport and avoid head-of-line-blocking out of the box.
Compose Protocols
Use pre-existing protocols built on iroh instead of writing your own:
- iroh-blobs for BLAKE3-based content-addressed blob transfer scaling from kilobytes to terabytes
- iroh-gossip for establishing publish-subscribe overlay networks that scale, requiring only resources that your average phone can handle
- iroh-docs for an eventually-consistent key-value store of iroh-blobs blobs
- iroh-willow for an (in-construction) implementation of the willow protocol
Getting Started
Rust Library
It's easiest to use iroh from rust.
Install it using cargo add iroh, then on the connecting side:
const ALPN: &[u8] = b"iroh-example/echo/0";
let endpoint = Endpoint::bind().await?;
// Open a connection to the accepting endpoint
let conn = endpoint.connect(addr, ALPN).await?;
// Open a bidirectional QUIC stream
let (mut send, mut recv) = conn.open_bi().await?;
// Send some data to be echoed
send.write_all(b"Hello, world!").await?;
send.finish()?;
// Receive the echo
let response = recv.read_to_end(1000).await?;
assert_eq!(&response, b"Hello, world!");
// As the side receiving the last application data - say goodbye
conn.close(0u32.into(), b"bye!");
// Close the endpoint and all its connections
endpoint.close().await;
And on the accepting side:
let endpoint = Endpoint::bind().await?;
let router = Router::builder(endpoint)
.accept(ALPN.to_vec(), Arc::new(Echo))
.spawn()
.await?;
// The protocol definition:
#[derive(Debug, Clone)]
struct Echo;
impl ProtocolHandler for Echo {
async fn accept(&self, connection: Connection) -> Result<()> {
let (mut send, mut recv) = connection.accept_bi().await?;
// Echo any bytes received back directly.
let bytes_sent = tokio::io::copy(&mut recv, &mut send).await?;
send.finish()?;
connection.closed().await;
Ok(())
}
}
The full example code with more comments can be found at echo.rs.
Or use one of the pre-existing protocols, e.g. iroh-blobs or iroh-gossip.
Other Languages
If you want to use iroh from other languages, make sure to check out iroh-ffi, the repository for FFI bindings.
Links
Repository Structure
This repository contains a workspace of crates:
iroh: The core library for hole-punching & communicating with relays.iroh-relay: The relay server implementation. This is the code we run in production (and you can, too!).iroh-base: Common types likeHash, key types orRelayUrl.iroh-dns-server: DNS server implementation powering then0_discoveryfor EndpointIds, running at dns.iroh.link.iroh-net-report: Analyzes your host's networking ability & NAT.
License
Copyright 2025 N0, INC.
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.