Skip to content

Commit 21081f1

Browse files
committed
fix: remove duplicate listen addresses when supplied to avoid muxer panic
1 parent 8e8f76a commit 21081f1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

config/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,26 @@ func (cfg *Config) validate() error {
475475
return errors.New("cannot use shared TCP listener with PSK")
476476
}
477477

478+
// check for duplicate listen addresses and remove them
479+
listenAddresses := cfg.ListenAddrs
480+
// Remove duplicates while keeping the first occurrence of each address
481+
seen := make(map[string]bool)
482+
listenAddresses = slices.DeleteFunc(listenAddresses, func(addr ma.Multiaddr) bool {
483+
addrStr := addr.String()
484+
if seen[addrStr] {
485+
return true // Remove this duplicate
486+
}
487+
seen[addrStr] = true
488+
return false // Keep this first occurrence
489+
})
490+
cfg.ListenAddrs = listenAddresses
478491
return nil
479492
}
480493

481494
// NewNode constructs a new libp2p Host from the Config.
482495
//
483496
// This function consumes the config. Do not reuse it (really!).
484497
func (cfg *Config) NewNode() (host.Host, error) {
485-
486498
validateErr := cfg.validate()
487499
if validateErr != nil {
488500
if cfg.ResourceManager != nil {

0 commit comments

Comments
 (0)