Commit 6e2a7a20 authored by piccit's avatar piccit

Prevent Message from calling TryComplete on the same ResultBox (by...

Prevent Message from calling TryComplete on the same ResultBox (by gettin/nulling the ResultBox in a thread safe way).
parent e432b29a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
using System.Runtime.Serialization; using System.Runtime.Serialization;
#endif #endif
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StackExchange.Redis namespace StackExchange.Redis
...@@ -493,15 +494,19 @@ public void SetResponseReceived() ...@@ -493,15 +494,19 @@ public void SetResponseReceived()
performance?.SetResponseReceived(); performance?.SetResponseReceived();
} }
public bool TryComplete(bool isAsync) public bool TryComplete(bool isAsync)
{ {
if (resultBox != null) //Ensure we can never call TryComplete on the same resultBox from two threads by grabbing it now
{ var currBox = Interlocked.Exchange(ref resultBox, null);
var ret = resultBox.TryComplete(isAsync); if (currBox != null)
{
var ret = currBox.TryComplete(isAsync);
if (ret && isAsync) //in async mode TryComplete will have unwrapped and recycled resultBox
if (!(ret && isAsync))
{ {
resultBox = null; // in async mode TryComplete will have unwrapped and recycled resultBox; ensure we no longer reference it via this message //put result box back if it was not already recycled
Interlocked.Exchange(ref resultBox, currBox);
} }
performance?.SetCompleted(); performance?.SetCompleted();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment