Skip to content

Thread Safe Py_AddPendingCall #48543

Closed
Closed

Description

@kristjanvalur
BPO 4293
Nosy @amauryfa, @mdickinson, @pitrou, @kristjanvalur, @tiran
Files
  • pendingalls.patch
  • pendingalls.patch: version 3 of the patch
  • pendingalls.patch: avoid deadlocks on the main thread only.
  • pendingalls.patch: new complete patch, checks for NULL pending_lock
  • pendingalls.patch: new version that doesn't assume signals on the main thread
  • xmlrpc.patch: Some fixes suggested by Amaury
  • testcapi.patch: test cases for Py_AddPendingCall()
  • testcapi.patch: testing the asynchronous notify, with suggested changes
  • pendingalls.patch: full patch including revised documentation
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-01-20.12:02:07.568>
    created_at = <Date 2008-11-10.11:36:34.958>
    labels = ['interpreter-core', 'type-crash']
    title = 'Thread Safe Py_AddPendingCall'
    updated_at = <Date 2009-01-20.12:02:07.567>
    user = 'https://github.com/kristjanvalur'

    bugs.python.org fields:

    activity = <Date 2009-01-20.12:02:07.567>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-01-20.12:02:07.568>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2008-11-10.11:36:34.958>
    creator = 'kristjan.jonsson'
    dependencies = []
    files = ['11978', '12008', '12031', '12032', '12035', '12594', '12596', '12619', '12624']
    hgrepos = []
    issue_num = 4293
    keywords = ['patch']
    message_count = 32.0
    messages = ['75691', '75692', '75693', '75700', '75782', '75783', '75872', '75963', '75966', '75970', '75977', '79113', '79119', '79128', '79132', '79142', '79147', '79151', '79152', '79241', '79255', '79269', '79270', '79290', '79504', '79507', '79511', '79658', '79975', '79982', '80091', '80250']
    nosy_count = 5.0
    nosy_names = ['amaury.forgeotdarc', 'mark.dickinson', 'pitrou', 'kristjan.jonsson', 'christian.heimes']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue4293'
    versions = ['Python 3.1', 'Python 2.7']

    Activity

    1. kristjanvalur commented on Nov 10, 2008

      kristjanvalurmannequin
      MannequinAuthor

      At CCP We have started using the Py_AddPendingCall() mechanism to
      signal python about a completed IO operation.
      However, we noticed that the existing mechanism was hoplelessly un-
      thread safe. This is bad, since on Windows at least, it is very
      convenient to have such callbacks happen on an arbitrary thread from
      the system's thread pool.
      I submit a thread-safe implementation instead to be used if WITH_THREAD
      is defined.
      This allows Py_AddPendingCall() to be called from any thread, from any
      context, even from a PendingCall callback itself.

    2. added
      interpreter-core
      type-crash
      on Nov 10, 2008
    3. tiran commented on Nov 10, 2008

      @tiran
      Member

      Your patch sounds like a useful addition. However I can comment on the
      details because I haven't dealt with Py_AddPendingCall() yet.

      Documentation updates and a unit test (Modules/_testcapimodule.c), that
      shows the bug and verifies your patch, are welcome, too.

    4. kristjanvalur commented on Nov 10, 2008

      kristjanvalurmannequin
      MannequinAuthor

      Good point. I'll make a test using ctypes and _testcapimodule.c to try
      to make it as non-platform-specific as possible.

    5. kristjanvalur commented on Nov 10, 2008

      kristjanvalurmannequin
      MannequinAuthor

      Okay, i add a new patch that includes tests.
      I am unsure where I should add documentation for the Py_AddPendingCall
      () function. Any suggestions?

    6. amauryfa commented on Nov 12, 2008

      @amauryfa
      Contributor

      Py_AddPendingCall is used inside signal handlers.
      It seems that there is a (tiny) chance that a signal interrupts the
      program inside Py_MakePendingCalls, while the pendinglock is held.
      Py_AddPendingCall would try to acquire this lock again and deadlock.

    7. kristjanvalur commented on Nov 12, 2008

      kristjanvalurmannequin
      MannequinAuthor

      Right.
      Isn't the best way to avoid it to block signals just while we pop the
      queue? I'll see if python has a portable sigaction thing to do that.
      Otherwise we'd have to start to mess with a "volatile busy" flag again
      and possibly introduce tricky race conditions.

    8. kristjanvalur commented on Nov 14, 2008

      kristjanvalurmannequin
      MannequinAuthor

      Here is a revised version.
      Since there is no portable way to block signals, and no way at all on
      windows (apparently) the simplest way is to simply use NOWAIT_LOCK when
      adding a new pending call. While this does not guarantee that we are
      always able to append a call (we may be unlucky in all our N tries and
      there is also no portable way to yield the thread timeslice between
      tries) such are also the semantics of the method. Sometimes the buffer
      may be full, or we fail to get the lock in time, and a -1 is returned.
      It is up to the caller to respond appropriately, perhaps by trying again
      later.

    9. kristjanvalur commented on Nov 17, 2008

      kristjanvalurmannequin
      MannequinAuthor

      Small refinement: Deadlock avoidance is only needed on the main thread
      since we only install signal handlers for the main thread.

    10. amauryfa commented on Nov 17, 2008

      @amauryfa
      Contributor

      Two comments about the patch:

      • typo in a comment: "...change chat a signal..."

      • PyThread_acquire_lock should not be called with a NULL lock: on win32
        this does not matter (there is a test in thread_nt.h), but Unix
        segfaults in thread_pthread.h if you hit Ctrl-C.

    11. kristjanvalur commented on Nov 17, 2008

      kristjanvalurmannequin
      MannequinAuthor

      I had forgotten that locks aren't initialized until PyEval_InitThreads
      () is called. Now we check for this case. Also reinitialize when fork
      () is called.
      Is there any suggested place where I should add documentation to this?

    12. kristjanvalur commented on Nov 17, 2008

      kristjanvalurmannequin
      MannequinAuthor

      I turns out that for SIGINT, windows creates a new thread. So, the
      assumption that signals are delivered on the main thread are not valid
      in general. Therefore, we must initialize the lock pending_lock
      independently of PyEval_InitThreads(), and also cannot assume that
      signals are deliverd on the main thread.

    13. kristjanvalur commented on Jan 5, 2009

      kristjanvalurmannequin
      MannequinAuthor

      Ping.
      Amaury, any thoughts?

    14. amauryfa commented on Jan 5, 2009

      @amauryfa
      Contributor
      • The things_to_do static variable should be declared as "volatile": it
        is read by the main loop without any lock.
        (by the way, could you rename it to something like "pendingcalls_to_do"?)

      • in the old Py_MakePendingCalls function, the "#ifdef WITH_THREAD" part
        is not useful now.

      • the "XXX" comments should probably be rewritten.

      A note to people concerned by performance: the additional lock is used
      only when there is an actual pending call to process. The main loop only
      regularly checks the "things_to_do static" volatile static variable, and
      the patch does not change this.

    15. 8 remaining items

    16. pitrou commented on Jan 6, 2009

      @pitrou
      Member

      Ok, the test patch is fine!

      Now looking at the main patch, some comments:

      • you removed "volatile" from pendingfirst and pendinglast, is it really
        safe?
      • what if pending_lock is not initialized when Py_AddPendingCall is
        called? I know it's unlikely, but imagine someone calling that function
        just after the interpreter has been initialized
      • the documentation should mention that the GIL must not be held when
        Py_AddPendingCall is called, otherwise there can be a deadlock (is it a
        change from previous versions by the way?)
      • you should check the return value of PyThread_allocate_lock() for NULL
      • is your implementation reentrant? that is, registering a callback from
        a callback. There is no need for it to be, but if it aims to be, then it
        should perhaps be documented and tested :)
    17. pitrou commented on Jan 6, 2009

      @pitrou
      Member

      Sorry, forget what I said about the GIL, it looks fine. Perhaps some
      comments should be added to clarify that matter, however.

    18. kristjanvalur commented on Jan 6, 2009

      kristjanvalurmannequin
      MannequinAuthor
      1. volatile is not required when access is guarded by locks.
      2. pendingcalls_to_do is initialized to 1, so the very first thing that
        the interpreter does (after executing one bytecode, I init _PyTicker to
        0 now) is to initialize this lock. Creating a lock really must be done
        by one well known startup thread only. There is a lot of functions in
        the python API that don't work until after python initialization
        (without that being explicitly documented), Py_AddPendingCall() is one
        of them. Btw, there is no generic "init" function for the ceval.c
        module. And PyEval_InitThreads isn't really suitable since we must
        have the lock, even if we haven't initialized the threads, to guard us
        against reentrancy during signal delivery. For extra safety, I have
        added a NULL test in Py_AddPendingCall().
        3)Py_AddPendingCall() can be called with our without the GIL.
        4)I've added a check in Py_MakePendingCalls(). In PyEval_ReInitThreads
        there is no way to do anything sensible in the case of error.
        5)Yes it is reentrant. The pending_lock is only held for a short while
        while we pop a callback off the queue. However, because even during
        that short while a signal can be delivered which causes
        Py_AddPendingCall() to be called, the latter function fails if it
        doesn't acquire the lock in a reasonable amount of time. This would
        cause a signal to be lost, and not a deadlock. This hasn't changed
        from the old implementation.

      I have also added a flag to make sure that we don't execute pending
      calls recursively, (async notifications is so much better) and
      explained it better in the docs.

    19. kristjanvalur commented on Jan 9, 2009

      kristjanvalurmannequin
      MannequinAuthor

      Checked in as revision: 68460

    20. pitrou commented on Jan 9, 2009

      @pitrou
      Member

      Kristján Valur Jónsson <kristjan@ccpgames.com> added the comment:

      Checked in as revision: 68460

      Looks like you forgot the unit tests!

    21. kristjanvalur commented on Jan 9, 2009

      kristjanvalurmannequin
      MannequinAuthor

      I indeed forgot the unittests and docs.
      They are now in, in revision: 68461

    22. kristjanvalur commented on Jan 12, 2009

      kristjanvalurmannequin
      MannequinAuthor

      Added MISC/News entry in revision: 68545

    23. mdickinson commented on Jan 16, 2009

      @mdickinson
      Member

      Kristján,

      The r68461 checkin seems to be causing a number of the buildbots to fail,
      typically with output like:

      test_capi
      test test_capi failed -- Traceback (most recent call last):
        File "/home/pybot/buildarea/trunk.klose-debian-ppc/build/Lib/test/test_capi.py", line 57, in 
      test_pendingcalls_threaded
          self.pendingcalls_wait(l, n)
        File "/home/pybot/buildarea/trunk.klose-debian-ppc/build/Lib/test/test_capi.py", line 42, in 
      pendingcalls_wait
          "timeout waiting for %i callbacks, got %i"%(n, len(l)))
      AssertionError: timeout waiting for 32 callbacks, got 23

      Please could you look into this?

      Also, I don't understand the code:

      for i in xrange(1000):
          a = i*i

      in pendingcalls_wait(), in test_capi.py. Whatever you're
      trying to do here, surely there's a better way?

    24. mdickinson commented on Jan 16, 2009

      @mdickinson
      Member

      How about using a timer instead of the 'count += 1' loop: after starting
      the 32 self.pendingcalls_submit threads, set up a threading.Event and
      start yet another thread that simply does a time.sleep(5.0) (or whatever)
      and then sets that event.

      Then your waiting loop could be something like:

      while not self.my_event.is_set():
          for i in range(1000):
              a = i*i
      self.assertEqual(len(l), n)

      There's probably a better way, but that's the best I can come up with this
      late on a Friday night...

    25. kristjanvalur commented on Jan 18, 2009

      kristjanvalurmannequin
      MannequinAuthor

      checked in r68722, let's hope this fixes things.

    26. mdickinson commented on Jan 20, 2009

      @mdickinson
      Member

      That seems to have done the trick. Thanks!

    27. transferred this issue fromon Apr 10, 2022
    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

      interpreter-coretype-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions