Skip to content

Commit b6e3eed

Browse files
committed
Fix OnErrorResumeNext not disposing the old source.
1 parent ebb0eaa commit b6e3eed

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

async-enumerable-dotnet-test/OnErrorResumeNextTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,16 @@ await AsyncEnumerable.Error<int>(new InvalidOperationException())
3333
.OnErrorResumeNext(e => throw e)
3434
.AssertFailure(typeof(AggregateException));
3535
}
36+
37+
[Fact]
38+
public async void Error_DisposeSource()
39+
{
40+
var disposed = false;
41+
await AsyncEnumerable.Error<int>(new Exception())
42+
.DoOnDispose(() => disposed = true)
43+
.OnErrorResumeNext(e => AsyncEnumerable.Empty<int>())
44+
.AssertResult();
45+
Assert.True(disposed);
46+
}
3647
}
3748
}

async-enumerable-dotnet/impl/OnErrorResumeNext.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public OnErrorResumeNextEnumerator(IAsyncEnumerator<T> source, Func<Exception, I
4646

4747
public ValueTask DisposeAsync()
4848
{
49-
return _source.DisposeAsync();
49+
var src = _source;
50+
if (src != null)
51+
{
52+
return src.DisposeAsync();
53+
}
54+
return new ValueTask();
5055
}
5156

5257
public async ValueTask<bool> MoveNextAsync()
@@ -62,10 +67,18 @@ public async ValueTask<bool> MoveNextAsync()
6267
}
6368
catch (Exception ex)
6469
{
70+
var old = _source;
71+
_source = null;
72+
if (old != null)
73+
{
74+
await old.DisposeAsync();
75+
}
76+
6577
IAsyncEnumerator<T> en;
6678

6779
try
6880
{
81+
6982
en = _handler(ex).GetAsyncEnumerator(_ct);
7083
}
7184
catch (Exception exc)

0 commit comments

Comments
 (0)