Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case when EventLoopScheduler gets disposed with in-flight items #969

Merged
merged 2 commits into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
eugbaranov marked this conversation as resolved.
Show resolved Hide resolved
{
// 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