Fix TcpListener disposal in TcpClientNmeaStreamReaderTests#268
Fix TcpListener disposal in TcpClientNmeaStreamReaderTests#268HowardvanRooijen merged 3 commits intofeature/dotnet-10-updatefrom
Conversation
Co-authored-by: HowardvanRooijen <128664+HowardvanRooijen@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors 11 test methods in TcpClientNmeaStreamReaderTests to ensure proper cleanup of TcpListener resources, even when exceptions occur during test setup. The refactoring prevents potential resource leaks by guaranteeing that Stop() is safely called in all scenarios.
Key Changes:
- Moved
TcpListenerinitialization from outside try blocks to inside them - Changed listener declarations to nullable type (
TcpListener?) initialized to null - Updated finally blocks to use null-conditional operator (
listener?.Stop()) for safe cleanup
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
|
|
||
| try | ||
| { | ||
| listener = new(IPAddress.Loopback, 0); |
There was a problem hiding this comment.
Disposable 'TcpListener' is created but not disposed.
TcpListenerinstances were created outside try blocks, meaningStop()wouldn't be called if exceptions occurred during setup.Changes
TcpListenerto ensure proper cleanuplistener?.Stop()for safe disposalBefore:
After:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.