diff --git a/iceoryx2-bb/posix/src/mutex.rs b/iceoryx2-bb/posix/src/mutex.rs index a1f2725cf..13315c0f5 100644 --- a/iceoryx2-bb/posix/src/mutex.rs +++ b/iceoryx2-bb/posix/src/mutex.rs @@ -266,7 +266,12 @@ pub enum MutexThreadTerminationBehavior { /// mutex owning /// thread/process dies the mutex is put into an inconsistent state which can be recovered with /// [`Mutex::make_consistent()`]. The inconsistent state is detected by the next instance which - /// calls [`Mutex::lock()`], [`Mutex::try_lock()`] or [`Mutex::timed_lock()`]. + /// calls [`Mutex::try_lock()`] or [`Mutex::timed_lock()`]. + /// + /// **Important:** If the owner dies after another thread has already locked the [`Mutex`] it + /// may become impossible to recover the [`Mutex`]. Therefore, this feature should be used + /// only in combination with either [`Mutex::try_lock`] or [`Mutex::timed_lock()`] and + /// never with [`Mutex::lock()`]. /// /// This is also known as robust mutex. ReleaseWhenLocked = posix::PTHREAD_MUTEX_ROBUST, diff --git a/iceoryx2-bb/posix/tests/mutex_tests.rs b/iceoryx2-bb/posix/tests/mutex_tests.rs index 9b15f3b56..cfec622c8 100644 --- a/iceoryx2-bb/posix/tests/mutex_tests.rs +++ b/iceoryx2-bb/posix/tests/mutex_tests.rs @@ -405,14 +405,16 @@ fn mutex_can_be_recovered_when_thread_died() { }); }); - let guard = sut.lock(); - assert_that!(guard, is_err); - match guard.as_ref().err().as_ref().unwrap() { - MutexLockError::LockAcquiredButOwnerDied(_) => (), - _ => assert_that!(true, eq false), + loop { + let guard = sut.try_lock(); + + if guard.is_ok() { + assert_that!(guard.as_ref().unwrap(), is_none); + } else if let Err(MutexLockError::LockAcquiredButOwnerDied(_)) = guard { + sut.make_consistent(); + break; + } } - sut.make_consistent(); - drop(guard); let guard = sut.try_lock(); assert_that!(guard, is_ok);