InvalidOperationException: "Disposable has already been assigned" when using Buffer


  • I'm on Rx 2.0.21114, using .NET 4 (Windows Forms app)

    Stack trace:

    System.InvalidOperationException: Disposable has already been assigned.
    at System.Reactive.Disposables.SingleAssignmentDisposable.set_Disposable(IDisposable value)
    at System.Reactive.Concurrency.CurrentThreadScheduler.Trampoline.Run(SchedulerQueue`1 queue)
    at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action)
    at System.Reactive.Concurrency.LocalScheduler.Schedule[TState](TState state, Func`3 action)
    at System.Reactive.Producer`1.SubscribeRaw(IObserver`1 observer, Boolean enableSafeguard)
    at System.ObservableExtensions.SubscribeSafe[T](IObservable`1 source, IObserver`1 observer)
    at System.Reactive.Linq.Observ?ble.Buffer`2._.CreateBufferClose()
    at System.Reactive.Concurrency.AsyncLock.Wait(Action action)
    at System.Reactive.Linq.Observ?ble.Buffer`2._.CloseBuffer(IDisposable closingSubscription)
    at System.Reactive.Linq.Observ?ble.Timer._.Invoke()
    at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action action)
    at System.Reactive.Concurrency.DefaultScheduler.<>c__DisplayClass4`1.<Schedule>b__3(Object _)
    at System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.Timer.Tick(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading._TimerCallback.PerformTimerCallback(Object state)

    I'm using buffer equivalently to this:

    .Buffer(() => Observable.Timer(SomeTimeSpan, Scheduler.Default))

    The reason I can't just use the TimeSpan, IScheduler overload is because the TimeSpan can
    change at runtime. This application was running fine for 4 hours when this happened randomly. The application has been running with this code for some time without this issue. I can't reproduce at will.

    If anyone has any ideas as to what could cause this, or next steps to take, I would appreciate it.

    Wednesday, February 27, 2013 10:53 PM

All replies

  • I've had the same issues with the TestScheduler and the only thing I could do to get around it was to do Catch on the scheduler and return true from it effectively just ignoring the error.

    So your code would be:

    Scheduler.Default.Catch(ex => true);

    .Buffer(() => Observable.Timer(SomeTimeSpan, Scheduler.Default));

    You can log the error or do any other kind of handling in the catch but at least the Observable will continue to run.

    Hope it helps,


    Thanks, nisbus

    Wednesday, December 04, 2013 10:39 PM