Skip to content

Fix SerialPort handle leakage (#122384) - #132557

Open
Alikhalesi wants to merge 1 commit into
dotnet:mainfrom
Alikhalesi:Issue-122384
Open

Fix SerialPort handle leakage (#122384)#132557
Alikhalesi wants to merge 1 commit into
dotnet:mainfrom
Alikhalesi:Issue-122384

Conversation

@Alikhalesi

Copy link
Copy Markdown

No description provided.

@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label Aug 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-ports
See info in area-owners.md if you want to be subscribed.

@Alikhalesi

Alikhalesi commented Aug 20, 2026

Copy link
Copy Markdown
Author

How to replicate the issue on windows:
1- Download https://sourceforge.net/projects/com0com/
2- Add a device pair containing COM5
3- Check "use port class"
4- Compile and run the provided code
5- When you see "unplug device,then enter", uncheck "use port class" in the com-0-com app, then press any enter
6- When you see "Plug device,then enter" , check "use port class" in the com-0-com app, then press any enter
image

`
using System.IO.Ports;

partial class Program
{

private static SerialPort CreatePort(string name)
{
return new SerialPort(name, 115200, Parity.None, 8, StopBits.One);
}
private static void Main()
{
Console.WriteLine("start?");
Console.ReadKey();
var names = SerialPort.GetPortNames();
// Change this with your COM port name
string portName = "COM5";
// Open + use port
SerialPort sp = CreatePort(portName);
sp.Open();
// sp.WriteLine("AT");
Console.WriteLine("unplug device,then enter");
// USER: unplug USB device now
Console.ReadKey();
// Close + dispose before the user plugs it back in
sp.Close();
sp.Dispose();
Console.WriteLine("Plug device,then enter");
// USER: plug USB device again now
Console.ReadKey();
// Recreate SerialPort after reconnection
SerialPort sp2 = CreatePort(portName);
// Expected to throw "resource in use" if the issue occurs
try
{
sp2.Open();
sp2.Close();
sp2.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}

}`

@Alikhalesi
Alikhalesi marked this pull request as ready for review August 21, 2026 04:49
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Portscommunity-contributionIndicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@Alikhalesi