Skip to content

Commit

Permalink
Disposing EventLoopScheduler with in-flight items (closes #286)
Browse files Browse the repository at this point in the history
  • Loading branch information
eugbaranov committed Jul 18, 2019
1 parent dca0515 commit 10a5004
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Expand Up @@ -153,7 +153,7 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
{
if (_disposed)
{
throw new ObjectDisposedException("");
throw new ObjectDisposedException(nameof(EventLoopScheduler));
}

if (dueTime <= TimeSpan.Zero)
Expand Down Expand Up @@ -351,7 +351,15 @@ private void Run()
{
if (!item.IsCanceled)
{
item.Invoke();
try
{
item.Invoke();
}
catch (ObjectDisposedException ex) when (nameof(EventLoopScheduler).Equals(ex.ObjectName))
{
// Since we are not inside the lock at this point
// the scheduler can be disposed before the item had a chance to run
}
}
}
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using Microsoft.Reactive.Testing;
using Xunit;
Expand Down Expand Up @@ -41,6 +42,19 @@ public void EventLoop_Now()
Assert.True(res.Seconds < 1);
}

[Fact]
public void EventLoop_DisposeWithInFlightActions()
{
using (var scheduler = new EventLoopScheduler())
using (var subscription = Observable
.Range(1, 10)
.ObserveOn(scheduler)
.Subscribe(_ => Thread.Sleep(50)))
{
Thread.Sleep(50);
}
}

[Fact]
public void EventLoop_ScheduleAction()
{
Expand Down

0 comments on commit 10a5004

Please sign in to comment.