Skip to content

Commit

Permalink
Fix panic when a signal is delivered before the server is instantiated (
Browse files Browse the repository at this point in the history
elastic#580)

Introduce a flag and a mutex in the beater so that we can't stop the server before assigning it (ie. read before write)
fixes elastic#514
  • Loading branch information
jalvz authored and Juan A committed Feb 1, 2018
1 parent cbfcf9b commit 8ff89d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ https://github.com/elastic/apm-server/compare/71df0d96445df35afe27f38bcf734a0828
==== Bug fixes
- Updated systemd doc url {pull}354[354]
- Updated readme doc urls {pull}356[356]
- Use updated stack trace frame values for calculating error `grouping_keys` {pull}485[485]
- Use updated stack trace frame values for calculating error `grouping_keys` {pull}485[485]
- Fix panic when a signal is delivered before the server is instantiated {pull}580[580]

==== Added
- Include build time and revision in version information {pull}396[396]
Expand Down
33 changes: 25 additions & 8 deletions beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import (
"net"
"net/http"
"regexp"
"sync"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)

type beater struct {
config *Config
server *http.Server
config *Config
mutex sync.Mutex // guards server and stopped
server *http.Server
stopped bool
logger *logp.Logger
}

// Creates beater
Expand All @@ -36,14 +40,15 @@ func New(b *beat.Beat, ucfg *common.Config) (beat.Beater, error) {
}

bt := &beater{
config: beaterConfig,
config: beaterConfig,
stopped: false,
logger: logp.NewLogger("beater"),
}
return bt, nil
}

func (bt *beater) Run(b *beat.Beat) error {
var err error
logger := logp.NewLogger("beater")

pub, err := newPublisher(b.Publisher, bt.config.ConcurrentRequests)
if err != nil {
Expand All @@ -53,24 +58,36 @@ func (bt *beater) Run(b *beat.Beat) error {

lis, err := net.Listen("tcp", bt.config.Host)
if err != nil {
logger.Errorf("failed to listen: %s", err.Error())
bt.logger.Errorf("failed to listen: %s", err.Error())

return err
}
go notifyListening(bt.config, pub.Send)

bt.mutex.Lock()
if bt.stopped {
defer bt.mutex.Unlock()
return nil
}

bt.server = newServer(bt.config, pub.Send)
bt.mutex.Unlock()

err = run(bt.server, lis, bt.config)
if err == http.ErrServerClosed {
logger.Infof("Listener stopped: %s", err.Error())
bt.logger.Infof("Listener stopped: %s", err.Error())
return nil
}
return err
}

// Graceful shutdown
func (bt *beater) Stop() {
logp.NewLogger("beater").Info("stopping apm-server...")
stop(bt.server, bt.config.ShutdownTimeout)
bt.logger.Info("stopping apm-server...")
bt.mutex.Lock()
if bt.server != nil {
stop(bt.server, bt.config.ShutdownTimeout)
}
bt.stopped = true
bt.mutex.Unlock()
}

0 comments on commit 8ff89d8

Please sign in to comment.