Skip to content

_thread module docs: update yet another bullet point in the *caveats* list #125058

Closed

Description

@zuo

Documentation

[Note: This issue is, in a way, a continuation of gh-125025.]

Near the end of the documentation of the _thread module there is a list of caveats. One of them – the third one – seems no longer accurate:

It is not possible to interrupt the acquire() method on a lock — the KeyboardInterrupt exception will happen after the lock has been acquired.

– whereas it appears that (since Python 3.2) lock acquisitions can be interrupted by signals, at least on platforms using Pthreads.

Linked PRs

Activity

  1. added
    docsDocumentation in the Doc dir
    on Oct 7, 2024
  2. zuo commented on Oct 7, 2024

    @zuo
    ContributorAuthor

    The question is whether nowadays the possibility to interrupt a blocking acquisition of a lock is possible only on POSIX systems (I can observe that on my Linux system), or maybe also on others? (i.e., Windows?)

    Unfortunately I am not sure what is the right answer, therefore a Python threading expert needs to speak up.

    According to the Experts Index the right persons are you, @gpshead and @pitrou. I hope, this is the right way to ask you for help?

    Thank you in advance! :)

  3. zuo commented on Oct 7, 2024

    @zuo
    ContributorAuthor

    PS I'd happy to provide a PR with an appropriate docs patch – when I'm sure what is the state of affairs when it comes the possibility to interrupt a lock acquisition on platforms with a non-Pthreads-based threads implementation.

  4. gpshead commented on Oct 7, 2024

    @gpshead
    Member

    Yes POSIXish platforms should happily allow interrupting threading.Lock.acquire() with ^C at this point. @zooba or @eryksun wear Windows hats and can answer for that platform.

  5. zooba commented on Oct 7, 2024

    @zooba
    Member

    Looks like Windows is using EnterNonRecursiveMutex() for this, which is not going to be interruptible (based on a quick look - trust Eryk's opinion over mine here).

    In order to be interruptible, it needs to be a WaitForMultipleObjects including the _PyOS_SigintEvent handle. I don't think it's trivial to convert, at least not without a performance loss, and likely with subtle behaviour changes from what it currently does.

  6. zuo commented on Oct 8, 2024

    @zuo
    ContributorAuthor

    @gpshead @zooba Thank you for your feedback! I presume that something along the lines of the following would be appropriate:

    It is platform-dependent whether the acquire() method on a lock can be interrupted (so that the KeyboardInterrupt exception will happen immediately, rather than only after the lock has been acquired or the operation has timed out).

  7. zuo commented on Oct 8, 2024

    @zuo
    ContributorAuthor

    PS Perhaps adding that

    It can on POSIX platforms.

  8. pitrou commented on Oct 8, 2024

    @pitrou
    Member

    In order to be interruptible, it needs to be a WaitForMultipleObjects including the _PyOS_SigintEvent handle.

    I think this used to be the case, but it got replaced by the faster native mutex for performance.

  9. colesbury commented on Oct 8, 2024

    @colesbury
    Contributor

    @kumaraditya303 has a PR to use PyMutex for _thread.lock, which blocks using an alertable (interruptible?) WaitForSingleObjectEx call:

  10. colesbury commented on Oct 8, 2024

    @colesbury
    Contributor

    My previous comment was incorrect. PyMutex calls WaitForSingleObjectEx with bAlertable=FALSE:

    wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE);

  11. zuo commented on Oct 8, 2024

    @zuo
    ContributorAuthor

    OK, so it remains platform-dependent whether acquire() is interruptible, doesn't it?

  12. pitrou commented on Oct 8, 2024

    @pitrou
    Member

    Yes. But concretely, it's interruptible on POSIX, not on Windows.

  13. pitrou commented on Oct 8, 2024

    @pitrou
    Member

    My previous comment was incorrect. PyMutex calls WaitForSingleObjectEx with bAlertable=FALSE:

    Regardless, it seems the bAlertable flag has nothing to do with being interruptible with Ctrl-C:
    https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobjectex

    If this parameter is TRUE and the thread is in the waiting state, the function returns when the system queues an I/O completion routine or APC, and the thread runs the routine or function. Otherwise, the function does not return, and the completion routine or APC function is not executed.

  14. added a commit that references this issue on Oct 8, 2024
  15. zuo commented on Oct 8, 2024

    @zuo
    ContributorAuthor

    PR is ready :)

  16. added a commit that references this issue on Oct 11, 2024
  17. added 2 commits that reference this issue on Oct 11, 2024
  18. added 2 commits that reference this issue on Oct 11, 2024
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

    docsDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions