Skip to content

multiprocessing.conenction.Connection.fileno does not return a valid fd on windows #141642

@ergoithz

Description

@ergoithz

Bug report

Bug description:

multiprocessing.conenction.Connection.fileno is returning a raw Windows file handle (unusable for os operations) instead of the corresponding python file descriptor from msvcrt.open_osfhandle.

This is somewhat documented as fileno() Return the file descriptor or handle used by the connection. (emphasis on handle), but this quirk is probably unjustified as msvcrt supports going back an forth between a proper fd for named pipes (actual file objects on windows, unlike sockets) so this is just a quirk maybe meant to support socket handles.

I think the right behavior for a file object on windows would be returning a python file descriptor instead of the raw handle (which isn't supported by most of stdlib), and so a fundamentally different case to winsock2 socket handles (which is directly supported by socket, asyncio, etc).

Simple test to reproduce.

import errno
import msvcrt  # windows-only
import multiprocessing
import os
import stat

conn_a, conn_b = multiprocessing.Pipe()

handle = conn_a.fileno()
try:
    os.fstat(handle)

except OSError as e:
    print(f'{errno.errorcode[e.errno]}: handle {handle} is not a valid fd!')

fd = msvcrt.open_osfhandle(handle, os.O_BINARY)
if stat.S_ISFIFO(os.fstat(fd).st_mode):
    print(f'fd {fd} from handle {handle} points to a pipe')

Test output.

EBADF: handle 132 is not a valid fd!
fd 3 from handle 132 points to a pipe

Since this is Windows-only, I'm using docker and wine to reproduce this myself in a proper OS.

docker run --volume .:/src --name pywine --rm -it --entrypoint wine tobix/pywine:3.14 python /src/connection_fileno_bug.py

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowspendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions