hn.today

Mutex poisoning is not for everyone

bertptrs.nl7 points1 comments
Screenshot of Mutex poisoning is not for everyone

Mutex poisoning marks a lock as compromised when its guard is dropped during a panic, causing later lock attempts to return an error. The mechanism aims to prevent code from observing data left in an invalid intermediate state, such as a value that must remain even while a critical section increments it twice. The author argues that this protection is valuable in some situations, especially when a lock safeguards state beyond the mutex’s direct control, but is uncommon in ordinary applications, where critical sections are simple and interruption leaves no lasting invariant violation. Rust should make the common case easy while allowing specialized poisoning where needed.

The main case against poisoning by default is its cost across every mutex, whether or not a program needs the feature. It adds state and can increase mutex size; more significantly, generated code includes poison checks, panic handling, and guard cleanup. Assembly comparisons show that locking and unwrapping a mutex can produce 147 lines, while explicitly recovering the inner value from a poison error reduces this to 68; a nightly non-poisoning mutex needs 34. The extra branching and code can affect performance and instruction-cache use. Poisoning also complicates everyday code: ignoring poison requires a lengthy recovery expression, while using unwrap is shorter but can panic unexpectedly and encourages habits that obscure whether poison matters. The author favors non-poisoning mutexes by default and a separate poison wrapper for programs that genuinely need the safeguard.

Read on bertptrs.nl1 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Big Data Is Dead

Big Data Is Dead

Big data is not the main obstacle to useful insights: hardware has kept pace with data growth, while teams still struggle to make sense of their information. The author argues that organizations should focus on making data easier to understand and use, rather than buying tools built for ever-larger datasets. (motherduck.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.