FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-35537: subprocess can use posix_spawn with pipes by vstinner · Pull Request #11575 · python/cpython · GitHub

/ cpython Public

bpo-35537: subprocess can use posix_spawn with pipes - #11575

Merged
vstinner merged 1 commit into
python:masterfrom
vstinner:subprocess_spawn2
Jan 23, 2019
Merged

bpo-35537: subprocess can use posix_spawn with pipes#11575
vstinner merged 1 commit into
python:masterfrom
vstinner:subprocess_spawn2

Conversation

vstinner commented Jan 16, 2019
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member
  • subprocess.Popen can now also use os.posix_spawn() with pipes if
    pipe file descriptors are greater than 2.
  • Fix Popen._posix_spawn(): set _child_created to True.
  • Add Popen._close_pipe_fds() helper function to factorize the code.

https://bugs.python.org/issue35537

Copy link
Copy Markdown
Member Author

Follow-up of PR #11452.

Copy link
Copy Markdown
Member Author

Copy link
Copy Markdown
Member Author

PR rebased on top of PR #11579 (commit 0785889).

pablogsal self-requested a review January 16, 2019 14:34
Comment thread Lib/subprocess.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Technically, there is no need to explicitly close parent pipe ends because they have O_CLOEXEC. One small advantage of doing that is to reduce the number of descriptors before the following dup2, which could increase their number (in a corner case when any of descriptors 0, 1, 2 is closed in the parent process) and potentially hit resource limits.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Technically, there is no need, but _posixsubprocess does that. In case of doubt, I prefer to mimick _posixsubprocess behavior.

    /* Close parent's pipe ends. */
    if (p2cwrite != -1)
        POSIX_CALL(close(p2cwrite));
    if (c2pread != -1)
        POSIX_CALL(close(c2pread));
    if (errread != -1)
        POSIX_CALL(close(errread));
    POSIX_CALL(close(errpipe_read));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If we decide to skip the close(), I would prefer to also modify _posixsubprocess. But here I only want to use posix_spawn() in more cases, so I prefer to not modify "unrelated" changes :-)

Comment thread Lib/subprocess.py Outdated
* subprocess.Popen can now also use os.posix_spawn() with pipes if
  pipe file descriptors are greater than 2.
* Fix Popen._posix_spawn(): set _child_created to True.
* Add Popen._close_pipe_fds() helper function to factorize the code.

Copy link
Copy Markdown
Member Author

PR rebased on top of PR #11579 (commit 0785889).

Since PR #11579, I rebased this PR again :-)

Copy link
Copy Markdown
Member Author

@gpshead @giampaolo: would you mind to review this change? It allows to use posix_spawn() in subprocess in much more cases.

Copy link
Copy Markdown
Member Author

If I don't get any feedback before the end of the week, I will just merge my PR. I prefer to merge it early during the 3.8 development cycle, to get more time to fix it if something goes wrong :)

Comment thread Lib/subprocess.py
os.close(errwrite)

if devnull_fd is not None:
os.close(devnull_fd)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

In case of premature failure on X.Close() or os.close(X) the remaining pipes/fds will remain "open". Perhaps it makes sense to use contextlib.ExitStack.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is it a regression caused by my PR? Or a general remark on existing code? My PR just moves code, no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yep, it doesn't look like a regression introduced by you (was already there).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

At least, I factorized the code so it should help to enhance the code ;-) Maybe open an issue if you want to work on that? I don't see an obvious pattern to ensure that all file descriptors are properly closed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, makes sense. I bet there are many other places which can benefit from ExitStack in a similar manner.

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

vstinner left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@giampaolo: So what do you think of the overall change? :-)

Comment thread Lib/subprocess.py
os.close(errwrite)

if devnull_fd is not None:
os.close(devnull_fd)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

At least, I factorized the code so it should help to enhance the code ;-) Maybe open an issue if you want to work on that? I don't see an obvious pattern to ensure that all file descriptors are properly closed.

giampaolo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

vstinner merged commit f6243ac into python:master Jan 23, 2019
vstinner deleted the subprocess_spawn2 branch January 23, 2019 18:00

Copy link
Copy Markdown
Member Author

@giampaolo: "LGTM"

Yay! Thanks for the approval! I merged my PR.

Copy link
Copy Markdown
Member Author

@giampaolo: As I wrote, please open a new issue if you would like to enhance the new _close_pipe_fds() helper method.

Copy link
Copy Markdown
Contributor

@vstinner here it is: #11686

gpshead pushed a commit that referenced this pull request Jan 29, 2019
Close pipes/fds in subprocess by using ExitStack.

"In case of premature failure on X.Close() or os.close(X) the remaining pipes/fds will remain "open". Perhaps it makes sense to use contextlib.ExitStack."
- Rationale: #11575 (comment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

5 participants


Back | FazBrowse Home | New Git URL