From de23af071a93260ba76096c7084c23efa6eb17c9 Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Fri, 12 Jul 2019 10:10:48 -0400 Subject: [PATCH] Move ctx and cancel func setup into the Replicator.Start (#6115) Previously a sequence of events like: Start Stop Start Stop would segfault on the second stop because the original ctx and cancel func were only initialized during the constructor and not during Start. --- agent/consul/replication.go | 5 ++--- agent/consul/replication_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 agent/consul/replication_test.go diff --git a/agent/consul/replication.go b/agent/consul/replication.go index 789df61e65..a6b7ad0d94 100644 --- a/agent/consul/replication.go +++ b/agent/consul/replication.go @@ -61,7 +61,6 @@ func NewReplicator(config *ReplicatorConfig) (*Replicator, error) { if config.Logger == nil { config.Logger = log.New(os.Stderr, "", log.LstdFlags) } - ctx, cancel := context.WithCancel(context.Background()) limiter := rate.NewLimiter(rate.Limit(config.Rate), config.Burst) maxWait := config.MaxRetryWait @@ -77,8 +76,6 @@ func NewReplicator(config *ReplicatorConfig) (*Replicator, error) { return &Replicator{ name: config.Name, running: false, - cancel: cancel, - ctx: ctx, limiter: limiter, waiter: waiter, replicate: config.ReplicateFn, @@ -94,6 +91,8 @@ func (r *Replicator) Start() { return } + r.ctx, r.cancel = context.WithCancel(context.Background()) + go r.run() r.running = true diff --git a/agent/consul/replication_test.go b/agent/consul/replication_test.go new file mode 100644 index 0000000000..28b6c579d9 --- /dev/null +++ b/agent/consul/replication_test.go @@ -0,0 +1,28 @@ +package consul + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestReplicationRestart(t *testing.T) { + config := ReplicatorConfig{ + Name: "mock", + ReplicateFn: func(ctx context.Context, lastRemoteIndex uint64) (uint64, bool, error) { + return 1, false, nil + }, + Rate: 1, + Burst: 1, + } + + repl, err := NewReplicator(&config) + require.NoError(t, err) + + repl.Start() + repl.Stop() + repl.Start() + // Previously this would have segfaulted + repl.Stop() +}