Skip to content

Add OpenStandardInputHandle, OpenStandardOutputHandle, and OpenStandardErrorHandle APIs#123478

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

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

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

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

Copilot AI and 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>
Copilot AI changed the title [WIP] Add low-level APIs for getting STD IN/OUT/ERR handles Add OpenStandardInputHandle, OpenStandardOutputHandle, and OpenStandardErrorHandle APIs Jan 22, 2026
Copilot AI requested a review from adamsitnik January 22, 2026 08:47
Copy link
Member

@adamsitnik adamsitnik left a comment

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

…ify tests

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copy link
Member

@adamsitnik adamsitnik left a comment

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

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copy link
Member

@adamsitnik adamsitnik left a comment

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

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copy link
Member

@adamsitnik adamsitnik left a comment

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:

public static SafeFileHandle OpenStandardInputHandle(bool verifyFd = true) => OpenStandardHandle(0, FileAccess.Read, verifyFd);

public static SafeFileHandle OpenStandardOutputHandle(bool verifyFd = true) => OpenStandardHandle(1, FileAccess.Write, verifyFd);

public static SafeFileHandle OpenStandardErrorHandle(bool verifyFd = true) => OpenStandardHandle(2, FileAccess.Write, verifyFd);

private static SafeFileHandle OpenStandardHandle(IntPtr fd, FileAccess access, bool verifyFd)
{
    if (verifyFd)
    {
        Interop.CheckIo(Interop.Sys.Fcntl.CheckAccess(fd, (int)access));
    }

    return new SafeFileHandle(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

@adamsitnik
Copy link
Member

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

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
…soleStream to BrowserConsoleStream

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
…orms

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@jkotas
Copy link
Member

jkotas commented Feb 3, 2026

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@jkotas jkotas left a comment

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.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

LGTM!

@jkotas
Copy link
Member

jkotas commented Feb 4, 2026

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

@jkotas jkotas merged commit 2fac8ba into main Feb 4, 2026
126 of 156 checks passed
@jkotas jkotas deleted the copilot/add-console-std-handle-apis branch February 4, 2026 19:31
@adamsitnik adamsitnik added this to the 11.0.0 milestone Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants