fix: try to avoid potential race condition
in some situations it could try to write to a logger that doesnt exist anymore. no logging but better than an race condition.
This commit is contained in:
parent
c4f3b09ee6
commit
0679e992c7
@ -84,7 +84,7 @@ func (d *Dispatcher) Start() {
|
|||||||
err := j.work(j.ctx)
|
err := j.work(j.ctx)
|
||||||
select {
|
select {
|
||||||
case <-j.ctx.Done():
|
case <-j.ctx.Done():
|
||||||
d.log.V(1).Info("received job return after canceled context", "worker", i, "return", err)
|
return
|
||||||
default:
|
default:
|
||||||
errChan <- err
|
errChan <- err
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
package workgroups_test
|
package workgroups_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -15,7 +14,6 @@ import (
|
|||||||
|
|
||||||
"github.com/go-logr/stdr"
|
"github.com/go-logr/stdr"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/tonglil/buflogr"
|
|
||||||
"go.xsfx.dev/workgroups"
|
"go.xsfx.dev/workgroups"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -195,44 +193,3 @@ func TestRetry(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrChanNotUsed(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
log := buflogr.NewWithBuffer(&buf)
|
|
||||||
require := require.New(t)
|
|
||||||
work := func(ctx context.Context) error {
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
d, ctx := workgroups.NewDispatcher(ctx, log, runtime.GOMAXPROCS(0), 1)
|
|
||||||
d.Start()
|
|
||||||
d.Append(workgroups.NewJob(ctx, work))
|
|
||||||
d.Close()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(time.Second / 2)
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
|
|
||||||
err := d.Wait()
|
|
||||||
require.ErrorIs(err, context.Canceled)
|
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
|
|
||||||
// Breaking glass!
|
|
||||||
s := log.GetSink()
|
|
||||||
|
|
||||||
underlier, ok := s.(buflogr.Underlier)
|
|
||||||
if !ok {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
bl := underlier.GetUnderlying()
|
|
||||||
|
|
||||||
bl.Mutex().Lock()
|
|
||||||
require.Contains(buf.String(), "received job return after canceled context")
|
|
||||||
bl.Mutex().Unlock()
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user