threading.Condition._is_owned() is wrong when using threading.Lock
| BPO | 25516 |
|---|---|
| Nosy | @nirs, @pitrou |
| PRs | |
| Files |
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 = None
created_at = <Date 2015-10-30.00:36:32.244>
labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
title = 'threading.Condition._is_owned() is wrong when using threading.Lock'
updated_at = <Date 2020-11-06.17:49:18.448>
user = 'https://github.com/nirs'
bugs.python.org fields:
activity = <Date 2020-11-06.17:49:18.448>
actor = 'vstinner'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-10-30.00:36:32.244>
creator = 'nirs'
dependencies = []
files = ['40900']
hgrepos = []
issue_num = 25516
keywords = ['patch']
message_count = 4.0
messages = ['253703', '253708', '253748', '298229']
nosy_count = 3.0
nosy_names = ['nirs', 'pitrou', 'Nir Soffer']
pr_nums = ['2681']
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25516'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']
When using threading.Lock, threading.Condition._is_owned is assuming that the calling thread is owning the condition lock if the lock cannot be acquired. This check is completely wrong if another thread owns the lock.
>>> cond = threading.Condition(threading.Lock())
>>> threading.Thread(target=cond.acquire).start()
>>> cond._is_owned()
True
>>> cond.notify()
>>> cond.wait(0)
False
Careful users that acquire the condition before calling wait() or notify() are not effected. Careless users that should have been warned by RuntimeError are.
Tested on Python 2.7 and 3.4.2 and 3.6.0a0.
The issue was introduced in this commit:
commit 8cb1ccbb8b9ed01c26d2c5be7cc86682e525dce7 Author: Guido van Rossum <[email protected]> Date: Thu Apr 9 22:01:42 1998 +0000
New Java-style threading module. The doc strings are in a separate module.
The commit hash in the previous message is a git commit from the github mirror: https://github.com/python/cpython/commit/8cb1ccbb8b9ed01c26d2c5be7cc86682e525dce7
I rebased the patch on master (it was created against the legacy git tree in github), and sent a pull request.
I think the fix by https://github.com/python/cpython/pull/2681 is being done at wrong level - if we want Condition to be thread aware, it should be implemented at Lock level.