Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
_Py_closerange: last-minute braino, flip logic around
  • Loading branch information
kevans91 committed Oct 11, 2020
commit e1dc81975c036b622384e33ae4df0f41eb1bf193
2 changes: 1 addition & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8783,7 +8783,7 @@ _Py_closerange(int first, int last)
{
first = Py_MAX(first, 0);
#ifdef HAVE_CLOSE_RANGE
if (close_range(first, last, 0) == -1 && errno != ENOSYS) {
if (close_range(first, last, 0) == 0 || errno != ENOSYS) {
Copy link

Choose a reason for hiding this comment

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

It might be nicer to cache ENOSYS the first time we try close_range and avoid it in future _Py_closerange calls — assuming this is ever used more than once per process, which might not be true.

Copy link
Member

Choose a reason for hiding this comment

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

I implemented such cache in the PEP 446 implementation: remind if a syscall supports atomic "O_CLOEXEC" flag. See for example _Py_open_cloexec_works in Python/fileutils.c.

Copy link
Member

Choose a reason for hiding this comment

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

In my opinion, I don't think this code path is sensitive enough to really care.

Copy link
Member

Choose a reason for hiding this comment

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

ah, nice, I hadn't thought of caching that. good idea. (one less failed syscall attempt before the real work likely involving many syscalls gets done in this case; so not a huge deal but still worthwhile)

Copy link
Member

Choose a reason for hiding this comment

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

For the subprocess use case, the cache will likely only be first initialized in child processes, each child process has to do the check, and so the cache is useless.

I would mostly benefit to os.closerange().

I'm fine with not adding a cache. I agree that os.closerange() is an uncommon function.

/* Any errors encountered while closing file descriptors are ignored;
* ENOSYS means no kernel support, though,
* so we'll fallback to the other methods. */
Expand Down