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 3a4f595 commit e7da2ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 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: 26 additions & 7 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,7 +40,9 @@ 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
}
Expand All @@ -52,23 +58,36 @@ func (bt *beater) Run(b *beat.Beat) error {

lis, err := net.Listen("tcp", bt.config.Host)
if err != nil {
logp.Err("failed to listen: %s", err)
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 {
logp.Info("Listener stopped: %s", err.Error())
bt.logger.Infof("Listener stopped: %s", err.Error())
return nil
}
return err
}

// Graceful shutdown
func (bt *beater) Stop() {
logp.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 e7da2ff

Please sign in to comment.