Skip to content

Commit

Permalink
Merge branch 'main' into bump-to-avalanchego-1.7.11
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed May 28, 2022
2 parents 4f0052d + 3a00f48 commit 44fda79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 15 additions & 4 deletions server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ type localNetwork struct {
customVMsReadyChCloseOnce sync.Once
customVMRestartMu *sync.RWMutex

stopCh chan struct{}
startDoneCh chan struct{}
startErrCh chan error
stopCh chan struct{}
startDoneCh chan struct{}
startErrCh chan error
startCtxCancel context.CancelFunc // allow the Start context to be cancelled

stopOnce sync.Once
}
Expand Down Expand Up @@ -252,11 +253,17 @@ func createConfigFileString(configFileMap map[string]interface{}, logDir string,
return string(finalJSON), nil
}

func (lc *localNetwork) start(ctx context.Context) {
func (lc *localNetwork) start(argCtx context.Context) {
defer func() {
close(lc.startDoneCh)
}()

// start triggers a series of different time consuming actions
// (in case of subnets: create a wallet, create subnets, issue txs, etc.)
// We may need to cancel the context, for example if the client hits Ctrl-C
var ctx context.Context
ctx, lc.startCtxCancel = context.WithCancel(argCtx)

color.Outf("{{blue}}{{bold}}create and run local network{{/}}\n")
nw, err := local.NewNetwork(lc.logger, lc.cfg, lc.options.rootDataDir, lc.options.snapshotsDir)
if err != nil {
Expand Down Expand Up @@ -422,6 +429,10 @@ func (lc *localNetwork) updateNodeInfo() error {
func (lc *localNetwork) stop(ctx context.Context) {
lc.stopOnce.Do(func() {
close(lc.stopCh)
// cancel possible concurrent still running start
if lc.startCtxCancel != nil {
lc.startCtxCancel()
}
serr := lc.nw.Stop(ctx)
<-lc.startDoneCh
color.Outf("{{red}}{{bold}}terminated network{{/}} (error %v)\n", serr)
Expand Down
16 changes: 11 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (s *server) Run(rootCtx context.Context) (err error) {
s.gRPCServer.Stop()
zap.L().Warn("closed gRPC server")
<-gRPCErrc
zap.L().Warn("gRPC terminated")

case err = <-gRPCErrc:
zap.L().Warn("gRPC server failed", zap.Error(err))
Expand Down Expand Up @@ -749,19 +750,24 @@ func (s *server) RestartNode(ctx context.Context, req *rpcpb.RestartNodeRequest)

func (s *server) Stop(ctx context.Context, req *rpcpb.StopRequest) (*rpcpb.StopResponse, error) {
zap.L().Debug("received stop request")
info := s.getClusterInfo()
if info == nil {
return nil, ErrNotBootstrapped
}

s.mu.Lock()
defer s.mu.Unlock()

if s.network == nil {
return nil, ErrNotBootstrapped
}

info := s.clusterInfo
if info == nil {
info = &rpcpb.ClusterInfo{}
}

s.network.stop(ctx)
s.network = nil
info.Healthy = false
s.clusterInfo = nil

info.Healthy = false
return &rpcpb.StopResponse{ClusterInfo: info}, nil
}

Expand Down

0 comments on commit 44fda79

Please sign in to comment.