Skip to content

SerialPort.Unix: use UnixHandleAsyncContext. - #134854

Draft
tmds wants to merge 2 commits into
dotnet:mainfrom
tmds:serialport-epoll
Draft

tmds wants to merge 2 commits into
dotnet:mainfrom
tmds:serialport-epoll

Conversation

@tmds

@tmds tmds commented Sep 29, 2026

Copy link
Copy Markdown
Member

This reduces read latency and CPU usage by replacing the poll-loop with epoll/kevent.

To access the UnixHandleAsyncContext from CoreLib, UnsafeAccessorAttribute is used.

This reduces read latency and CPU usage by replacing the poll-loop with epoll/kevent.

To access the UnixHandleAsyncContext from CoreLib, UnsafeAccessorAttribute is used.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 29, 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.

@tmds

tmds commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

The SerialPort.Tests are passing on my machine, but that is probably not covering much since I don't have a serial port...

I suspect the Tests themselves will need some updates too which is not yet included in this PR. I can look into this further if there is some interest in this change.

To access the UnixHandleAsyncContext from CoreLib, UnsafeAccessorAttribute is used.

I'm not sure if this is considered acceptable.

The SerialPort implementation was already part of #127228. To make this PR I only had to adjust it for using the UnixHandleAsyncContext that was added to CoreLib.

@jkotas

jkotas commented Sep 29, 2026

Copy link
Copy Markdown
Member

System.IO.Ports is standalone nuget package. It is not ok to depend on internal APIs from standalone nuget package. This can proceed only once the APIs are approved and public.

@jkotas jkotas added blocked Issue/PR is blocked on something - see comments NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) labels Sep 29, 2026
@tmds

tmds commented Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

System.IO.Ports is standalone nuget package. It is not ok to depend on internal APIs from standalone nuget package. This can proceed only once the APIs are approved and public.

I assumed there to be such a limitation, though I don't understand what drives it. Is it technical? Or is it a policy?
I would assume that a net12.0 target could use the APIs (even if not public) from .NET 12?

@jkotas

jkotas commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

.NET 12 target shipping in 12.0 nuget package is expected to work on top of .NET 13+ runtime. It means that we would not be able to change the internal APIs and they would become de-facto public without proper process.

@tmds

tmds commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Got it.

I didn't want the effort I put into making SerialPort work on top of epoll in #127228 go to waste/stale which is why I created this PR. SerialPort has some specific challenges due to its DataReceived event and the ReceivedBytesThreshold property.

There is some work to be done still in the Tests project too. I'll mark the PR as draft.

@tmds

tmds commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

@dotnet/area-system-io-ports the main issues this is fixing are: #45635, #2379, and #113889.

There is extra work in API approval for UnixHandleAsyncContext. I previously had feedback from @rzikm and @liveans the API is near a proposable shape: #47631 (comment).

@jkotas

jkotas commented Sep 30, 2026

Copy link
Copy Markdown
Member

There is extra work in API approval for UnixHandleAsyncContext. I previously had feedback from @rzikm and @liveans the API is near a proposable shape: #47631 (comment) .

We are planning to focus on improving threadpool performance in .NET 12 (cc @VSadov).

I would like to see an investigation to be done about what can be done to improve performance now that we have both threadpool in corelib. I do not think that having the two independent threadpools in Corelib that do not cooperate much is where we want to be. I expect that the API shape is likely to change as a result of this investigation.

@tmds

tmds commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

I would like to see an investigation to be done about what can be done to improve performance now that we have both threadpool in corelib. I do not think that having the two independent threadpools in Corelib that do not cooperate much is where we want to be. I expect that the API shape is likely to change as a result of this investigation.

I'm interested to see where this will go. I don't think of UnixHandleAsyncContext as a ThreadPool. I consider it an abstraction that delivers the readiness notification. It requires a thread because that is what epoll/kevent need.

This branch has not been deployed

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

Labels

area-System.IO.Ports blocked Issue/PR is blocked on something - see comments community-contribution Indicates that the PR has been added by a community member NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants