Skip to content

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

Closed
@dcollison

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()
        except BaseException:
            _lock.release()
            raise

def _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