Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: subprocess: inheritance of std descriptors inconsistent #42127

Closed
ndparker mannequin opened this issue Jun 26, 2005 · 8 comments
Closed

doc: subprocess: inheritance of std descriptors inconsistent #42127

ndparker mannequin opened this issue Jun 26, 2005 · 8 comments
Labels
3.12 bugs and security fixes docs Documentation in the Doc dir easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ndparker
Copy link
Mannequin

ndparker mannequin commented Jun 26, 2005

BPO 1227748
Nosy @ndparker, @eryksun, @iritkatriel
Files
  • 1227748.patch: Doc patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2005-06-26.13:37:33.000>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'library', 'docs']
    title = 'doc: subprocess: inheritance of std descriptors inconsistent'
    updated_at = <Date 2021-04-29.11:14:57.614>
    user = 'https://github.com/ndparker'

    bugs.python.org fields:

    activity = <Date 2021-04-29.11:14:57.614>
    actor = 'vstinner'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'Library (Lib)']
    creation = <Date 2005-06-26.13:37:33.000>
    creator = 'ndparker'
    dependencies = []
    files = ['20411']
    hgrepos = []
    issue_num = 1227748
    keywords = ['patch', 'easy']
    message_count = 6.0
    messages = ['25643', '25644', '63330', '126315', '392244', '392270']
    nosy_count = 7.0
    nosy_names = ['astrand', 'ndparker', 'kiilerix', 'docs@python', 'rosslagerwall', 'eryksun', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1227748'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Linked PRs

    @ndparker
    Copy link
    Mannequin Author

    ndparker mannequin commented Jun 26, 2005

    The inheritance of std descriptors is inconsistent
    between Unix and Windows implementations.

    If one calls Popen with stdin = stdout = stderr = None,
    the caller's std descriptors are inherited on *x, but
    not on Windows, because of the following optimization
    (from subprocess.py r1.20):

    655 def _get_handles(self, stdin, stdout,
    stderr):
    656 """Construct and return tupel with
    IO objects:
    657 p2cread, p2cwrite, c2pread,
    c2pwrite, errread, errwrite
    658 """
    659 if stdin is None and stdout is None
    and stderr is None:
    660 return (None, None, None, None,
    None, None)
    661

    I suggest to just remove those lines 659 and 660. The
    current workaround is to duplicate the handles by the
    application and supply an own STARTUPINFO structure.

    @ndparker ndparker mannequin assigned astrand Jun 26, 2005
    @ndparker ndparker mannequin added the stdlib Python modules in the Lib dir label Jun 26, 2005
    @ndparker ndparker mannequin assigned astrand Jun 26, 2005
    @ndparker ndparker mannequin added the stdlib Python modules in the Lib dir label Jun 26, 2005
    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Jan 29, 2007

    If one calls Popen with stdin = stdout = stderr = None,
    the caller's std descriptors are inherited on *x, but
    not on Windows,

    This is a correct observation. However, the current implementation is not necessarily wrong. This could instead be seen as a consequence of the different environments. The subprocess documentation states that "With None, no redirection will occur". So, it becomes an interpretation of what this really mean. Since the "default" behaviour on UNIX is to inherit and the default behaviour on Windows is to attach the standard handles to (an often newly created) console window, one could argue that this fits fairly good with the description "no redirection will occur".

    If we would change this, so that the parents handles are always inherited, then how would you specify that you want to attach the standard handles to the new console window?

    For best flexibility, the API should allow both cases: Both inherit all handles from the parent as well as attaching all standard handles to the new console window. As you point out, the current API allows this. So why change this?

    One thing that's clearly an bug is the second part of the documentation:

    "With None, no redirection will occur; the child's file handles will be inherited from the
    parent"

    This is currently only true on UNIX. If we should keep the current behaviour, at least the comment needs to be fixed.

    @kiilerix
    Copy link
    Mannequin

    kiilerix mannequin commented Mar 6, 2008

    Note to others searching for a solution to this and similar problems:
    http://svn.python.org/view/python/trunk/Lib/subprocess.py?rev=60115&view=auto
    shows that this now (for 2.6?) has been changed so that close_fds now
    controls inheritance through the CreateProcess parameter bInheritHandles

    Thanks!

    @BreamoreBoy BreamoreBoy mannequin added type-bug An unexpected behavior, bug, or error labels Aug 21, 2010
    @rosslagerwall
    Copy link
    Mannequin

    rosslagerwall mannequin commented Jan 15, 2011

    I think all that is needed is a documentation patch. Attached is a doc patch which changes the doc to explicitly describe what happens on unix & windows (as described by Peter).

    @rosslagerwall rosslagerwall mannequin added docs Documentation in the Doc dir labels Jan 15, 2011
    @ned-deily ned-deily assigned docspython and unassigned astrand Feb 5, 2011
    @iritkatriel
    Copy link
    Member

    The patch needs to be converted into a github PR.

    @iritkatriel iritkatriel added easy 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Apr 28, 2021
    @iritkatriel iritkatriel changed the title subprocess: inheritance of std descriptors inconsistent doc: subprocess: inheritance of std descriptors inconsistent Apr 28, 2021
    @iritkatriel iritkatriel changed the title subprocess: inheritance of std descriptors inconsistent doc: subprocess: inheritance of std descriptors inconsistent Apr 28, 2021
    @eryksun
    Copy link
    Contributor

    eryksun commented Apr 29, 2021

    If one calls Popen with stdin = stdout = stderr = None,
    the caller's std descriptors are inherited on *x, but
    not on Windows

    In the default case, CreateProcessW() is called with bInheritHandles as false and without explicitly setting the standard-handle values. Also, creationflags is 0 in the default case, not CREATE_NEW_CONSOLE, CREATE_NO_WINDOW, or DETACHED_PROCESS. In this case, the child's standard handles are implicitly duplicated from the parent if the child shares the parent's console session. Contra "1227748.patch", the standard handles are not "attached to the console window". For example, if the parent's stdout is redirected to a handle for an open disk file, the child's stdout will be redirected to the same open file. It has nothing to do with a window, and the console session doesn't even need to have a window (i.e. created with CREATE_NO_WINDOW; or a pseudoconsole session).

    In particular, if the executable image of the child is a console application, the system duplicates the parent's standard handles to the child, as well as a handle for the parent's console session, if any. If the child can connect to the parent's console at startup, then the duplicated standard handles are used, whatever they are. (In general, they need to be handles for pipe, character, or disk files opened in synchronous mode, as required by C standard I/O.) If the child can't connect to a console session, it allocates a new console and opens the console "Input" and "Output" as its standard handles.

    On the other hand, if the executable image of the child is not a console application, the system does not implicitly duplicate standard handles from the parent to the child. The window manager and graphical shell even explicitly reuse the standard-handle values (via STARTUPINFO) to implement features that aren't I/O file related when running GUI apps, including configuring an activation hotkey (stdin) or passing a handle for the preferred output monitor (stdout).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    @LeamHall
    Copy link

    I have submitted a discussion to the core-mentorship list, with a possible resolution. Since this is my first contribution, trying to get good feedback.

    @ethanfurman
    Copy link
    Member

    Due to multiple core-dev conflicting recomendations, and in agreement with @LeamHall's observation:

    I recommend that #42127 (#42127) be closed with no code changes. It is not a bug, it is a difference of opinion. In the eighteen years this issue has been open, the Windows development folks have been able to do their job.

    Closing this issue.

    @ethanfurman ethanfurman closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes docs Documentation in the Doc dir easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants