• Kirill Smelkov's avatar
    sync.WorkGroup: Fix deadlock thinko in tests · b8b042c5
    Kirill Smelkov authored
    sync.WorkGroup test was doing ctx.done() wait from under test mutex,
    something like
    
        def _(ctx, i):
            with mu:
                ...
                if i == 0:
                    raise RuntimeError()    # to cause ctx cancel
                ctx.done().recv()           # i=1 -> wait till ctx is canceled
    
    but it can be a deadlock if T(i=1) runs first and enters
    ctx.done().recv() before T(i=0) is run - then T(i=0) will block forever
    waiting to lock mu.
    
    This failure was not seen so far, probably because the time to go a new
    thread/goroutine is relatively high. However one of upcoming patches,
    where go is made faster, revealed this problem and, without the fix,
    sync.WorkGroup test was regularly deadlocking.
    
    The problem was there from sync.WorkGroup beginning - from
    9ee7ba91 (sync += WorkGroup).
    
    Fix the deadlock by waiting for ctx.done() outside of mu.
    b8b042c5
sync_test.py 5.1 KB