Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WriteDirectAsync throws System.NullReferenceException #962

Closed
olehb007 opened this issue Sep 24, 2018 · 4 comments
Closed

WriteDirectAsync throws System.NullReferenceException #962

olehb007 opened this issue Sep 24, 2018 · 4 comments

Comments

@olehb007
Copy link

Hi,

Time to time I'm getting System.NullReferenceException with a following stack:

StackExchange.Redis.ServerEndPoint.WriteDirectAsync[T](Message message, ResultProcessor`1 processor, Object asyncState, PhysicalBridge bridge) in C:\proj\trouter\StackExchange.Redis\src\StackExchange.Redis\ServerEndPoint.cs: line 562
StackExchange.Redis.ServerEndPoint.SendTracer(TextWriter log) in C:\proj\trouter\StackExchange.Redis\src\StackExchange.Redis\ServerEndPoint.cs: line 591
StackExchange.Redis.ConnectionMultiplexer.d__155.MoveNext() in C:\proj\trouter\StackExchange.Redis\src\StackExchange.Redis\ConnectionMultiplexer.cs: line 1678
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout)
StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(Object configuration, TextWriter log) in C:\proj\trouter\StackExchange.Redis\src\StackExchange.Redis\ConnectionMultiplexer.cs: line 917
StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationOptions configuration, TextWriter log) in C:\proj\trouter\StackExchange.Redis\src\StackExchange.Redis\ConnectionMultiplexer.cs: line 903

As far as I understood GetBridge can return null and thus throw on bridge.TryWrite.

        internal Task<T> WriteDirectAsync<T>(Message message, ResultProcessor<T> processor, object asyncState = null, PhysicalBridge bridge = null)
        {
            var tcs = TaskSource.Create<T>(asyncState);
            var source = ResultBox<T>.Get(tcs);
            message.SetSource(processor, source);
            if (bridge == null) bridge = GetBridge(message.Command);
    var result = bridge.TryWrite(message, isSlave);
@mgravell
Copy link
Collaborator

mgravell commented Sep 24, 2018 via email

@olehb007
Copy link
Author

Nothing specific, just simulating multiple clients and thus calling ConnectionMultiplexer.Connect(connectionConfig, null) multiple times.

@joshuaChou
Copy link

joshuaChou commented Sep 25, 2018

In my case

Nuget Version:2.0.505
redis server :redis cluster 4.0.10
.net Version:.net framework 4.6.1

Code:

 public static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            ConnectionMultiplexer conn = null;
            ConfigurationOptions _config;

            _config = new ConfigurationOptions
            {
                CommandMap = CommandMap.Default,
                EndPoints = { { "127.0.0.1", 6379 } },
                AllowAdmin = true,
                SyncTimeout = 5000,
                KeepAlive = 60,
                ConnectTimeout = 10000,
                DefaultVersion = new Version(4, 0),
                AbortOnConnectFail = false
            };

            try
            {
                conn = ConnectionMultiplexer.Connect(_config);
            }
            catch (Exception ex)
            {

                Console.WriteLine("{0}", ex.ToString());
            }

            return conn;
        });

Some time Get Error

System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at StackExchange.Redis.ServerEndPoint.WriteDirectAsync[T](Message message, ResultProcessor`1 processor, Object asyncState, PhysicalBridge bridge)
   at StackExchange.Redis.ServerEndPoint.SendTracer(TextWriter log)
   at StackExchange.Redis.ConnectionMultiplexer.<ReconfigureAsync>d__151.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(Object configuration, TextWriter log)
   at StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationOptions configuration, TextWriter log)
   at RedisClusterTest.Program.<>c.<.cctor>b__8_0() in C:\Users\Joshua\Source\repos\RedisClusterTest\RedisClusterTest\Program.cs:line 71
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
   at StackExchange.Redis.ServerEndPoint.WriteDirectAsync[T](Message message, ResultProcessor`1 processor, Object asyncState, PhysicalBridge bridge)
   at StackExchange.Redis.ServerEndPoint.SendTracer(TextWriter log)
   at StackExchange.Redis.ConnectionMultiplexer.<ReconfigureAsync>d__151.MoveNext()<---

System.NullReferenceException: Object reference not set to an instance of an object.
   at RedisClusterTest.Program.Main(String[] args) in C:\Users\Joshua\Source\repos\RedisClusterTest\RedisClusterTest\Program.cs:line 17

I Try fix this error In ServerEndPoint.cs line 562

   internal Task<T> WriteDirectAsync<T>(Message message, ResultProcessor<T> processor, object asyncState = null, PhysicalBridge bridge = null)
        {
            var tcs = TaskSource.Create<T>(asyncState);
            var source = ResultBox<T>.Get(tcs);
            message.SetSource(processor, source);
            if (bridge == null) bridge = GetBridge(message.Command);

            // Add Code
            while (bridge == null)
            {
                bridge = GetBridge(message.Command);
            }

            var result = bridge.TryWrite(message, isSlave);
            if (result != WriteResult.Success)
            {
                var ex = Multiplexer.GetException(result, message, this);
                ConnectionMultiplexer.ThrowFailed(tcs, ex);
            }
            return tcs.Task;
        }

but i think this is not a good idea.

Thank!

mgravell added a commit that referenced this issue Sep 26, 2018
@mgravell
Copy link
Collaborator

Should be fixed next build, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants