file.d icon indicating copy to clipboard operation
file.d copied to clipboard

Bug: Potential deadlock in case of events starvation

Open vadimalekseev opened this issue 1 year ago • 0 comments

Subject code here – in usual case, we wait for the signal of new event. But it may happen that all events are back in the pool before the Wait call.

This test can reproduce this beheivior:

func TestDeadLock(t *testing.T) {
	const (
		poolCapacity = 32
		concurrency  = 64
	)
	pool := newEventPool(poolCapacity, DefaultAvgInputEventSize)
	for i := 0; i < 100_000; i++ {
		fmt.Println("new test iteration", i)
		wg := new(sync.WaitGroup)
		wg.Add(concurrency)
		for i := 0; i < concurrency; i++ {
			go func() {
				defer wg.Done()
				e := pool.get()
				runtime.Gosched()
				pool.back(e)
			}()
		}
		wg.Wait()
	}
}

Solution: We have to run a goroutine that will force the Broadcast/Signal every timer tick. I'll send PR by the end of the week.

vadimalekseev avatar Oct 17 '24 19:10 vadimalekseev