Skip to content

Add API for detecting broken pipe when write to stdout(or stderr) #30540

Closed as not planned

Description

@itn3000

currently, corefx's System.Console.Out implementation is ignoring broken pipe, just pretend to success.(due to #15587)

This behavior is sometimes inconvenient when work together with some command like head command.
if dotnet command output is very long, there is no easy way to stop program if command which follows after pipe stop (except using native API, but it is sometimes very difficult).

So, I want the API to detect whether stdout(and maybe stderr) is broken.

Activity

  1. transferred this issue fromdotnet/corefxon Feb 1, 2020
  2. added this to the 5.0 milestone on Feb 1, 2020
  3. added
    untriaged
    on Feb 23, 2020
  4. modified the milestones: 5.0.0, Future on Jun 29, 2020
  5. removed
    untriaged
    on Jul 6, 2020
  6. added
    needs-further-triage
    on Jan 27, 2021
  7. removed their assignment
    on Oct 11, 2021
  8. removed their assignment
    on Nov 13, 2023
  9. adamsitnik commented on Nov 14, 2023

    @adamsitnik
    Member

    Triage: I don't believe that we can provide reliable API for this feature request. To check whether pipe is broken, we would need to perform a write. Writing 0 bytes has a special meaning (#81053), so writing 0 bytes is not an option. Changing the behavior to always throw in such cases is also not an option, as it would break plenty of apps (especially those which are printing to console while the process is shutting down).

    Since there was only one up-vote, the best I can do right now is to provide a workaround:

    static void Workaround(string text, out bool brokenPipe)
    {
        Console.Write(text);
        brokenPipe = Marshal.GetLastWin32Error() == (OperatingSystem.IsWindows() ? 0x6D : 0x10043);
    }
  10. locked and limited conversation to collaborators on Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Consoleneeds-further-triage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions