Skip to content

Conversation

Copy link

Copilot AI commented Nov 29, 2025

TcpListener instances were created outside try blocks, meaning Stop() wouldn't be called if exceptions occurred during setup.

Changes

  • Refactored all 11 test methods using TcpListener to ensure proper cleanup
  • Listener is now declared as nullable and initialized inside the try block
  • Finally block uses null-conditional listener?.Stop() for safe disposal

Before:

TcpListener listener = new(IPAddress.Loopback, 0);
listener.Start();
try { ... }
finally { listener.Stop(); }  // Never reached if Start() throws

After:

TcpListener? listener = null;
try {
    listener = new(IPAddress.Loopback, 0);
    listener.Start();
    ...
}
finally { listener?.Stop(); }  // Always safe

💡 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.

Copilot AI changed the title [WIP] Update to address feedback on .NET 10 updates Fix TcpListener disposal in TcpClientNmeaStreamReaderTests Nov 29, 2025
@HowardvanRooijen HowardvanRooijen marked this pull request as ready for review November 29, 2025 10:53
Copilot AI review requested due to automatic review settings November 29, 2025 10:53
@HowardvanRooijen HowardvanRooijen merged commit 8ebc720 into feature/dotnet-10-update Nov 29, 2025
5 checks passed
@HowardvanRooijen HowardvanRooijen deleted the copilot/sub-pr-267 branch November 29, 2025 10:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TcpListener initialization 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);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.

try
{
listener = new(IPAddress.Loopback, 0);
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disposable 'TcpListener' is created but not disposed.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants