Opened 6 years ago

Closed 6 years ago

#12350 closed Bugs (fixed)

shared_mutex (pthreads) unlocked too early in unlock_shared()

Reported by: Maximilian Czerny <maximilian.czerny@…> Owned by: viboes
Milestone: Boost 1.62.0 Component: thread
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

Hej, I experienced some odd behaviour in the unlock_shared() function of the shared_mutex in the pthreads implementation. I wanted to do the following: There is a class node that has a shared_mutex with which I implemented optimistic locking for a tree data structure. A node can be deleted if its parent and the node itself is locked (exclusive) by the current thread. The problem with that boils down to the following order of two threads (R - reader, W - writer) operating on a node in this way:

  1. R successfully calls lock_shared() on a node
  2. W (which holds a shared lock) wants to change the node
  • try_unlock_shared_and_lock_upgrade()
  • unlock_upgrade_and_lock() puts W to sleep as R still holds a shared lock
  1. R calls unlock_shared() and internally does (thread/pthreads/shared_mutex.hpp void unlock_shared()):
  • it gets the unique_lock to modify the state
  • it decreases the number of shared locks to 0
  • it therefore notifies R and unlocks the unique_lock
  1. W modifies the node and deletes it
  2. R crashes in release_waiters as it now accesses deleted memory

I fixed this by removing the two premature unlocks (lk.unlock()) but I am not sure if that is valid for all applications. That means that state changes are ownly possible after all operations are actually done (when the unique_lock goes out of scope).

Change History (6)

comment:1 by Maximilian Czerny <maximilian.czerny@…>, 6 years ago

Summary: shared_mutex (pthreads) unlocked to early in unlock_shared()shared_mutex (pthreads) unlocked too early in unlock_shared()

comment:2 by viboes, 6 years ago

Sorry for been late. I will take a look at asap.

comment:3 by viboes, 6 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:4 by viboes, 6 years ago

Good catch.

I'll commit the change soon.

comment:6 by viboes, 6 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.