Skip to content

Commit

Permalink
Ensure ports are freed on error when dialing.
Browse files Browse the repository at this point in the history
  • Loading branch information
志宇 committed Mar 6, 2020
1 parent 98ae279 commit 9a8708b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/app/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down

0 comments on commit 9a8708b

Please sign in to comment.