Skip to content

Commit 3cc0cb6

Browse files
authored
Merge pull request #202 from seesharper/bugfix-stackoverflow-on-disposeasync
Ensure DisposeAsync does not cause stack overflow
2 parents 415dc63 + 93ec480 commit 3cc0cb6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/LightInject.Microsoft.DependencyInjection.Tests/AsyncDisposableTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ public async Task ShouldDisposeAsyncDisposableFromRootScope()
4040
asyncDisposable = serviceProvider.GetService<AsyncDisposable>();
4141
await ((IAsyncDisposable)serviceProvider).DisposeAsync();
4242

43+
// Call it twice to ensure only disposed once.
44+
await ((IAsyncDisposable)serviceProvider).DisposeAsync();
45+
46+
4347
Assert.Contains(asyncDisposable, disposedObjects);
48+
Assert.Single(disposedObjects);
49+
}
50+
51+
[Fact]
52+
public async Task ShouldHandleDisposeAsyncOnServiceProvider()
53+
{
54+
4455
}
4556
}
4657

src/LightInject.Microsoft.DependencyInjection/LightInject.Microsoft.DependencyInjection.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,16 @@ public void Dispose()
349349
#if USE_ASYNCDISPOSABLE
350350

351351
public ValueTask DisposeAsync()
352-
=> scope.DisposeAsync();
352+
{
353+
if (isDisposed)
354+
{
355+
return ValueTask.CompletedTask;
356+
}
357+
358+
isDisposed = true;
359+
360+
return scope.DisposeAsync();
361+
}
353362
#endif
354363

355364
/// <summary>

0 commit comments

Comments
 (0)