StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Bug: deadlock happens

Open melnikov77 opened this issue 12 years ago • 4 comments

Sometimes I have deadlocks related to async methods. I think it happens because sometimes async methods finish in "reading" thread. Here is example:

var task1 = _db.SetAddAsync(key1, val1);
var task2 = _db.SetAddAsync(key2, val2);

await Task.WhenAll(new[]{task1, task2}).ConfigureAwait(false);

//if you check current thread name sometimes it has name "computername:Read".
Console.WriteLine(System.Threading.Thread.CurrentThread.Name);

//if this happens the next code will newer finish.
// actually it could be any async call
await _db.SetAddAsync(key2, val3).ConfigureAwait(false);
// if you replace it with sync method the timeout will happen

melnikov77 avatar May 07 '14 19:05 melnikov77

Updates: The code to create deadlock

var task1 = _db.SetAddAsync(key1, val1);
var task2 = _db.SetAddAsync(key2, val2);

await Task.WhenAll(new[]{task1, task2}).ConfigureAwait(false);

// Deadlock happens here regardless if current thread is "reading" thread or not.
// it happesns because we blocks "reading" or "processAsyncCompletionQueue" thread
_db.SetAddAsync(key2, val3).Wait();
// if you replace it with sync method teh deadlock also happens
// but because you have timeout the timeout will happen and deadlock is resolved

It looks like the async methods could be completed just in two threads, "reading" thread and "processAsyncCompletionQueue" thread (when PreserveAsyncOrder is on, see CompleteSyncOrAsync) So if we have some blocking code or long running code in task's continuation function then other db's consumer will be affected.

melnikov77 avatar May 07 '14 22:05 melnikov77

The continuation pattern has changed since this was posted - are you able to repro this on a current version? If so, are you on .Net 4.6+ or prior?

NickCraver avatar Jan 25 '16 12:01 NickCraver

Hi Nick,

We do use patched version where we always execute continuations in ThreadPool's thread i.e. newer execute in reader thread. I saw your fix but it requires .net 4.6 which we cannot use right now.

Thanks, Sergey

On Mon, Jan 25, 2016 at 4:22 AM, Nick Craver [email protected] wrote:

The continuation pattern has changed since this was posted - are you able to repro this on a current version? If so, are you on .Net 4.6+ or prior?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/36#issuecomment-174489314 .

melnikov77 avatar Jan 26 '16 05:01 melnikov77

@melnikov77 can you point me to the patch? If this is an issue more are hitting, a config option may be in order.

NickCraver avatar Jan 26 '16 11:01 NickCraver