Avoid ODE in SignalR TestServer tests#63278
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an ObjectDisposedException (ODE) that was occurring in SignalR TestServer tests by ensuring proper resource cleanup. The issue arose when moving to HostBuilder and TestServer in the DI container, where connections weren't being properly disposed before the host was closed.
- Updates connection disposal to use
await usingpattern for automatic async disposal - Ensures connections are properly closed before host disposal to prevent ObjectDisposedException
| connectionBuilder.Services.AddLogging(); | ||
| connectionBuilder.Services.AddSingleton(LoggerFactory); | ||
| var connection = connectionBuilder.Build(); | ||
| await using var connection = connectionBuilder.Build(); |
There was a problem hiding this comment.
Using await using for connection disposal is correct, but ensure that the connection is explicitly stopped before disposal to prevent potential race conditions. Consider calling await connection.StopAsync() before the connection goes out of scope.
There was a problem hiding this comment.
DisposeAsync does the same thing as StopAsync for the HubConnection.
| connectionBuilder.Services.AddLogging(); | ||
| connectionBuilder.Services.AddSingleton(LoggerFactory); | ||
| var connection = connectionBuilder.Build(); | ||
| await using var connection = connectionBuilder.Build(); |
There was a problem hiding this comment.
Using await using for connection disposal is correct, but ensure that the connection is explicitly stopped before disposal to prevent potential race conditions. Consider calling await connection.StopAsync() before the connection goes out of scope.
Moving to
HostBuilderand thusTestServerin the DI container, instead of explicitly creating it, has caused an ODE to appear in this test because we weren't closing the connection but were closing the host.Should be fixed by closing the connection before closing the host.