Browse Source

A Task-continuation should be cancelled when the Sink is disposed to avoid leaking the continuation on long-lived tasks.

Daniel Weber 7 years ago
parent
commit
f850df1c98
1 changed files with 10 additions and 1 deletions
  1. 10 1
      Rx.NET/Source/src/System.Reactive/Linq/Observable/Merge.cs

+ 10 - 1
Rx.NET/Source/src/System.Reactive/Linq/Observable/Merge.cs

@@ -288,6 +288,7 @@ namespace System.Reactive.Linq.ObservableImpl
                 }
 
                 private readonly object _gate = new object();
+                private readonly CancellationTokenSource _cts = new CancellationTokenSource();
                 private volatile int _count = 1;
 
                 public override void OnNext(Task<TSource> value)
@@ -299,7 +300,7 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     else
                     {
-                        value.ContinueWith((t, thisObject) => ((_)thisObject).OnCompletedTask(t), this);
+                        value.ContinueWith((t, thisObject) => ((_)thisObject).OnCompletedTask(t), this, _cts.Token);
                     }
                 }
 
@@ -354,6 +355,14 @@ namespace System.Reactive.Linq.ObservableImpl
                         }
                     }
                 }
+
+                protected override void Dispose(bool disposing)
+                {
+                    if (disposing)
+                        _cts.Cancel();
+
+                    base.Dispose(disposing);
+                }
             }
         }
     }