mirror of https://github.com/Cysharp/UniTask
Fix problem with part of await foreach not executing on break
parent
d210e3d76a
commit
b4486802f2
|
@ -159,6 +159,30 @@ namespace NetCoreTests.Linq
|
|||
list.Should().Equal(100, 200, 300, 400);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AwaitForeachBreak()
|
||||
{
|
||||
var finallyCalled = false;
|
||||
var enumerable = UniTaskAsyncEnumerable.Create<int>(async (writer, _) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await writer.YieldAsync(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
finallyCalled = true;
|
||||
}
|
||||
});
|
||||
|
||||
await foreach (var x in enumerable)
|
||||
{
|
||||
x.Should().Be(1);
|
||||
break;
|
||||
}
|
||||
finallyCalled.Should().BeTrue();
|
||||
}
|
||||
|
||||
async IAsyncEnumerable<int> Range(int from, int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
|
|
|
@ -52,6 +52,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
writer.Dispose();
|
||||
return default;
|
||||
}
|
||||
|
||||
|
@ -127,7 +128,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||
}
|
||||
}
|
||||
|
||||
sealed class AsyncWriter : IUniTaskSource, IAsyncWriter<T>
|
||||
sealed class AsyncWriter : IUniTaskSource, IAsyncWriter<T>, IDisposable
|
||||
{
|
||||
readonly _Create enumerator;
|
||||
|
||||
|
@ -137,6 +138,15 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||
{
|
||||
this.enumerator = enumerator;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
var status = core.GetStatus(core.Version);
|
||||
if (status == UniTaskStatus.Pending)
|
||||
{
|
||||
core.TrySetCanceled();
|
||||
}
|
||||
}
|
||||
|
||||
public void GetResult(short token)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue