satellite/metainfo: close loop separately to avoid logical races (#3100)

This commit is contained in:
Egon Elbre 2019-09-23 22:14:39 +03:00 committed by GitHub
parent 3647fd7b37
commit 4bd1ce868f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 5 deletions

View File

@ -121,8 +121,6 @@ func (loop *Loop) Join(ctx context.Context, observer Observer) (err error) {
func (loop *Loop) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
defer close(loop.done)
for {
err := loop.runOnce(ctx)
if err != nil {
@ -131,6 +129,12 @@ func (loop *Loop) Run(ctx context.Context) (err error) {
}
}
// Close closes the looping services.
func (loop *Loop) Close() (err error) {
close(loop.done)
return nil
}
// runOnce goes through metainfo one time and sends information to observers.
func (loop *Loop) runOnce(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)

View File

@ -223,6 +223,7 @@ func TestLoopCancel(t *testing.T) {
metaLoop := metainfo.NewLoop(metainfo.LoopConfig{
CoalesceDuration: 1 * time.Second,
}, satellite.Metainfo.Service)
// create a cancelable context to pass into metaLoop.Run
loopCtx, cancel := context.WithCancel(ctx)
@ -270,6 +271,9 @@ func TestLoopCancel(t *testing.T) {
err := group.Wait()
require.NoError(t, err)
err = metaLoop.Close()
require.NoError(t, err)
obs3 := newTestObserver(nil)
err = metaLoop.Join(ctx, obs3)
require.Error(t, err)

View File

@ -670,6 +670,9 @@ func (peer *Peer) Run(ctx context.Context) (err error) {
group, ctx := errgroup.WithContext(ctx)
group.Go(func() error {
return errs2.IgnoreCanceled(peer.Metainfo.Loop.Run(ctx))
})
group.Go(func() error {
return errs2.IgnoreCanceled(peer.Version.Run(ctx))
})
@ -679,9 +682,6 @@ func (peer *Peer) Run(ctx context.Context) (err error) {
group.Go(func() error {
return errs2.IgnoreCanceled(peer.Repair.Checker.Run(ctx))
})
group.Go(func() error {
return errs2.IgnoreCanceled(peer.Metainfo.Loop.Run(ctx))
})
group.Go(func() error {
return errs2.IgnoreCanceled(peer.Repair.Repairer.Run(ctx))
})
@ -788,6 +788,9 @@ func (peer *Peer) Close() error {
if peer.Overlay.Service != nil {
errlist.Add(peer.Overlay.Service.Close())
}
if peer.Metainfo.Loop != nil {
errlist.Add(peer.Metainfo.Loop.Close())
}
return errlist.Err()
}