How to use `withCheckedContinuation` with an async closure?

This looks good, but I think there might be a race between the two Mutexs:

Task 1 goes into push, does not find a continuation and is now in line 368 but before acquiring the _buffer lock.

At the same time Task 2 goes into next(), it acquires the _buffer lock, does not find anything in the buffer and leaves the lock again. Now Task 1 will acquire the _buffer lock and write the invocation into the buffer. Task 2 now acquires the _continuation lock and stores the continuation.

In the end you have one item in the buffer and a stored waiter continuation but they failed to meet each other. :broken_heart:

I think if you combine the _continuation and _buffer behind a single mutex, you can avoid this situation.

1 Like