diff --git a/agent/consul/replication.go b/agent/consul/replication.go index 789df61e6..a6b7ad0d9 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 000000000..28b6c579d --- /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() +}