Skip to content

gh-139184: set O_CLOEXEC for master_fd when call os.forkpty#139408

Merged
vstinner merged 16 commits into
python:mainfrom
Zheaoli:manjusaka/fix-139184
Oct 10, 2025
Merged

gh-139184: set O_CLOEXEC for master_fd when call os.forkpty#139408
vstinner merged 16 commits into
python:mainfrom
Zheaoli:manjusaka/fix-139184

Conversation

@Zheaoli

@Zheaoli Zheaoli commented Sep 29, 2025

Copy link
Copy Markdown
Contributor

Signed-off-by: Manjusaka <me@manjusaka.me>
Comment thread Lib/test/test_pty.py Outdated
Zheaoli and others added 2 commits September 29, 2025 17:42
Co-authored-by: Shamil <ashm.tech@proton.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Comment thread Lib/test/test_pty.py Outdated
Comment thread Lib/test/test_pty.py Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Misc/NEWS.d/next/Library/2025-09-29-14-15-20.gh-issue-139184.dNl9O4.rst Outdated
Signed-off-by: Manjusaka <me@manjusaka.me>
@Zheaoli

Zheaoli commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

@vstinner Thanks for your time. PTAL when you get time

Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Comment thread Doc/library/os.rst Outdated
Signed-off-by: Manjusaka <me@manjusaka.me>
@Zheaoli

Zheaoli commented Oct 9, 2025

Copy link
Copy Markdown
Contributor Author

@vstinner Done

Comment thread Modules/posixmodule.c Outdated

@picnixz picnixz 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.

os.* functions are meant to mimic libc ones. As such, I'm not sure we should forcibly change inheritance (or does forkpty actually sets O_CLOEXEC as well?). Instead I would suggest one of the two:

  • add an additional parameter to forkpty to allow setting O_CLOEXEC at the same time.
  • add a separate function (I don't have a name for it) that does it.

Comment thread Doc/library/os.rst Outdated
Comment thread Doc/library/pty.rst Outdated
Comment thread Modules/posixmodule.c
if (pid == -1) {
return posix_error();
}

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 don't think you need this change.

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 like this change, it helps readability :-)

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.

Considering the function is already very compact, I don't think it helps much. But I wouldn't mind having more blank lines in the entire function to really help readability (the return is usually sufficiently clear IMO; I'm more worried about the all those ifs before actually calling forkpty)

Comment thread Modules/posixmodule.c
@vstinner

vstinner commented Oct 9, 2025

Copy link
Copy Markdown
Member

os.* functions are meant to mimic libc ones. As such, I'm not sure we should forcibly change inheritance

There is PEP 446 "Make newly created file descriptors non-inheritable" which requires Python to create new file descriptors as non-inheritable.

I see this change as a bugfix, but I prefer to not backport it to avoid bad surprises to users.

Zheaoli and others added 5 commits October 9, 2025 22:36
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>

@vstinner vstinner 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. Thanks for the update! It now looks good to me with PyErr_FormatUnraisable().

Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
@vstinner vstinner merged commit 7cafd76 into python:main Oct 10, 2025
81 of 83 checks passed
@vstinner

Copy link
Copy Markdown
Member

Merged, thank you.

@Zheaoli

Zheaoli commented Oct 11, 2025

Copy link
Copy Markdown
Contributor Author

Merged, thank you.

@vstinner @picnixz thanks for your time!

@Zheaoli Zheaoli deleted the manjusaka/fix-139184 branch October 11, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants