فهرست منبع

Fix Zip(IEnumerable) NullReferenceException if a source completes immediately

(cherry picked from commit 1b19cb6b565abc3e073e0177d517ab589da31036)
Dávid Karnok 7 سال پیش
والد
کامیت
1990de7c85

+ 5 - 1
Rx.NET/Source/src/System.Reactive/Linq/Observable/Zip.cs

@@ -661,7 +661,11 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     else
                     {
-                        _subscriptions[index].Dispose();
+                        var subscriptions = Volatile.Read(ref _subscriptions);
+                        if (subscriptions != null && subscriptions != Array.Empty<IDisposable>())
+                        {
+                            Disposable.TryDispose(ref subscriptions[index]);
+                        }
                     }
                 }
             }

+ 35 - 0
Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/ZipTest.cs

@@ -4448,6 +4448,41 @@ namespace ReactiveTests.Tests
 
         #endregion
 
+        [Fact]
+        public void Zip2WithImmediateReturn()
+        {
+            Observable.Zip<Unit, Unit, Unit>(
+                Observable.Return(Unit.Default), 
+                Observable.Return(Unit.Default), 
+                (_, __) => Unit.Default
+            )
+            .Subscribe(_ => {  });
+        }
+
+        [Fact]
+        public void Zip3WithImmediateReturn()
+        {
+            Observable.Zip<Unit, Unit, Unit, Unit>(
+                Observable.Return(Unit.Default),
+                Observable.Return(Unit.Default),
+                Observable.Return(Unit.Default),
+                (_, __, ___) => Unit.Default
+            )
+            .Subscribe(_ => { });
+        }
+
+        [Fact]
+        public void ZipEnumerableWithImmediateReturn()
+        {
+            Enumerable.Range(0, 100)
+                .Select(_ => Observable.Return(Unit.Default))
+                .Zip()
+                .Subscribe(_ =>
+                {
+
+                }
+                );
+        }
     }
 #pragma warning restore IDE0039 // Use local function
 }