Skip to content

Commit 341aa86

Browse files
authored
Removing PY2 lock handler. (#1013)
1 parent 7a95aeb commit 341aa86

File tree

2 files changed

+1
-80
lines changed

2 files changed

+1
-80
lines changed

openhtf/util/logs.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,6 @@ def filter(self, record):
252252
return match.group('test_uid') == self.test_uid
253253

254254

255-
class KillableThreadSafeStreamHandler(logging.StreamHandler):
256-
257-
def handle(self, record):
258-
# logging.Handler objects have an internal lock attribute that is a
259-
# threading.RLock instance; it can cause deadlocks in Python 2.7 when a
260-
# KillableThread is killed while its release method is running.
261-
with threads.safe_lock_release_context(self.lock):
262-
return super(KillableThreadSafeStreamHandler, self).handle(record)
263-
264-
265255
class RecordHandler(logging.Handler):
266256
"""A handler to save logs to an HTF TestRecord."""
267257

@@ -273,13 +263,6 @@ def __init__(self, test_uid, test_record, notify_update):
273263
self.addFilter(MAC_FILTER)
274264
self.addFilter(TestUidFilter(test_uid))
275265

276-
def handle(self, record):
277-
# logging.Handler objects have an internal lock attribute that is a
278-
# threading.RLock instance; it can cause deadlocks in Python 2.7 when a
279-
# KillableThread is killed while its release method is running.
280-
with threads.safe_lock_release_context(self.lock):
281-
return super(RecordHandler, self).handle(record)
282-
283266
def emit(self, record):
284267
"""Save a logging.LogRecord to our test record.
285268
@@ -350,7 +333,7 @@ def configure_logging():
350333
logging_level = logging.DEBUG
351334

352335
# Configure a handler to print to the CLI.
353-
cli_handler = KillableThreadSafeStreamHandler(stream=sys.stdout)
336+
cli_handler = logging.StreamHandler(stream=sys.stdout)
354337
cli_handler.setFormatter(CliFormatter())
355338
cli_handler.setLevel(logging_level)
356339
cli_handler.addFilter(MAC_FILTER)

openhtf/util/threads.py

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -34,68 +34,6 @@ class InvalidUsageError(Exception):
3434
"""Raised when an API is used in an invalid or unsupported manner."""
3535

3636

37-
def safe_lock_release_context(rlock):
38-
# Python3 has a C-implementation of RLock, which doesn't have the thread
39-
# termination issues.
40-
return _placeholder_release()
41-
42-
43-
@contextlib.contextmanager
44-
def _placeholder_release():
45-
yield
46-
47-
48-
# pylint: disable=protected-access
49-
@contextlib.contextmanager
50-
def _safe_lock_release_py2(rlock):
51-
"""Ensure that a threading.RLock is fully released for Python 2.
52-
53-
The RLock release code is:
54-
https://github.com/python/cpython/blob/2.7/Lib/threading.py#L187
55-
56-
The RLock object's release method does not release all of its state if an
57-
exception is raised in the middle of its operation. There are three pieces of
58-
internal state that must be cleaned up:
59-
- owning thread ident, an integer.
60-
- entry count, an integer that counts how many times the current owner has
61-
locked the RLock.
62-
- internal lock, a threading.Lock instance that handles blocking.
63-
64-
Args:
65-
rlock: threading.RLock, lock to fully release.
66-
67-
Yields:
68-
None.
69-
"""
70-
assert isinstance(rlock, threading._RLock)
71-
ident = _thread.get_ident()
72-
expected_count = 0
73-
if rlock._RLock__owner == ident:
74-
expected_count = rlock._RLock__count
75-
try:
76-
yield
77-
except ThreadTerminationError:
78-
# Check if the current thread still owns the lock by checking if we can
79-
# acquire the underlying lock.
80-
if rlock._RLock__block.acquire(0):
81-
# Lock is clean, so unlock and we are done.
82-
rlock._RLock__block.release()
83-
elif rlock._RLock__owner == ident and expected_count > 0:
84-
# The lock is still held up the stack, so make sure the count is accurate.
85-
if rlock._RLock__count != expected_count:
86-
rlock._RLock__count = expected_count
87-
elif rlock._RLock__owner == ident or rlock._RLock__owner is None:
88-
# The internal lock is still acquired, but either this thread or no thread
89-
# owns it, which means it needs to be hard reset.
90-
rlock._RLock__owner = None
91-
rlock._RLock__count = 0
92-
rlock._RLock__block.release()
93-
raise
94-
95-
96-
# pylint: enable=protected-access
97-
98-
9937
def loop(_=None, force=False):
10038
"""Causes a function to loop indefinitely."""
10139
if not force:

0 commit comments

Comments
 (0)