Skip to content

Newly added test_cmd_line_script.test_script_as_dev_fd() fails on FreeBSD: /dev/fd/3 doesn't exist #100005

Description

@vstinner

I propose to skip test_cmd_line_script.test_script_as_dev_fd() on FreeBSD. Here is why.

cc @Jehops @emaste @koobs

The test added by PR #99768 fails on FreeBSD. test_cmd_line_script.test_script_as_dev_fd() fails with:

FAIL: test_script_as_dev_fd (test.test_cmd_line_script.CmdLineTest.test_script_as_dev_fd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/home/buildbot/python/3.x.koobs-freebsd-9e36.nondebug/build/Lib/test/test_cmd_line_script.py", line 766, in test_script_as_dev_fd
    self.assertEqual(out, b"12345678912345678912345\n")
AssertionError: b"/usr/home/buildbot/python/3.x.koobs-free[94 chars]ry\n" != b'12345678912345678912345\n'
Stderr:
/usr/home/buildbot/python/3.x.koobs-freebsd-9e36.nondebug/build/Lib/subprocess.py:849: RuntimeWarning: pass_fds overriding close_fds.
  warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)

Note: Failure first reported in issue #99985.

FreeBSD /dev/fd/ behaves differently than Linux /dev/fd/. I'm not sure why. If the parent opens a file and the file descriptor is inherited, the child process can open the file, but /dev/fd/ only contains file descriptors 0, 1, 2. Example:

parent.py:

import subprocess
import sys

script = 'print("Hello")'

script_name = 'script.py'
with open(script_name, 'w') as fp:
    fp.write(script)

with open(script_name, "r") as fp:
    fd = fp.fileno()
    print("FD", fd)

    cmd = [sys.executable]
    p = subprocess.Popen(cmd, close_fds=False, pass_fds=(0, 1, 2, fd))
    p.wait()

Child process:

$ ./python y.py 
FD 3

/usr/home/vstinner/python/main/Lib/subprocess.py:849: RuntimeWarning: pass_fds overriding close_fds.
  warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)

Python 3.12.0a2+ (heads/main:e3a3863cb9, Dec  5 2022, 12:09:39) [Clang 13.0.0 (git@github.com:llvm/llvm-project.git llvmorg-13.0.0-0-gd7b669b3a3 on freebsd13
Type "help", "copyright", "credits" or "license" for more information.
>>> import os

# The process has 4 file descriptors:
>>> fd=os.dup(0); os.close(fd)
>>> fd=os.dup(1); os.close(fd)
>>> fd=os.dup(2); os.close(fd)
>>> fd=os.dup(3); os.close(fd)

# But /dev/fd/3 doesn't exist
>>> os.listdir("/dev/fd")
['0', '1', '2']

>>> f=open("/dev/fd/3", "rb")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/dev/fd/3'

# Using directly file descriptor 3 works as expected:
>>> f=open(3, "rb")
>>> f.close()

Linked PRs

Activity

  1. added a commit that references this issue on Dec 5, 2022
  2. added 2 commits that reference this issue on Dec 5, 2022
  3. vstinner commented on Dec 5, 2022

    @vstinner
    MemberAuthor

    Fixed by 038b151

  4. added a commit that references this issue on Oct 8, 2024
  5. added a commit that references this issue on Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions