Repository navigation
The os.sched_yield() implementation in posixmodule.c doesn't release the GIL #96078
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 18, 2022 - addedextension-modulesC modules in the Modules dirC modules in the Modules dir3.11only security fixesonly security fixes3.10 (EOL)end of lifeend of life3.12only security fixesonly security fixes
on Aug 18, 2022 Interestingly,
os.sched_yield()was added 11 years ago, and no one has ever mentioned this oversight. I guess the users, if any1, are satisfied that it can only yield execution to ready threads that aren't waiting to acquire the GIL. The behavior was never documented, so I'm tagging this as a bug in 3.10+.Footnotes
-
The man page for
sched_yield()in Linux says the behavior is "unspecified" if the process isn't using a deterministic realtime scheduler such asSCHED_FIFO(first in, first out) orSCHED_RR(round robin). I'm guessing it's not common for Python applications to use realtime scheduling. ↩
-
[...] using a deterministic realtime scheduler such as
SCHED_FIFO(first in, first out) orSCHED_RR(round robin). I'm guessing it's not common for Python applications to use realtime scheduling. ↩That is exactly what I do - using
SHED_FIFOwith two threads I sometimes callos.sched_yield()to ensure that the second thread was executed as freshly as possible.Maybe it's a good idea to add an annotation in docs for older cpythons that os.sched_yield() doesn't release the GIL?
- added a commit that references this issue
on Oct 6, 2022 - added 4 commits that reference this issue
on Oct 7, 2022 - added a commit that references this issue
on Oct 8, 2022 - added a commit that references this issue
on Oct 11, 2022 - added a commit that references this issue
on Aug 9, 2026
I've reviewed the
mainbranch and the GIL isn't released: https://github.com/python/cpython/blob/main/Modules/posixmodule.c#L7060The forum thread: https://discuss.python.org/t/does-the-os-sched-yield-release-the-gil/18301
Expected: The GIL is released before sched_yield() and acquired after that.