Skip to content

The os.sched_yield() implementation in posixmodule.c doesn't release the GIL #96078

Description

@FilipDepta

I've reviewed the main branch and the GIL isn't released: https://github.com/python/cpython/blob/main/Modules/posixmodule.c#L7060

The 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.

Activity

  1. eryksun commented on Aug 18, 2022

    @eryksun
    Contributor

    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

    1. The man page for sched_yield() in Linux says the behavior is "unspecified" if the process isn't using a deterministic realtime scheduler such as SCHED_FIFO (first in, first out) or SCHED_RR (round robin). I'm guessing it's not common for Python applications to use realtime scheduling. ↩

  2. FilipDepta commented on Aug 19, 2022

    @FilipDepta
    Author

    [...] using a deterministic realtime scheduler such as SCHED_FIFO (first in, first out) or SCHED_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_FIFO with two threads I sometimes call os.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?

  3. added 2 commits that reference this issue on Oct 6, 2022
  4. added a commit that references this issue on Oct 6, 2022
    e5d2169
  5. added 4 commits that reference this issue on Oct 7, 2022
    920e025
    4a38944
    e39b511
    11945f2
  6. added a commit that references this issue on Oct 8, 2022
    415ed49
  7. added a commit that references this issue on Oct 11, 2022
    0c970bd
  8. added a commit that references this issue on Aug 9, 2026
    b9d2e81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixes3.12only security fixesextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions