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

Handle error from pthread_cond_wait() #1574

Closed
maxsharabayko opened this issue Sep 24, 2020 · 2 comments
Closed

Handle error from pthread_cond_wait() #1574

maxsharabayko opened this issue Sep 24, 2020 · 2 comments
Labels
[core] Area: Changes in SRT library core Priority: Medium Type: Bug Indicates an unexpected problem or unintended behavior
Milestone

Comments

@maxsharabayko
Copy link
Collaborator

pthread_cond_wait() can report an error. The wrapper ignores it. Throw an exception similar to C++11 condition_variable. See this comment: link.

Extracted from #1103.

@maxsharabayko maxsharabayko added Type: Bug Indicates an unexpected problem or unintended behavior [core] Area: Changes in SRT library core labels Sep 24, 2020
@maxsharabayko maxsharabayko added this to the v1.5.0 milestone Sep 24, 2020
@maxsharabayko maxsharabayko modified the milestones: v1.5.0, v1.4.3 Oct 22, 2020
@ethouris
Copy link
Collaborator

This is the excerpt from the manpage for pthread_cond_wait (cut off parts that refer to pthread_cond_timedwait only):

ERRORS
       These functions shall fail if:

       ENOTRECOVERABLE
              The state protected by the mutex is not recoverable.

       EOWNERDEAD
              The  mutex  is  a robust mutex and the process containing the previous owning thread terminated while holding the mutex lock. The mutex lock shall be acquired by the calling thread and it is up to the new owner to make the
              state consistent.

       EPERM  The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and the current thread does not own the mutex.

Every error in this list concerns exclusively the "robust mutex", which isn't the default type of the mutex and must be set so explicitly, nor do we use such kind of mutex in the POSIX wrapper. It's not explicitly described by ENOTRECOVERABLE, but this same error is described more thoroughyl in the manpage of pthread_mutexattr_setrobust:

       PTHREAD_MUTEX_ROBUST
              If a mutex is initialized with the PTHREAD_MUTEX_ROBUST attribute and its owner dies without unlocking it, any future attempts to call pthread_mutex_lock(3) on this mutex will succeed and return EOWNERDEAD to indicate that
              the original owner no longer exists and the mutex is in an inconsistent state.  Usually after EOWNERDEAD is returned, the next owner should call pthread_mutex_consistent(3) on the acquired mutex to make it consistent again
              before using it any further.

              If  the  next  owner unlocks the mutex using pthread_mutex_unlock(3) before making it consistent, the mutex will be permanently unusable and any subsequent attempts to lock it using pthread_mutex_lock(3) will fail with the
              error ENOTRECOVERABLE.  The only permitted operation on such a mutex is pthread_mutex_destroy(3).

              If the next owner terminates before calling pthread_mutex_consistent(3), further pthread_mutex_lock(3) operations on this mutex will still return EOWNERDEAD.

In result, every error in the list may only be reported in case of using a robust mutex associated with the condition here. Which means that in the current implementation pthread_cond_wait doesn't report any errors. Of course, it could still report errors if you pass NULL for mutex or condition, but it is believed that the POSIX wrapper is taking care of it earlier.

It might be a good idea to implement robust mutexes as a special "robust mode" enabled at compile time to try to reproduce any thread errors - such mutexes are armed in extra bug tracing abilities, such as report error when trying to lock a mutex that a dying thread has left locked. But that's for another activity.

@maxsharabayko
Copy link
Collaborator Author

Closing as nothing to do according to the analysis by @ethouris:

... in the current implementation pthread_cond_wait doesn't report any errors. Of course, it could still report errors if you pass NULL for mutex or condition, but it is believed that the POSIX wrapper is taking care of it earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Priority: Medium Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants