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.
pull/6121/head
Matt Keeler 5 years ago committed by GitHub
parent 3f3cd9a164
commit de23af071a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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()
}
Loading…
Cancel
Save