1

Closed

Using CancellationToken overload of Subscribe causes deadlock on cancellation

description

Subscribing to the IObservable<T> returned by EtwObservable.FromSession using the extension method Subscribe<T>(this IObservable<T> source, Action<T> onNext, CancellationToken token) causes a deadlock on cancellation.
            IObservable<EtwNativeEvent> session = EtwObservable.FromSession(sessionName);
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                session.Subscribe(e => Console.WriteLine("[{0}] provider={1}, id={2}", e.TimeStamp, e.ProviderId, e.Id), cts.Token);
                Console.ReadLine();
                cts.Cancel();
            }
When running this program, after pressing ENTER, the cancellation token is transition to the canceled state. The main thread is waiting on the cancellation callback to complete, getting stuck in Tx.Windows.EtwListener.Dispose waiting for Thread.Join to complete:
  • System.Threading.Thread.JoinInternal(Int32)
  • Tx.Windows.EtwListener.Dispose()
  • System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
  • System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
  • System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
  • System.Threading.CancellationCallbackInfo.ExecuteCallback()
  • System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments)
  • System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean)
  • ConsoleApplication1.Program.Main(System.String[])
The ETW thread proc is stuck, however, waiting for the cancellation token registration to be disposed (probably invoked via the OnCompleted implementation):
  • System.Threading.Thread.SleepInternal(Int32)
  • System.Threading.SpinWait.SpinOnce()
  • System.Threading.CancellationTokenSource.WaitForCallbackToComplete(System.Threading.CancellationCallbackInfo)
  • System.Threading.CancellationTokenRegistration.Dispose()
  • System.Reactive.Disposables.SingleAssignmentDisposable.Dispose()
  • System.ObservableExtensions+<>c__DisplayClass4`1[[Tx.Windows.EtwNativeEvent, Tx.Windows]].<Subscribe_>b__1()
  • Tx.Windows.EtwListener.ThreadProc()
Closed Jul 23, 2014 at 8:05 AM by georgis
Fixed in build 1.0.40127
◾4 = year 2014,
◾01 27 =Jan 27

comments

georgis wrote Jan 28, 2014 at 3:37 AM

Thank you Brian,

Fixed in build 1.0.40127
  • 4 = year 2014,
  • 01 27 =Jan 27