Commit Graph

214 Commits

Author SHA1 Message Date
Marcin Rataj bcc2bf1866 fix(basic_host): set read deadline before multistream Close to prevent blocking
streamWrapper.Close() can block indefinitely when the remote peer is slow
or unresponsive during the multistream-select handshake completion.

The lazy multistream protocol negotiation defers reading the handshake
response until Close() is called. If the remote peer doesn't respond,
the read blocks forever, causing goroutine leaks.

This is particularly problematic for bitswap servers where taskWorkers
can get stuck trying to close streams after sending blocks.

The fix sets a read deadline (using DefaultNegotiationTimeout) before
calling the multistream Close(), ensuring the operation will time out
rather than block indefinitely.

Related: https://github.com/multiformats/go-multistream/issues/47
Related: https://github.com/multiformats/go-multistream/pull/48
2026-01-07 17:11:44 -08:00
sukun 3e69227598 basichost: move EvtLocalAddrsChanged to addrs_manager (#3355) 2025-09-30 16:20:38 +05:30
Marco Munizaga da810a1764 feat: Migrate to log/slog (#3364) 2025-08-25 14:00:01 -07:00
sukun 90b841260e basichost: move observed address manager to basichost (#3332) 2025-08-18 12:16:16 -07:00
sukun fc4a618881 swarm: move AddCertHashes to swarm (#3330) 2025-08-07 22:41:24 +05:30
sukun 26a5710d35 basichost: fix deadlock with addrs_manager (#3348) 2025-07-30 17:08:12 +05:30
sukun 8e8f76abf6 basichost: fix Addrs docstring (#3341) 2025-07-28 20:04:04 +05:30
sukun 2d45a88295 autonatv2: add metrics (#3308) 2025-06-12 21:48:16 +05:30
Piotr Galar 335c75a398 Merge branch 'master' into uci-golangci-lint 2025-06-11 10:42:45 +01:00
galargh 50f065b4df chore: fix revive issues 2025-06-11 10:00:47 +01:00
sukun dbf7e1b972 autonatv2: add Unknown addrs to event (#3305)
Providing unknown addresses to the user, helps them infer whether autonatv2 has any more addresses it might confirm.

There's no upside to not sending this information.

This also changes host.ConfirmedAddrs to return all the three categories, Reachable, Unreachable, and Unknown addrs.
2025-06-10 21:02:18 +05:30
sukun fb1d9512e8 basichost: use autonatv2 to verify reachability (#3231)
This introduces addrsReachabilityTracker that tracks reachability on
a set of addresses. It probes reachability for addresses periodically
and has an exponential backoff in case there are too many errors
or we don't have any valid autonatv2 peer.

There's no smartness in the address selection logic currently. We just
test all provided addresses. It also doesn't use the addresses provided
by `AddrsFactory`, so currently there's no way to get a user provided
address tested for reachability, something that would be a problem for
dns addresses. I intend to introduce an alternative to
`AddrsFactory`, something like, `AnnounceAddrs(addrs []ma.Multiaddr)`
that's just appended to the set of addresses that we have, and check
reachability for those addresses.

There's only one method exposed in the BasicHost right now that's
`ReachableAddrs() []ma.Multiadd`r that returns the host's reachable
addrs. Users can also use the event `EvtHostReachableAddrsChanged`
to be notified when any addrs reachability changes.
2025-06-03 17:13:56 +05:30
sukun 5e6f217d84 addrsmanager: extract out addressing logic from basichost (#3075)
Benchmark for AllAddrs:

```
goos: linux
goarch: amd64
pkg: github.com/libp2p/go-libp2p
cpu: AMD Ryzen 7 7840U w/ Radeon  780M Graphics
BenchmarkAllAddrs-16               16737            122245 ns/op           21240 B/op        218 allocs/op
```
after:
```
goos: linux
goarch: amd64
pkg: github.com/libp2p/go-libp2p
cpu: AMD Ryzen 7 7840U w/ Radeon  780M Graphics
BenchmarkAllAddrs-16            11103236               105.7 ns/op           192 B/op          1 allocs/op
```
2025-02-27 22:24:57 +05:30
Marco Munizaga 785181fbb8 chore: Update go-multiaddr to v0.15 (#3145) 2025-02-24 14:21:47 -08:00
sukun f88beca8d5 basichost: add certhashes to addrs in place (#3200) 2025-02-18 13:19:00 -08:00
sukun 914331ba90 autorelay: send addresses on eventbus; dont wrap address factory (#3071) 2025-02-18 18:36:17 +05:30
sukun 02ab795c92 Implement error codes spec (#2927) 2025-02-09 21:39:18 +05:30
Ivan Shvedunov 8423de3a64 basichost: avoid modifying slice returned by AddrsFactory (#3068) 2024-11-28 22:02:47 +05:30
Marco Munizaga c31f093851 fix: basichost: Use NegotiationTimeout as fallback timeout for NewStream (#3020) 2024-11-04 09:42:44 -08:00
sukun b9cb861a98 autonat: fix interaction with autorelay (#2967)
* autonat: fix interaction with autorelay

* Fix race in test

* Use deadline from context if available for DialBack

* Return hasNewAddrs correctly

* nit: cleanup contains check

* Shuffle peers

* nits

* Change comment to indicate the bug

* holepuncher: pass address function in constructor (#2979)

* holepunch: pass address function in constructor

* nit

* Remove getPublicAddrs

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>

* Make a copy of the multiaddr slice in Addrs()

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-10-16 20:08:43 -07:00
Marco Munizaga e8b6685edb fix: swarm: refactor address resolution (#2990)
* Remove unused resolver in basic host

* Refactor Swarm.resolveAddrs

Refactors how DNS Address resolution works.

* lint

* Move MultiaddrDNSResolver interface to core

* Reserve output space for addresses left to resolve

* feat: core/transport: Add SkipResolver interface (#2989)

* Rebase on top of resolveAddrs refactor

* Add comments

* Sanitize address inputs when returning a reservation message (#3006)
2024-10-16 12:20:21 -07:00
Marco Munizaga c79cf3653d Add backoff for updating local IP addresses on error (#2999) 2024-10-16 12:12:51 -07:00
sukun c2556325d5 basichost: ensure no duplicates in Addrs output (#2980) 2024-10-03 10:43:59 +05:30
Marco Munizaga 9038a72c48 peerstore: better GC in membacked peerstore (#2960)
---------

Co-authored-by: sukun <sukunrt@gmail.com>
2024-09-17 23:49:53 +05:30
sukun 8a11b7c8e4 basic_host: close swarm on Close (#2916)
Using the `BasicHost` constructor transfers the ownership of the swarm.
This is similar to how using `libp2p.New` transfers the ownership of
user provided config options like `ResourceManager`, all of which are
closed on `host.Close`
2024-08-15 18:18:35 +05:30
Marco Munizaga 0cc0b2fba7 ObsAddrManager: Infer external addresses for transports that share the same listening address. (#2892)
* Remove this hack, the new observed address manager just works

* Infer addresses that share the same local thin waist

* Use Interface listen address as well
2024-08-01 16:05:04 -07:00
sukun f80d18f24c config: fix AddrFactory for AutoNAT (#2868)
---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-07-31 16:56:32 +05:30
sukun 76b266558b basichost: reset stream if SetProtocol fails (#2875) 2024-07-18 17:38:25 +05:30
sukun c88d0a0d89 basichost: reset new stream if rcmgr blocks (#2869) 2024-07-11 22:52:41 +05:30
sukun ee0ac60d3f autonatv2: add server metrics for dial requests (#2848) 2024-07-02 16:00:52 +05:30
sukun 87c435512c autonatv2: implement autonatv2 spec (#2469) 2024-06-21 14:55:22 +05:30
sukun 6861cecb3c identify: refactor observed address manager to do address mapping at thin waist(IP+TCP/UDP) layer (#2793)
* refactor observed address manager to do mapping at thin waist layer

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>

* restrict output message size, add top level option to disable address
discovery

* Comment nit

* Increase maxPeerRecordSize

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-05-15 20:17:25 -07:00
sukun 01ce97c730 webrtc: add webrtc addresses to host normalizer (#2784) 2024-05-08 15:52:01 +05:30
Steven Allen af0161e83e Add a "Limited" network connectivity state (#2696)
Rename the Transient state on connection to Limited. This is more appropriate and also 
doesn't conflict with the transient resource manager scope.

Adds a Limited connectedness state for peers connected to us over Limited connections. 
This allows  users to ignore such peers if they are interested in only peers connected to 
us over Unlimited connections. 

For some peers who disconnect before we sent a Connectedness event, we will now 
only send a Disconnected event. 
---------

Co-authored-by: guillaumemichel <guillaume@michel.id>
Co-authored-by: sukun <sukunrt@gmail.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-05-08 15:51:22 +05:30
sukun 0385ec924b basichost: append certhash for webrtc addresses provided via address factory (#2774) 2024-04-23 17:12:01 +05:30
Marten Seemann 9d149fa3cb use Fx to start and stop the host, swarm, autorelay and quicreuse (#2118)
* config: refactor AutoNAT construction into separate method

* config: use a lifecycle hook to start listening on swarm addresses

* use Fx to construct the host

* add a test for constructing a routed host

* use Fx hooks to start the host

* config: use Fx lifecycle hooks to start AutoRelay and for PeerRouting

* basichost: don't close the swarm

The swarm is not constructed by the basic host, thus is shouldn't be
closed by it.

* config: use Fx hook to close the quicreuse connection manager

* test for goroutine leaks when starting/stopping fx

To do this, I've had to move a few leaky tests into a separate package.
I've filed a bug for the AutoNAT issue (#2743) but the "error on
startup" issue is going to require some pretty invasive changes (we need
to construct _then_ start).

* go fmt

* Ignore one more top function

* Typo

* Ignore any not top

---------

Co-authored-by: Sukun <sukunrt@gmail.com>
Co-authored-by: Steven Allen <steven@stebalien.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-03-21 10:18:34 -07:00
zhiqiangxu e416c52172 basichost: log more info when protocol selection fails (#2734) 2024-03-21 09:08:25 +05:30
shuoer86 3089ac3061 chore: fix typos (#2608)
Co-authored-by: Sukun <sukunrt@gmail.com>
2023-11-18 15:03:52 +03:00
Marten Seemann 284469128b pstoremanager: fix race condition when removing peers from peer store (#2644) 2023-11-17 15:22:06 +07:00
Sukun f5493140ff swarm: cleanup stream handler goroutine (#2610) 2023-10-19 21:09:40 -07:00
Marten Seemann dce942454b basichost: handle the SetProtocol error in NewStream (#2555) 2023-09-04 17:03:24 +05:30
Jorropo 2398ba4fac cleanup: add continue in case of failure in the (*BasicHost).Addrs certhash loop (#2492)
This does not do change anything because the transport always returns the same multiaddr you passed in in case of failure, it just looks wrong and relies on a obscure detail of the webtransport's AddCertHashes function, while this does not.
2023-08-16 09:24:41 -07:00
Jorropo d46e631f8b basichost: use byte representation of WebTransport multiaddr as map key (#2494) 2023-08-16 06:20:06 -07:00
Sukun 260b9695ca basichost: remove invalid comment (#2435) 2023-07-20 00:19:03 +05:30
Marten Seemann 63fdb48cfb update go-multiaddr to v0.10.1, use Unique function from there (#2407) 2023-07-11 01:50:50 +05:30
VM 8e341f7936 docs: fix some comments (#2391)
Co-authored-by: DylanYong <dylan.y@nodereal.io>
2023-06-28 10:12:31 -07:00
Hlib Kanunnikov e5334ed296 basichost / blankhost: wrap errors (#2331) 2023-06-27 11:21:32 -07:00
Sukun f87f6dbca8 nat: add HasDiscoveredNAT method for checking NAT environments (#2358)
* nat: add HasNAT method for checking NAT environments

* rename to better variable
2023-06-12 11:01:17 -07:00
Sukun 73c34f98da holepunch: add metrics (#2246)
* holepunch: add metrics

* don't track addresses

* add cancelled and no_suitable_address state

* separate holepunch dashbords by transport

* fix dashboard fields

* add instance filter to dashboard

* initialise metrics labels with 0

* fix panel names in dashboard

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
2023-06-05 05:41:26 -07:00
Marten Seemann 0f9ad8c242 revert "feat:add contexts to all peerstore methods (#2312)" (#2328) 2023-06-03 16:55:08 +03:00