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

NullReferenceException in PhysicalConnection.BeginReading crashes process

Open JonCole opened this issue 12 years ago • 3 comments

Looks like there is a race condition that can lead to a NullReferenceException in BeginReading. I am using build 1.0.371.

Here is the stack:

StackExchange.Redis.PhysicalConnection.BeginReading() [c:\TeamCity\buildAgent\work\58bc9a6df18a3782\StackExchange.Redis\StackExchange\Redis\PhysicalConnection.cs @ 600] System.Net.LazyAsyncResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net\_LazyAsyncResult.cs @ 415] System.Net.Security._SslStream.ProcessFrameBody(Int32, Byte[], Int32, Int32, System.Net.AsyncProtocolRequest) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols\_SslStream.cs @ 856] System.Net.Security._SslStream.ReadFrameCallback(System.Net.AsyncProtocolRequest) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols\_SslStream.cs @ 1028] System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(Int32) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols\_FixedSizeReader.cs @ 125] System.Net.FixedSizeReader.ReadCallback(System.IAsyncResult) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols\_FixedSizeReader.cs @ 150] System.Net.LazyAsyncResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net\_LazyAsyncResult.cs @ 415] System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 581] System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 530] System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 519] System.Net.ContextAwareResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net\_ContextAwareResult.cs @ 463] System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*) [f:\dd\ndp\fx\src\Net\System\Net\Sockets\_BaseOverlappedAsyncResult.cs @ 385] System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*) [f:\dd\ndp\clr\src\BCL\System\Threading\Overlapped.cs @ 126] [GCFrame: 00000070e567f018] [DebuggerU2MCatchHandlerFrame: 00000070e567f1e8]

Looking at the object in the debugger I can see that PhysicalConnection.netStream is null and it seems this is because the ConnectionMultiplexer was disposed.

I was able to reproduce this issue using code like this from multiple threads simultaneously.

            using (var connection = await CreateConnectionAsync(host, ssl, key))
            {
                await connection.GetDatabase().StringGetAsync("foo");
                await Task.Delay(new Random().Next(100, 400), cancellationToken);
            }

This exception crashes the process because it happens on a background thread and there is no way for me to catch it since my code is not on the stack.

This does not block me personally as this was a test app I was writing, but thought I would file the issue anyway to track it in case others hit it.

JonCole avatar Nov 05 '14 18:11 JonCole

"Merge pull request #138 from paveliak/exception_in_callback" Fixed this issue as well

SiddharthChatrolaMs avatar Jan 21 '15 19:01 SiddharthChatrolaMs

Probably the first thing to do is: try build 394 - does it occur there? On 5 Nov 2014 18:40, "JonCole" [email protected] wrote:

Looks like there is a race condition that can lead to a NullReferenceException in BeginReading. I am using build 1.0.371.

Here is the stack:

StackExchange.Redis.PhysicalConnection.BeginReading() [c:\TeamCity\buildAgent\work\58bc9a6df18a3782\StackExchange.Redis\StackExchange\Redis\PhysicalConnection.cs @ 600] System.Net.LazyAsyncResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net_LazyAsyncResult.cs @ 415] System.Net.Security._SslStream.ProcessFrameBody(Int32, Byte[], Int32, Int32, System.Net.AsyncProtocolRequest) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols_SslStream.cs @ 856] System.Net.Security.SslStream.ReadFrameCallback(System.Net.AsyncProtocolRequest) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols_SslStream.cs @ 1028] System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(Int32) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols_FixedSizeReader.cs @ 125] System.Net.FixedSizeReader.ReadCallback(System.IAsyncResult) [f:\dd\ndp\fx\src\Net\System\Net\SecureProtocols_FixedSizeReader.cs @ 150] System.Net.LazyAsyncResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net_LazyAsyncResult.cs @ 415] System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 581] System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 530] System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [f:\dd\ndp\clr\src\BCL\System\Threading\ExecutionContext.cs @ 519] System.Net.ContextAwareResult.Complete(IntPtr) [f:\dd\ndp\fx\src\Net\System\Net_ContextAwareResult.cs @ 463] System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped) [f:\dd\ndp\fx\src\Net\System\Net\Sockets_BaseOverlappedAsyncResult.cs @ 385] System.Threading.IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped) [f:\dd\ndp\clr\src\BCL\System\Threading\Overlapped.cs @ 126] [GCFrame: 00000070e567f018] [DebuggerU2MCatchHandlerFrame: 00000070e567f1e8]

Looking at the object in the debugger I can see that PhysicalConnection.netStream is null and it seems this is because the ConnectionDemuxer was disposed.

I was able to reproduce this issue using code like this from multiple threads simultaneously.

        using (var connection = await CreateConnectionAsync(host, ssl, key))
        {
            await connection.GetDatabase().StringGetAsync("foo");
            await Task.Delay(new Random().Next(100, 400), cancellationToken);
        }

This exception crashes the process because it happens on a background thread and there is no way for me to catch it since my code is not on the stack.

This does not block me personally as this was a test app I was writing, but thought I would file the issue anyway to track it in case others hit it.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/123.

mgravell avatar Jan 21 '15 20:01 mgravell

No it doesn't occur with 1.0.394.

SiddharthChatrolaMs avatar Feb 23 '15 19:02 SiddharthChatrolaMs