Skip to content

Add OpenStandardInputHandle, OpenStandardOutputHandle, and OpenStandardErrorHandle APIs - #123478

Merged
jkotas merged 21 commits into
mainfrom
copilot/add-console-std-handle-apis
Feb 4, 2026
Merged

Add OpenStandardInputHandle, OpenStandardOutputHandle, and OpenStandardErrorHandle APIs#123478
jkotas merged 21 commits into
mainfrom
copilot/add-console-std-handle-apis

Conversation

CopilotAI commented Jan 22, 2026

Copy link
Copy Markdown
Contributor
  • Removed Interop.Dup.cs references from Browser ItemGroup (lines 62-63)
  • Removed Interop.Dup.cs references from Unix ItemGroup (lines 226-227)
  • Verified Dup is no longer used anywhere in System.Console source code
  • All platform builds succeed without Dup.cs references

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

CopilotAIand others added 3 commits January 22, 2026 08:24
…rdErrorHandle APIs
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
CopilotAI changed the title [WIP] Add low-level APIs for getting STD IN/OUT/ERR handlesAdd OpenStandardInputHandle, OpenStandardOutputHandle, and OpenStandardErrorHandle APIsJan 22, 2026
CopilotAI requested a review from adamsitnikJanuary 22, 2026 08:47

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot please address my feedback

Comment threadsrc/libraries/System.Console/src/System/ConsolePal.Unix.cs Outdated
Comment threadsrc/libraries/System.Console/src/System/ConsolePal.Windows.cs Outdated
Comment threadsrc/libraries/System.Console/tests/ConsoleHandles.cs Outdated
Comment threadsrc/libraries/System.Console/tests/ConsoleHandles.cs
…ify tests
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot please address my feedback

Comment threadsrc/libraries/System.Console/src/System/ConsolePal.Unix.cs
Comment threadsrc/libraries/System.Console/tests/ConsoleHandles.cs
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot please address my feedback

Comment threadsrc/native/libs/System.Native/pal_io.h Outdated
Comment threadsrc/native/libs/System.Native/pal_io.c Outdated
Comment threadsrc/native/libs/System.Native/pal_io.c Outdated
Comment threadsrc/libraries/Common/src/Interop/Unix/System.Native/Interop.Fcntl.cs Outdated
Comment threadsrc/libraries/System.Console/src/System/ConsolePal.Unix.cs Outdated
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wrapped my head around error handling. Long story short, we need to keep throwing the same exception in all the places that were using Interop.Sys.FileDescriptors so far.

Before the introduction of the new public API, we were using Interop.CheckIo(Interop.Sys.Dup(FD)).
Dup would fail with EBADF if FD was invalid, so is fcntl. We need to return -1 in SystemNative_FcntlCheckAccess when fcntl fails.
When the access rights are not as requested, the SystemNative_FcntlCheckAccess should also set errno to EBADF and return -1. To mimic read/write sys-calls.
So SystemNative_FcntlCheckAccess should not return bool, but failure (invalid fd, wrong access) or success.

The OpenStandardInputHandle, OpenStandardOutputHandle and OpenStandardErrorHandle form ConsolePal.Unix.cs should accept an optional boolean parameter bool verifyFd: true. And When the call to SystemNative_FcntlCheckAcces fails, they should just call Interop.CheckIo when verifyFd is true.
When verifyFd is false, the method should just return raw handle value. Something like this:

publicstaticSafeFileHandleOpenStandardInputHandle(boolverifyFd=true)=>OpenStandardHandle(0,FileAccess.Read,verifyFd);publicstaticSafeFileHandleOpenStandardOutputHandle(boolverifyFd=true)=>OpenStandardHandle(1,FileAccess.Write,verifyFd);publicstaticSafeFileHandleOpenStandardErrorHandle(boolverifyFd=true)=>OpenStandardHandle(2,FileAccess.Write,verifyFd);privatestaticSafeFileHandleOpenStandardHandle(IntPtrfd,FileAccessaccess,boolverifyFd){if(verifyFd){Interop.CheckIo(Interop.Sys.Fcntl.CheckAccess(fd,(int)access));}returnnewSafeFileHandle(fd,ownsHandle:false);}

Now, all the places that were using Interop.Sys.FileDescriptors.STDIN_FILENO and similar directly like here, should pass verifyFd: false to ensure they don't suddenly start throwing an exception:

public static bool IsInputRedirectedCore()
{
- return IsHandleRedirected(Interop.Sys.FileDescriptors.STDIN_FILENO);+ return IsHandleRedirected(OpenStandardInputHandle(verifyFd: false));
}

This should allow to not introduce any breaking changes into existing public API

Comment threadsrc/libraries/System.Console/src/System/ConsolePal.Unix.cs Outdated
@adamsitnik

Copy link
Copy Markdown
Member

@copilot please address my feedback from #123478 (review) (I forgot to tag you)

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Comment threadsrc/libraries/System.Console/src/System.Console.csproj Outdated
…orms
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas

Copy link
Copy Markdown
Member

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jkotasjkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

Please check that the new tests pass in runtime-extra-platforms pipeline before merging.

@adamsitnikadamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@jkotas

Copy link
Copy Markdown
Member

/ba-g none of the failures in extra platforms look related to this change. Mostly infrastructure issues.

@jkotas
jkotas merged commit 2fac8ba into mainFeb 4, 2026
126 of 156 checks passed
@jkotas
jkotas deleted the copilot/add-console-std-handle-apis branch February 4, 2026 19:31
@adamsitnikadamsitnik added this to the 11.0.0 milestone Feb 6, 2026
lewing pushed a commit to lewing/runtime that referenced this pull request Feb 9, 2026
…rdErrorHandle APIs (dotnet#123478)
Fixesdotnet#122802
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
iremyux pushed a commit to iremyux/dotnet-runtime that referenced this pull request Mar 2, 2026
…rdErrorHandle APIs (dotnet#123478)
Fixesdotnet#122802
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Mar 9, 2026
@adamsitnikadamsitnik linked an issue Apr 2, 2026 that may be closed by this pull request
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Console: low-level APIs for getting STD IN/OUT/ERR handles

4 participants

@adamsitnik@jkotas@stephentoub