elast0ny / elast0ny/raw_sync-rs

locks example will deadlock on macos, but not Linux

Open
#30 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
66
Forks
20
PR merge metrics
No merged PRs in 30d

Description

Running command `cargo run --example locks` on Linux will run through a number of mutex and rwlock tests,
and eventually return to the command line.

After applying #29, the locks example will now compile and run on macos, but will deadlock, and not finish.
(Note that changes in PR #29 should not alter Linux behavior)
On linux, running the locks example will go through all the mutex and rwlocks examples and return to the prompt without deadlocking.

_I believe, but have not confirmed the macos failure is due to **recursive** Mutex and RWLock locking.
Linux appears to better handle recursive mutexes than macos, at least for the pthread calls.
https://en.wikipedia.org/wiki/Reentrant_mutex

In fact!, there is a 10 year old discussion on Stack Overflow how some recursive mutex code works on Linux, FreeBSD and Solaris, but fails to work on MacOS.
This is what leads me to believe the recursive locking of the example causes the deadlock on macos.

The macos platform has a special type: [NSRecursiveLock](https://developer.apple.com/library/archive/documentation/LegacyTechnologies/WebObjects/WebObjects_3.5/Reference/Frameworks/ObjC/Foundation/Classes/NSRecursiveLock/Description.html#//apple_ref/occ/cl/NSRecursiveLock)
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html
This type is suggested for recursive locking needs. (And is not used in this library, macos pthreads is used)

*one solution to fix the example would be to avoid recursive locking on the macos platform*

So, this library might work well for you on macos, and you are not doing recursive locking.
In the linked wiki article, some notable computer scientists recommend against avoiding recursive locking if possible.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running `cargo run --example locks` on macOS and Linux, then inspect the `locks` example and the changes from PR #29. Trace the mutex and rwlock cases that recursively lock and identify the macOS-specific deadlock. Done means the example completes on macOS without changing its Linux behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.