Title: threading.Condition._is_owned() is wrong when using threading.Lock
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6, Python 3.5, Python 3.4, Python 3.2, Python 3.3, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Nir Soffer, haypo, nirs, pitrou
Priority: normal Keywords: patch

Created on 2015-10-30 00:36 by nirs, last changed 2017-07-12 15:56 by Nir Soffer.

File name Uploaded Description Edit
0001-Issue-25516-threading.Condition._is_owned-fails-when.patch nirs, 2015-10-30 02:09 patch fixing this issue by implementing _is_owned in the _thread module review
Pull Requests
URL Status Linked Edit
PR 2681 open nirs, 2017-07-12 15:43
Messages (4)
msg253703 - (view) Author: Nir Soffer (nirs) * Date: 2015-10-30 00:36
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()
>>> cond.notify()
>>> cond.wait(0)

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.
msg253708 - (view) Author: Nir Soffer (nirs) * Date: 2015-10-30 02:00
The issue was introduced in this commit:

commit 8cb1ccbb8b9ed01c26d2c5be7cc86682e525dce7
Author: Guido van Rossum <>
Date:   Thu Apr 9 22:01:42 1998 +0000

    New Java-style threading module.  The doc strings are in a separate module.
msg253748 - (view) Author: Nir Soffer (nirs) * Date: 2015-10-30 17:19
The commit hash in the previous message is a git commit from the github mirror:
msg298229 - (view) Author: Nir Soffer (Nir Soffer) Date: 2017-07-12 15:56
I rebased the patch on master (it was created against the legacy git tree
in github), and sent a pull request.
Date User Action Args
2017-07-12 15:56:53Nir Soffersetnosy: + Nir Soffer
messages: + msg298229
2017-07-12 15:43:17nirssetpull_requests: + pull_request2747
2016-04-29 12:24:45berker.peksaglinkissue20247 superseder
2015-11-02 19:29:36nirssetnosy: + pitrou, haypo
2015-10-30 22:31:37nirssettype: behavior
2015-10-30 17:19:40nirssetmessages: + msg253748
2015-10-30 02:09:59nirssetfiles: + 0001-Issue-25516-threading.Condition._is_owned-fails-when.patch
keywords: + patch
2015-10-30 02:00:02nirssetmessages: + msg253708
2015-10-30 00:36:32nirscreate