Serious bug with the Windows Phone version of .NET RX

Answered Serious bug with the Windows Phone version of .NET RX

  • Tuesday, February 01, 2011 6:21 AM
     
      Has Code

    Every once in a while I get a NullReferenceException in the CurrentThreadScheduler.Trampoline.Run() method.  This is a serious error since it will immediately crash the application.  The stack trace is as follows (please note this is for the WINDOWS PHONE version of .NET RX, i.e. Microsoft.Phone.Reactive).

     

      at Microsoft.Phone.Reactive.CurrentThreadScheduler.Trampoline.Run()
      at Microsoft.Phone.Reactive.CurrentThreadScheduler.EnsureTrampoline(Action action)
      at Microsoft.Phone.Reactive.AnonymousObservable`1.Subscribe(IObserver`1 observer)
      at Microsoft.Phone.Reactive.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext, Action`1 onError, Action onCompleted)
      at Microsoft.Phone.Reactive.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext)
      at weave.CustomList.<>c__DisplayClassb.<SetNews>b__5(Int32 notUsed)
      at Microsoft.Phone.Reactive.AnonymousObserver`1.Next(Int32 value)
      at Microsoft.Phone.Reactive.AbstractObserver`1.OnNext(Int32 value)
      at Microsoft.Phone.Reactive.AnonymousObservable`1.AutoDetachObserver.Next(Int32 value)
      at Microsoft.Phone.Reactive.AbstractObserver`1.OnNext(Int32 value)
      at Microsoft.Phone.Reactive.Notification`1.OnNext.Accept(IObserver`1 observer)
      at Microsoft.Phone.Reactive.Observable.<>c__DisplayClass2e2`1.<>c__DisplayClass2e4.<Delay>b__2e1(Action`1 self)
      at Microsoft.Phone.Reactive.Scheduler.<>c__DisplayClassc.<Schedule>b__9()
      at Microsoft.Phone.Reactive.AsyncLock.Wait(Action action)
      at Microsoft.Phone.Reactive.Scheduler.<>c__DisplayClassc.<Schedule>b__8()
      at Microsoft.Phone.Reactive.ThreadPoolScheduler.<>c__DisplayClass5.<Schedule>b__3(Object _)
      at System.Threading.Timer.ring()
    
    

    Anyone know why this is happening, and what workaround there is to resolve it?  Thank you!

     

All Replies

  • Tuesday, February 01, 2011 7:51 AM
     
     

    It sounds like you are passing null to OnNext, or some other scheduled action that cannot be null.

    If that's the case, let the team know so they can add a guard to the original method.

  • Tuesday, February 01, 2011 7:54 AM
     
     
    How do I contact the team?
  • Tuesday, February 01, 2011 6:51 PM
    Owner
     
     
    We'll investigate and come back to you. Thanks for reporting this.
    using (Microsoft.Sql.Cloud.DataProgrammability.Rx) { Signature.Emit("Bart De Smet"); }
  • Tuesday, February 01, 2011 10:15 PM
    Owner
     
     
    Thanks for bringing this to our attention. Do you have a small repro we can use for our investigation? It seems you're getting a sporadic failure if I understand your message correctly.
    using (Microsoft.Sql.Cloud.DataProgrammability.Rx) { Signature.Emit("Bart De Smet"); }
  • Wednesday, February 02, 2011 12:03 AM
     
     Answered

    We found the problem and have been able to repro it on our side.  Our next release on MSDN Data Developer Center and DevLabs will have a fix in it.  We are engaging with the Windows Phone 7 team to see how we can provide a fix for the version that ships with the phone.

    The essence of the problem is that Silverlight for Windows Phone 7 has no thread local storage.  The CurrentThreadScheduler uses the ThreadStatic attribute on one of its fields; however, the runtime ignores this attribute and silently allows threads to contend for the static field.

    We'll let you know more as we figure out how we will address this issue on the phone.  Sorry for the inconvenience.

    • Proposed As Answer by fixedpoint Wednesday, February 02, 2011 12:03 AM
    • Marked As Answer by Arash Emami Wednesday, February 02, 2011 2:14 AM
    •  
  • Wednesday, February 02, 2011 2:14 AM
     
     

    You guys move fast, thank you for finding the bug so quickly.

    Is there anything I can do in my code to work around this as a temporary measure?  If I provided a custom IScheduler implementation and passed it into all ObserveOn and SubscribeOn methods, would that guard against the exception happening?

    Thank you,

    Arash

  • Wednesday, February 02, 2011 2:17 AM
     
     
    Unfortunately, there is no workaround because each observable calls EnsureTrampoline on the CurrentThreadScheduler.  Will you be able to take a download with the fix when we make this next release public?
  • Wednesday, February 02, 2011 4:00 AM
     
     

    Hi Wes,

    I can definitely use the download once you make it available.  It is easy to release an updated version of my app.  Thank you so much for finding the bug so quickly.

    Arash

  • Wednesday, February 02, 2011 10:06 AM
     
     

    We found the problem and have been able to repro it on our side.  Our next release on MSDN Data Developer Center and DevLabs will have a fix in it.  We are engaging with the Windows Phone 7 team to see how we can provide a fix for the version that ships with the phone.

    The essence of the problem is that Silverlight for Windows Phone 7 has no thread local storage.  The CurrentThreadScheduler uses the ThreadStatic attribute on one of its fields; however, the runtime ignores this attribute and silently allows threads to contend for the static field.

    We'll let you know more as we figure out how we will address this issue on the phone.  Sorry for the inconvenience.


    Wow... that's nasty!
    James Miles http://enumeratethis.com
  • Wednesday, July 27, 2011 7:55 AM
     
     
    Unfortunately, there is no workaround because each observable calls EnsureTrampoline on the CurrentThreadScheduler.  Will you be able to take a download with the fix when we make this next release public?


    Hi Wes,

    Can you provide an update to this issue? Namely, will a fix for the bug be included in the Mango release?

    We're creating a library and are trying to avoid creating a binary dependency on a specific version of Rx (by depending on the build in version), allowing consumers of the library to choose any version of the System.Reactive Rx libraries.

    Cheers,
    Richard

  • Wednesday, July 27, 2011 7:11 PM
     
     
    There is a fix in Mango and the current downloadable bits don't have the issue.
  • Wednesday, July 27, 2011 9:38 PM
     
     
    Wes - would you recommend using the latest version of RX, or the version that is included with Mango, when doing Windows Phone development?
  • Wednesday, July 27, 2011 10:38 PM
     
     
    If you can, I recommend using the latest version of RX.
  • Thursday, July 28, 2011 8:55 AM
     
     
    There is a fix in Mango and the current downloadable bits don't have the issue.

    Fantastic, thanks Wes.
  • Thursday, July 28, 2011 8:24 PM
     
     
    Thank you Wes - will use the latest then.