Skip to content

Commit 9db23cd

Browse files
committed
fix task loop
(cherry picked from commit 23061fb)
1 parent 7eca1f1 commit 9db23cd

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

internal/background/worker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ func (b *Worker) starterLoop() {
135135
for bgTask := range b.tasks {
136136
b.workers.Add(1)
137137

138-
go func() {
138+
go func(task backgroundTask) {
139139
defer b.workers.Done()
140140

141-
pprof.Do(b.ctx, pprof.Labels("background", bgTask.name), bgTask.callback)
142-
}()
141+
pprof.Do(b.ctx, pprof.Labels("background", task.name), task.callback)
142+
}(bgTask)
143143
}
144144
}
145145

internal/background/worker_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ func TestWorkerContext(t *testing.T) {
2323
})
2424

2525
t.Run("Dedicated", func(t *testing.T) {
26-
ctx := context.WithValue(context.Background(), "1", "2")
26+
type ctxkey struct{}
27+
ctx := context.WithValue(context.Background(), ctxkey{}, "2")
2728
w := NewWorker(ctx)
28-
require.Equal(t, "2", w.Context().Value("1"))
29+
require.Equal(t, "2", w.Context().Value(ctxkey{}))
2930
})
3031

3132
t.Run("Stop", func(t *testing.T) {
@@ -96,7 +97,7 @@ func TestWorkerClose(t *testing.T) {
9697

9798
func TestWorkerConcurrentStartAndClose(t *testing.T) {
9899
xtest.TestManyTimes(t, func(t testing.TB) {
99-
targetClose := int64(10000)
100+
targetClose := int64(100)
100101
parallel := 10
101102

102103
var counter int64
@@ -106,13 +107,16 @@ func TestWorkerConcurrentStartAndClose(t *testing.T) {
106107

107108
closeIndex := int64(0)
108109
closed := make(empty.Chan)
110+
109111
go func() {
112+
defer close(closed)
113+
110114
xtest.SpinWaitCondition(t, nil, func() bool {
111115
return atomic.LoadInt64(&counter) > targetClose
112116
})
117+
113118
require.NoError(t, w.Close(ctx, nil))
114119
closeIndex = atomic.LoadInt64(&counter)
115-
close(closed)
116120
}()
117121

118122
stopNewStarts := xatomic.Bool{}
@@ -123,11 +127,9 @@ func TestWorkerConcurrentStartAndClose(t *testing.T) {
123127
return
124128
}
125129

126-
go func() {
127-
w.Start("test", func(ctx context.Context) {
128-
atomic.AddInt64(&counter, 1)
129-
})
130-
}()
130+
w.Start("test", func(ctx context.Context) {
131+
atomic.AddInt64(&counter, 1)
132+
})
131133
}
132134
}()
133135
}

0 commit comments

Comments
 (0)