You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use fcntl(fd, F_GETFD) to check whether an fd is valid #90073
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-11-28.10:13:06.935>labels= ['expert-C-API', 'type-bug', '3.11']
title='Use fcntl(fd, F_GETFD) to check whether an fd is valid'updated_at=<Date2021-12-14.03:31:17.636>user='https://github.com/tiran'
is_valid_fd() uses dup() or fstat() to check whether an fd is valid. Both operations are costly.
fcntl() with F_GETFD returns the file descriptor flags (e.g. CLOEXEC) and -1 with errno set to EBADF when fd is not an open file descriptor. It's faster than duplicating + closing a fd or calling fstat(). The idea to use fcntl(fd, F_GETFD) is inspired by the patch [1]. According to Stackoverflow [2]:
fcntl(fd, F_GETFD) is the canonical cheapest way to check that fd is a valid open file descriptor.
F_GETFD is cheaper in principle since it only dereferences the (process-local) file descriptor in kernel space, not the underlying open file description (process-shared) which it refers to.
PR 29821 adds __APPLE__ to the platforms that use fcntl(fd, F_GETFD). Is this okay on macOS, given bpo-30225? Apparently fstat() fails if the other end of a pipe is closed.
Activity
tiran commentedon Nov 28, 2021
is_valid_fd() uses dup() or fstat() to check whether an fd is valid. Both operations are costly.
fcntl() with F_GETFD returns the file descriptor flags (e.g. CLOEXEC) and -1 with errno set to EBADF when fd is not an open file descriptor. It's faster than duplicating + closing a fd or calling fstat(). The idea to use fcntl(fd, F_GETFD) is inspired by the patch [1]. According to Stackoverflow [2]:
[1] singlestore-labs@0364554
[2] https://stackoverflow.com/questions/12340695/how-to-check-if-a-given-file-descriptor-stored-in-a-variable-is-still-valid
tiran commentedon Nov 28, 2021
New changeset f87ea03 by Christian Heimes in branch 'main':
bpo-45915: use fcntl(fd, F_GETFD) in is_valid_fd() (GH-29821)
f87ea03
eryksun commentedon Nov 28, 2021
I've created bpo-45919 with a suggested enhancement to use GetFileType() in Windows, since the Windows C runtime does not provide fcntl().
tiran commentedon Nov 28, 2021
Thank you!
eryksun commentedon Dec 14, 2021
PR 29821 adds __APPLE__ to the platforms that use fcntl(fd, F_GETFD). Is this okay on macOS, given bpo-30225? Apparently fstat() fails if the other end of a pipe is closed.
kumaraditya303 commentedon May 9, 2022
Fixed by #29821