diff --git a/beater/beater.go b/beater/beater.go index 35de9b57036..b5616384cdf 100644 --- a/beater/beater.go +++ b/beater/beater.go @@ -198,9 +198,12 @@ func (bt *beater) start(ctx context.Context, cancelContext context.CancelFunc, b if err != nil { return nil, err } - bt.stopServer = func() { + go func() { defer close(done) defer closeTracer() + s.Wait() + }() + bt.stopServer = func() { if bt.config.ShutdownTimeout > 0 { time.AfterFunc(bt.config.ShutdownTimeout, cancelContext) } @@ -316,11 +319,18 @@ func (s *serverRunner) String() string { return "APMServer" } +// Stop stops the server. func (s *serverRunner) Stop() { s.stopOnce.Do(s.cancelRunServerContext) + s.Wait() +} + +// Wait waits for the server to stop. +func (s *serverRunner) Wait() { s.wg.Wait() } +// Start starts the server. func (s *serverRunner) Start() { s.wg.Add(1) go func() {