Skip to content

Commit 1990de7

Browse files
akarnokddanielcweber
authored andcommitted
Fix Zip(IEnumerable) NullReferenceException if a source completes immediately
(cherry picked from commit 1b19cb6)
1 parent 1888ca8 commit 1990de7

File tree

2 files changed

+40
-1
lines changed
  • Rx.NET/Source
    • src/System.Reactive/Linq/Observable
    • tests/Tests.System.Reactive/Tests/Linq/Observable

2 files changed

+40
-1
lines changed

Rx.NET/Source/src/System.Reactive/Linq/Observable/Zip.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,11 @@ private void OnCompleted(int index)
661661
}
662662
else
663663
{
664-
_subscriptions[index].Dispose();
664+
var subscriptions = Volatile.Read(ref _subscriptions);
665+
if (subscriptions != null && subscriptions != Array.Empty<IDisposable>())
666+
{
667+
Disposable.TryDispose(ref subscriptions[index]);
668+
}
665669
}
666670
}
667671
}

Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/ZipTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4448,6 +4448,41 @@ public void Zip_AtLeastOneThrows4()
44484448

44494449
#endregion
44504450

4451+
[Fact]
4452+
public void Zip2WithImmediateReturn()
4453+
{
4454+
Observable.Zip<Unit, Unit, Unit>(
4455+
Observable.Return(Unit.Default),
4456+
Observable.Return(Unit.Default),
4457+
(_, __) => Unit.Default
4458+
)
4459+
.Subscribe(_ => { });
4460+
}
4461+
4462+
[Fact]
4463+
public void Zip3WithImmediateReturn()
4464+
{
4465+
Observable.Zip<Unit, Unit, Unit, Unit>(
4466+
Observable.Return(Unit.Default),
4467+
Observable.Return(Unit.Default),
4468+
Observable.Return(Unit.Default),
4469+
(_, __, ___) => Unit.Default
4470+
)
4471+
.Subscribe(_ => { });
4472+
}
4473+
4474+
[Fact]
4475+
public void ZipEnumerableWithImmediateReturn()
4476+
{
4477+
Enumerable.Range(0, 100)
4478+
.Select(_ => Observable.Return(Unit.Default))
4479+
.Zip()
4480+
.Subscribe(_ =>
4481+
{
4482+
4483+
}
4484+
);
4485+
}
44514486
}
44524487
#pragma warning restore IDE0039 // Use local function
44534488
}

0 commit comments

Comments
 (0)