Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Jul 11, 2025

It's not always possible to guarantee that the file was opened with O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.

It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
@vstinner
Copy link
Member Author

The test fails on Arch Linux: #136281 (comment)

cc @serhiy-storchaka

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Is removing the test the only way? Can we check _O_TMPFILE_WORKS after calling TemporaryFile()?

@vstinner
Copy link
Member Author

vstinner commented Jul 11, 2025

Is removing the test the only way? Can we check _O_TMPFILE_WORKS after calling TemporaryFile()?

The test already checks _O_TMPFILE_WORKS after TemporaryFile():

            with tempfile.TemporaryFile('w', dir=dir) as tmp:
                # the flag can become False on Linux <= 3.11
                if not tempfile._O_TMPFILE_WORKS:
                    self.skipTest("O_TMPFILE doesn't work")

The problem is that TemporaryFile() doesn't set _O_TMPFILE_WORKS to False on OSError (other than IsADirectoryError), it only ignores the error silently.

@vstinner vstinner merged commit cbf007b into python:main Jul 11, 2025
45 checks passed
@vstinner vstinner deleted the remove_test_link_tmpfile branch July 11, 2025 14:45
@vstinner
Copy link
Member Author

I removed the test. It can be added back if someone finds a clever trick to check if O_TMPFILE was used or not. I don't know how to query open flags from a file descriptor.

@serhiy-storchaka
Copy link
Member

What was that error? Why _O_TMPFILE_WORKS was not set here?

@vstinner
Copy link
Member Author

What was that error? Why _O_TMPFILE_WORKS was not set here?

I don't have access to the buildbot, so I don't know. But I suspect that we hit this code path:

            except OSError:
                # The filesystem of the directory does not support O_TMPFILE.
                # For example, OSError(95, 'Operation not supported').
                #
                # On Linux kernel older than 3.11, trying to open a regular
                # file (or a symbolic link to a regular file) with O_TMPFILE
                # fails with NotADirectoryError, because O_TMPFILE is read as
                # O_DIRECTORY.
                pass
            # Fallback to _mkstemp_inner().

Later, linkat() fails with FileNotFoundError: #136281 (comment)

@vstinner
Copy link
Member Author

Example trying to call linkat() whereas O_TMPFILE is not used:

import os, tempfile
dir = '.'
flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
fd, name = tempfile._mkstemp_inner(dir, "tmp", "", flags, str)
os.unlink(name)
try:
    os.link(f'/proc/self/fd/{fd}', 'link', follow_symlinks=True)
finally:
    os.close(fd)

It fails with:

Traceback (most recent call last):
  File "/home/vstinner/python/main/x.py", line 7, in <module>
    os.link(f'/proc/self/fd/{fd}', 'link', follow_symlinks=True)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/proc/self/fd/3' -> 'link'

@serhiy-storchaka
Copy link
Member

There may be a bug in TemporaryFile.

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
picnixz pushed a commit to picnixz/cpython that referenced this pull request Jul 13, 2025
It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
It's not always possible to guarantee that the file was opened with
O_TMPFILE even if tempfile._O_TMPFILE_WORKS is true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants