0

I trying to merge the use of several technologies in a web application. Most of the Rx code is already tested in Test Projects and everything seems to be working correctly. But I'm having some exceptions when I try to make it work in the app environment (IIS):

Object reference not set to an instance of an object.
   at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
   at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
   at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
   at System.Reactive.Concurrency.SynchronizationContextExtensions.PostWithStartComplete(SynchronizationContext context, Action action)
   at System.Reactive.Concurrency.SynchronizationContextScheduler.Schedule[TState](TState state, Func`3 action)
   at System.Reactive.Concurrency.Scheduler.Schedule(IScheduler scheduler, Action action)
   at System.Reactive.Linq.ObservableImpl.EventProducer`2.Session.<>c__DisplayClass3.<Connect>b__2()
   at System.Reactive.Disposables.AnonymousDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Sink`1.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.ObserveOnObserver`1.Dispose(Boolean disposing)
   at System.Reactive.ObserverBase`1.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.RefCountDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Sink`1.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.ObserveOnObserver`1.Dispose(Boolean disposing)
   at System.Reactive.ObserverBase`1.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Disposables.CompositeDisposable.Dispose()
   at System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
   at System.Reactive.Sink`1.Dispose()
   at System.Reactive.Linq.ObservableImpl.SelectMany`2._.Iter.OnError(Exception error)
   at System.Reactive.ScheduledObserver`1.Run(Object state, Action`1 recurse)
   at System.Reactive.Concurrency.Scheduler.<>c__DisplayClass10`1.<InvokeRec1>b__d(TState state1)
   at System.Reactive.Concurrency.Scheduler.InvokeRec1[TState](IScheduler scheduler, Pair`2 pair)
   at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore()
   at System.Reactive.Concurrency.ScheduledItem`1.Invoke()
   at System.Reactive.Concurrency.EventLoopScheduler.Run()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I suspect that: "It's All About the SynchronizationContext" or Rx Scheduler, someone can give more information or details about it... Currently, I cannot find the cause or how to fix these problems, because all the exceptions are internal (from .NET libraries or Rx library).

Edit 1: After some research I found other topics related, but there is no answer.
- Async logging throwing a NullReferenceException
- NullReferenceException in System.Threading.Tasks calling HttpClient.GetAsync(url)
- http://connect.microsoft.com/VisualStudio/feedback/details/745956/getting-null-reference-exception-in-asp-net-mvc-4-rc#

Edit 2: After spend a few hours to get this error in a sample app (incredibly in my test web app was working properly), here is the code that throws the error (this code is just for example).

https://github.com/jlVidal/WebForms-Signalr-And-Rx-Error
To simulate, please interact with the interface, subscribe, unsubscribe, disconnect, reconnect.. I always have an error when I click to subscribe some ID, and close the page (it calls OnDisconnected method) and throws NullReferenceException with the stack trace similar to above.

After all.. I don't know if I found the cause, many observations may not make sense:

  1. I cannot create and manage my Rx subscriptions in the creation of hub, or do something similar.. I did it because of the lazy loading. In fact the moment where I manage my subscriptions (Subscribe) affects the whole, if I move this code, apparently seems to stop to give error.
  2. The error occurs often in the OnDisconnected (SignalR Hub method), but it is almost random, I really don't found a pattern.

Someone can give me a light?

Community
  • 1
  • 1
J. Lennon
  • 3,311
  • 4
  • 33
  • 64

2 Answers2

0

Your code inside SomeHub is accessing a global dictionary (_all) in a very thread-unsafe manner. This dictionary contains SortedSet instances which are also being accessed in a thread-unsafe manner.

At least some of your "random" crashes are likely caused by the race conditions in your thread-unsafe code. Make your SomeHub code thread-safe and then see if that resolves any or all of your errors.

Brandon
  • 38,310
  • 8
  • 82
  • 87
  • I know that is not thread safe code, but if the problem is there, it is clear that the stack trace would be different, change the code to use concurrent collections (or to deal with multi-threading) will not make the app stop throw exceptions. – J. Lennon May 22 '14 at 02:08
0

This problem seems that was solved by updating the NuGet packages for the newest versions and/or Microsoft ASP.NET product updates.

J. Lennon
  • 3,311
  • 4
  • 33
  • 64