Skip to content

Commit f308df2

Browse files
authored
Merge pull request #969 from eugbaranov/els-dispose
Handle the case when EventLoopScheduler gets disposed with in-flight items
2 parents dd9a824 + 94627f5 commit f308df2

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Rx.NET/Source/src/System.Reactive/Concurrency/EventLoopScheduler.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
153153
{
154154
if (_disposed)
155155
{
156-
throw new ObjectDisposedException("");
156+
throw new ObjectDisposedException(nameof(EventLoopScheduler));
157157
}
158158

159159
if (dueTime <= TimeSpan.Zero)
@@ -351,7 +351,15 @@ private void Run()
351351
{
352352
if (!item.IsCanceled)
353353
{
354-
item.Invoke();
354+
try
355+
{
356+
item.Invoke();
357+
}
358+
catch (ObjectDisposedException ex) when (nameof(EventLoopScheduler).Equals(ex.ObjectName))
359+
{
360+
// Since we are not inside the lock at this point
361+
// the scheduler can be disposed before the item had a chance to run
362+
}
355363
}
356364
}
357365
}

Rx.NET/Source/tests/Tests.System.Reactive/Tests/Concurrency/EventLoopSchedulerTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.Reactive.Concurrency;
99
using System.Reactive.Disposables;
10+
using System.Reactive.Linq;
1011
using System.Threading;
1112
using Microsoft.Reactive.Testing;
1213
using Xunit;
@@ -41,6 +42,19 @@ public void EventLoop_Now()
4142
Assert.True(res.Seconds < 1);
4243
}
4344

45+
[Fact]
46+
public void EventLoop_DisposeWithInFlightActions()
47+
{
48+
using (var scheduler = new EventLoopScheduler())
49+
using (var subscription = Observable
50+
.Range(1, 10)
51+
.ObserveOn(scheduler)
52+
.Subscribe(_ => Thread.Sleep(50)))
53+
{
54+
Thread.Sleep(50);
55+
}
56+
}
57+
4458
[Fact]
4559
public void EventLoop_ScheduleAction()
4660
{

0 commit comments

Comments
 (0)