Skip to content

Conversation

@raixyzaditya
Copy link

@raixyzaditya raixyzaditya commented Dec 6, 2025

Description

Fixes the misleading documentation for tempfile.mkstemp() regarding file descriptor inheritance.

The current documentation states "The file descriptor is not inherited by child processes," which is incomplete and misleading.

What was wrong

The file descriptor created by mkstemp() is inherited by child processes created via fork(). The close-on-exec flag (O_CLOEXEC), when available, only prevents inheritance across exec() calls, not fork() calls.

Changes made

Updated the documentation to accurately describe the inheritance behavior:

  • Clarifies that O_CLOEXEC only affects exec() calls
  • Notes that the fd is still inherited by forked child processes
  • Maintains technical accuracy while being clear for users

Testing

Verified the documentation builds correctly:

cd Doc
make html

Related Issue

Fixes #142302


📚 Documentation preview 📚: https://cpython-previews--142338.org.readthedocs.build/

The documentation incorrectly stated that the file descriptor is not
inherited by child processes. In reality, the close-on-exec flag (when
available) only prevents inheritance across exec() calls, not fork().

The fd will still be inherited by forked child processes.
@python-cla-bot
Copy link

python-cla-bot bot commented Dec 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz
Copy link
Member

picnixz commented Dec 6, 2025

Please sign the CLA and address the CI issues. In addition, please wait for the maintainer to confirm that the documentation should be changed like that.

@picnixz picnixz marked this pull request as draft December 6, 2025 12:50
@raixyzaditya raixyzaditya changed the title Fix mkstemp() documentation: clarify file descriptor inheritance behavior gh-142302: Fix mkstemp() documentation: clarify file descriptor inheritance behavior Dec 6, 2025
Adjust indentation to match the rest of the file formatting.
@raixyzaditya
Copy link
Author

Thank you for your review
CLA: I have signed the CLA.
CI Issues: All tests are now passing. The CI issues have been resolved.
Documentation Change: I'm happy to wait for maintainer confirmation on the wording. The current documentation states "The file descriptor is not inherited by child processes," which is misleading since:

  • The fd IS inherited by forked child processes
  • Only exec() calls are affected by O_CLOEXEC (when available)
  • This has been verified empirically in production code
    I'm open to alternative wording suggestions if the current approach needs adjustment. Please let me know if there's a preferred way to clarify this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Tempfile.mkstemp file descriptor issue

2 participants