Skip to content

Commit

Permalink
fix race on shutdown (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbyte73 authored May 8, 2024
1 parent 40832fa commit 582ccba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/service/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *Process) Gather() ([]*dto.MetricFamily, error) {
// Get the metrics from the handler via IPC
metricsResponse, err := p.ipcHandlerClient.GetMetrics(context.Background(), &ipc.MetricsRequest{})
if err != nil {
logger.Warnw("failed to obtain metrics from handler", err, "egress_id", p.req.EgressId)
logger.Warnw("failed to obtain metrics from handler", err, "egressID", p.req.EgressId)
return make([]*dto.MetricFamily, 0), nil // don't return an error, just skip this handler
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,17 @@ func (s *Service) killProcess(egressID string, maxUsage float64) {
}

func (s *Service) Close() {
for s.GetRequestCount() > 0 {
for {
s.mu.RLock()
isIdle := len(s.activeHandlers) == 0
s.mu.RUnlock()

if isIdle {
logger.Infow("closing server")
s.psrpcServer.Shutdown()
return
}

time.Sleep(time.Second)
}
logger.Infow("closing server")
s.psrpcServer.Shutdown()
}
1 change: 1 addition & 0 deletions pkg/service/service_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (s *Service) AddHandler(egressID string, p *Process) error {
}()

case <-time.After(10 * time.Second):
logger.Warnw("no response from handler", nil, "egressID", egressID)
_ = p.cmd.Process.Kill()
s.processEnded(p, errors.ErrEgressNotFound)
}
Expand Down

0 comments on commit 582ccba

Please sign in to comment.