➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author christian.heimes
Recipients christian.heimes, vstinner
Date 2021-11-28.10:13:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <[email protected]>
In-reply-to
Content
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.

[1] https://github.com/singlestore-labs/cpython/commit/0364554615c79b9364a0acf3038147a999ea2219

[2] https://stackoverflow.com/questions/12340695/how-to-check-if-a-given-file-descriptor-stored-in-a-variable-is-still-valid
History
Date User Action Args
2021-11-28 10:13:07christian.heimessetrecipients: + christian.heimes, vstinner
2021-11-28 10:13:06christian.heimessetmessageid: <[email protected]>
2021-11-28 10:13:06christian.heimeslinkissue45915 messages
2021-11-28 10:13:06christian.heimescreate