StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.85k stars 1.5k forks source link

Commited transaction with task not completed #2631

Closed pianoman4873 closed 5 months ago

pianoman4873 commented 5 months ago

Hello,

After committing a transaction ( calling Execute() ), I noticed that quite often the not all the tasks return true when checking Task.IsCompleted. I access the .Result of the tasks and that seems to work fine in my unittests, but I'm a little worried .. Is this expected behavior and is calling .Result ( in sync mode ) safe enough ?

Thanks ! Shimon

PS it also happens when in async mode when calling ExecuteAsync() , but less frequently. In that case I simply await the task.

mgravell commented 5 months ago

I imagine that this is purely a timing issue; for complicated reasons relating to thread-theft, we need to use non-synchronous task completion, and the moment we do that: order becomes hard to guarantee. This is supported by your observation that if you access .Result, the value does become available. However, I would recommend using await to fetch those results, which is the most correct way to read the result of any awaitable.

pianoman4873 commented 5 months ago

Thanks @mgravell But the method is synchronous so I can't use await. Any alternative? What could be the ramifications if when calling .Result, the task is not completed yet ( while in a synchronous method ).

mgravell commented 5 months ago

I can't stop you from accessing .Result, and in reality it is likely that completion is nanoseconds away - we've almost certainly already set that ball rolling; but it would be remiss of me to simply say that all the usually "sync over async" realities exist. But in this case: you should indeed be fine.

pianoman4873 commented 5 months ago

@mgravell OK, Thanks.