This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Intercepting thread lock objects not working under context managers
Type: behavior Stage:
Components: C API, Demos and Tools, Documentation Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, dstufft, eric.araujo, mhmdkanj, rhettinger, vstinner
Priority: normal Keywords:

Created on 2020-12-21 15:11 by mhmdkanj, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
tracing-locking-mechanisms.py mhmdkanj, 2020-12-21 15:11
Messages (3)
msg383530 - (view) Author: Mohamad Kanj (mhmdkanj) Date: 2020-12-21 15:14
The interception of Python and C functions is achieved through the `sys` module - however, when using thread locks from `threading` module for instance under context managers, the calls on the lock to `acquire()` and `release()` cannot be witnessed. If these methods were to be called explicitly then this is no problem.
msg383557 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-12-21 21:43
The sys.setprofile() doc don't make it clear whether the __enter__ and __exit__ call were intended to be traceable events.  The docs do state, "there is no way for the profiler to know about context switches between threads, so it does not make sense to use this in the presence of multiple threads."
msg383560 - (view) Author: Mohamad Kanj (mhmdkanj) Date: 2020-12-21 21:51
Thanks Raymond for the input.
So with some experimentation, the __enter__() & __exit__() functions could be traceable when other kinds of objects are used with the context manager (i.e with statement using file IO or custom class objects). However, just when the object is a Lock object, then this is not the case anymore - which basically shows an unusual behavior if we were to think that such statements are executed for a specific thread. Also please mind that the threading module already implements settrace() and setprofile() methods like the sys module to extend tracing capabilities.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86871
2021-01-17 12:27:24jklothsetcomponents: - Distutils
2020-12-21 21:51:51mhmdkanjsetmessages: + msg383560
2020-12-21 21:43:02rhettingersetnosy: + rhettinger, vstinner
messages: + msg383557
2020-12-21 15:14:43mhmdkanjsetmessages: + msg383530
2020-12-21 15:11:17mhmdkanjcreate