From 9a8708b91ae0d465fd4fe7de44467180938c41ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Sat, 7 Mar 2020 00:26:29 +0800 Subject: [PATCH] Ensure ports are freed on error when dialing. --- pkg/app/appnet/skywire_networker.go | 9 ++++++++- pkg/router/router.go | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/app/appnet/skywire_networker.go b/pkg/app/appnet/skywire_networker.go index 5f33d9b69..59c68c450 100644 --- a/pkg/app/appnet/skywire_networker.go +++ b/pkg/app/appnet/skywire_networker.go @@ -44,12 +44,19 @@ func (r *SkywireNetworker) Dial(addr Addr) (net.Conn, error) { } // DialContext dials remote `addr` via `skynet` with context. -func (r *SkywireNetworker) DialContext(ctx context.Context, addr Addr) (net.Conn, error) { +func (r *SkywireNetworker) DialContext(ctx context.Context, addr Addr) (conn net.Conn, err error) { localPort, freePort, err := r.porter.ReserveEphemeral(ctx, nil) if err != nil { return nil, err } + // ensure ports are freed on error. + defer func() { + if err != nil { + freePort() + } + }() + rg, err := r.r.DialRoutes(ctx, addr.PubKey, routing.Port(localPort), addr.Port, router.DefaultDialOptions()) if err != nil { return nil, err diff --git a/pkg/router/router.go b/pkg/router/router.go index 6511d13c1..577e20d20 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -40,6 +40,7 @@ var ( // ErrUnknownPacketType is returned when packet type is unknown. ErrUnknownPacketType = errors.New("unknown packet type") + // ErrRemoteEmptyPK occurs when the specified remote public key is empty. ErrRemoteEmptyPK = errors.New("empty remote public key") )