Skip to content

threading.RLock (and threading.Condition) doesn't work on Python 3.2+ with monkey-patching #185

@vstinner

Description

@vstinner

The threading.RLock class was reimplemented in C in Python 3.2. threading.RLock doesn't use _thread.get_ident() or threading.get_ident(), but it calls directly the C function. Because of that, RLock doesn't work ("as expected") with eventlet monkey-patching. Example:

#!python
import eventlet
eventlet.monkey_patch()

import threading
#threading.RLock = threading._PyRLock

def func(c):
    with c:
        print("thread: owned?", c._is_owned())
        c.notify()
        print("thread: owned?", c._is_owned())

c = threading.Condition()
print("LOCK", threading.RLock)
with c:
    t = threading.Thread(target=func, args=(c,))
    t.start()
    print("main: owned?", c._is_owned())
    c.wait()
    print("main: owned?", c._is_owned())

The code hangs with eventlet 0.16 on Python 3.5 (and probably also Python 3.2, 3.3 and 3.4).

I tried to reuse the Python implementation of the RLock class (threading._PyRLock), but in this case c.notify() raises: RuntimeError("cannot notify on un-acquired lock").

I will work on a fix.

Note: I'm working on porting the Oslo Messaging project to Python 3. The initial issue came from a threading.Condition object, threading.Condition uses internally a threading.RLock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions