Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register Controller does not release mutex when exceptions are thrown within the yield #57

Open
josephnobes-stfc opened this issue May 28, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@josephnobes-stfc
Copy link
Contributor

The register controller's context manager is added so that with can be used during field / register access to make mutex handling easier. However, the current code simply returns the mutex ownership after the yield:

    @contextmanager
    def acquire(self, blocking=True, timeout=-1):
        # Grab the lock with the supplied settings
        result = self._register_mutex.acquire(blocking=blocking, timeout=timeout)

        # Allow the caller to execute their 'with'. 'result' is needed so that
        # if the lock cannot be grabbed the user can handle it.
        yield result

        # Release the lock, if it was actually acquired
        if result:
            self._register_mutex.release()

This means that if there is an exception thrown within the yield, it skips the mutex release and the caller will permanently own the resource.

The yield should be surrounded with a try...except that will release the mutex before raising the exception so as not to mask it.

This was encountered in https://github.com/stfc-aeg/babyd-embedded/issues/7

@josephnobes-stfc josephnobes-stfc added the bug Something isn't working label May 28, 2024
@josephnobes-stfc josephnobes-stfc self-assigned this May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: To do
Development

No branches or pull requests

1 participant