Skip to content

Update logging library module lock to use context manager to acquire/release lock. #109461

Description

@dcollison

Feature or enhancement

Proposal:

Current implementation relies on both _acquireLock() and _releaseLock() being called, otherwise a lock may never be released:

def_acquireLock():
""" Acquire the module-level lock for serializing access to shared data. This should be released with _releaseLock(). """if_lock:
try:
_lock.acquire()
exceptBaseException:
_lock.release()
raisedef_releaseLock():
""" Release the module-level lock acquired by calling _acquireLock(). """if_lock:
_lock.release()

The majority of usages of _acquireLock() manually add a try/except/finally block to ensure that the lock is released if an exception is thrown. Some usages of _acquireLock() have no safety.

The proposal is to alter the usage of _acquireLock() to be a context manager that deals with acquiring and releasing automatically rather than requiring try/except/finally blocks to be used anywhere the function is called.

For example,
usage before:

_acquireLock()
try:
...
finally:
_releaseLock()

proposed usage:

with_acquireLock():
...

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions