Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41839: Fix error checking in sched_get_priority_ functions #22374

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kulikjak
Copy link
Contributor

@kulikjak kulikjak commented Sep 23, 2020

Python presumes that any negative number returned from sched_get_priority_min or sched_get_priority_max indicates that error has occurred. However, neither Linux manual pages nor POSIX.1-2001 specification forbids negative values to be returned; only -1 has a special meaning for error.

https://bugs.python.org/issue41839

@kulikjak
Copy link
Contributor Author

kulikjak commented Nov 2, 2020

If this seems too dangerous to merge to you, we can use the variant used by OmniOS instead, which is not as nice & simple, but definitely won't break existing stuff.

https://github.com/citrus-it/omnios-build/blob/python39/build/python39/patches/mod-posix-sched_priority.patch

@jstasiak
Copy link
Contributor

jstasiak commented Nov 5, 2020

Is it guaranteed that a successul syscall clears errno? Because if yes, then the if (max == -1) will still report legitimate -1 priority as an error. The illumos man page says "If unsuccessful, they return -1 and set errno to indicate the error." and I may be reading too much into this, but if errno is not set to indicate an error is a sufficient condition to conclude there's no error I think?

@kulikjak
Copy link
Contributor Author

kulikjak commented Nov 6, 2020

It is not guaranteed, and this simple case confirms that it is not being cleared:

errno = EINVAL;
res = sched_get_priority_min(SCHED_OTHER);
printf("%d %d\n", res, errno);  // EINVAL is still set

but we can still do it ourselves.

You are right that -1 is still a legitimate priority, and although I don't think it even happens in any Solaris (historically, some schedulers used -60 and 60, the newer ones are using positive values only), it is not impossible (and also, I might be wrong :)).

I updated the code, now it should always work on systems where sched_get_priority_* is POSIX.1-2001 compliant (which is True for Linux as well as all Unix systems) no matter the priority.

@kulikjak kulikjak force-pushed the fix-sched-error-checking branch 2 times, most recently from fe9ee56 to d1a27a6 Compare August 23, 2022 12:02
@kulikjak kulikjak force-pushed the fix-sched-error-checking branch from 5b7ab02 to 4d818a2 Compare March 20, 2023 13:42
kulikjak added a commit to kulikjak/cpython that referenced this pull request Mar 20, 2023
kulikjak added a commit to kulikjak/cpython that referenced this pull request May 9, 2023
@kulikjak kulikjak force-pushed the fix-sched-error-checking branch from 4d818a2 to f55a532 Compare November 7, 2023 09:59
@mtelka
Copy link

mtelka commented Feb 8, 2024

What needs to be done to see this PR merged? The change is obviously improving compatibility with POSIX and there is not known any regression.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants