Skip to content

Commit

Permalink
Fixed possible panics of transport re-establishment.
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed Jun 13, 2019
1 parent 2975549 commit df4a2d7
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transport
import (
"context"
"errors"
"io"
"math/big"
"strings"
"sync"
Expand Down Expand Up @@ -241,21 +242,17 @@ func (tm *Manager) CreateTransport(ctx context.Context, remote cipher.PubKey, tp
// DeleteTransport disconnects and removes the Transport of Transport ID.
func (tm *Manager) DeleteTransport(id uuid.UUID) error {
tm.mu.Lock()
tr := tm.transports[id]
delete(tm.transports, id)
if tr, ok := tm.transports[id]; ok {
delete(tm.transports, id)
_ = tr.Close() //nolint:errcheck
}
tm.mu.Unlock()

tr.Close()

if _, err := tm.config.DiscoveryClient.UpdateStatuses(context.Background(), &Status{ID: id, IsUp: false}); err != nil {
tm.Logger.Warnf("Failed to change transport status: %s", err)
}

tm.Logger.Infof("Unregistered transport %s", id)
if tr != nil {
return tr.Close()
}

return nil
}

Expand Down Expand Up @@ -330,11 +327,13 @@ func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tp
tm.transports[entry.ID] = mTr
tm.mu.Unlock()

tm.TrChan <- mTr

go tm.manageTransport(ctx, mTr, factory, remote, public, false)

return mTr, nil
select {
case <-tm.doneChan:
return nil, io.ErrClosedPipe
case tm.TrChan <- mTr:
go tm.manageTransport(ctx, mTr, factory, remote, public, false)
return mTr, nil
}
}

func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*ManagedTransport, error) {
Expand Down Expand Up @@ -371,11 +370,13 @@ func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (*Manag
tm.transports[entry.ID] = mTr
tm.mu.Unlock()

tm.TrChan <- mTr

go tm.manageTransport(ctx, mTr, factory, remote, true, true)

return mTr, nil
select {
case <-tm.doneChan:
return nil, io.ErrClosedPipe
case tm.TrChan <- mTr:
go tm.manageTransport(ctx, mTr, factory, remote, true, true)
return mTr, nil
}
}

func (tm *Manager) walkEntries(walkFunc func(*Entry) bool) *Entry {
Expand Down

0 comments on commit df4a2d7

Please sign in to comment.