Subject: Re: Unlocking an unlocked mutex
To: Vincent <>
From: Bill Studenmund <>
List: tech-kern
Date: 10/13/2005 11:02:26
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Oct 13, 2005 at 07:24:46PM +0200, Vincent wrote:
> Bill Studenmund a ?crit :
> >My day job is writing an application that uses pthreads. These assertion=
> >REALLY help. The problem with unlocking an unlocked mutex is that you mo=
> >likely just finished modifying data that another thread thought _it_ had=
> >exclusive access to. You can end up with problems showing up in whatever=
> >code goes to use the unprotected data, so the end error looks like it's=
> >miles away from where the bug is.
> Could be. As I said, I did not analysed the program quite more. The=20
> error happens as you switch between audio channels, so I presume the=20
> mutex has to do with allocating the audio hardware. Or something close.=
> Anyhow, ignoring the error does not cause any core dump or malfunction.=
> So I assume it is just *dirty* programming.

As far as you can tell so far.

The real problem with these kinds of bugs is that they usually slip
through testing. You won't SEE a problem if your activity load is light
enough that you effectively only have one thread wanting to run at once; =
unless there's another thread that has this mutex locked and is modifying
the protected variables, you won't have a problem. And even "heavy" loads
aren't necessarily enough to notice the problem.

But if you hit the right, heavy load, BOOM!

I recently had a situation where a few (per second) small transactions
were fine, a lot of small transactions were fine, a few large transactions
were fine, but a lot of large transactions hit a problem. Sadly this
problem wasn't a mutex locking one, so I didn't get a clear indication of
the issue. But the idea is the same; seemingly-similar work loads can
trigger different code dynamics.

All of that (and this thread) said, I understand your frustration at the
prospect of trying to debug such an errant program. I had the advantage
that my application was developed under NetBSD, so I was "informed" of
locking errors as soon as I made them. It may not be that easy to find out
where the error really is. Also, the original programmers may not have
included enough info for you to figure out what's supposed to be
happening. I have learned the hard way to note what locks are supposed to
be held and what are supposed to be locked, both on entry and exit, as
part of a routine's comments.

Take care,


Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.2.3 (NetBSD)