Locking error (?!?)

Tim Peters tim.one at comcast.net
Fri Mar 28 19:35:34 CET 2003

[Scott Pigman]
> is this a bug in my code or my brain:

It looks like a bug in your code, but not in the code you showed us.

> ------------------------------------- class Agent:
>         assert self.lock.locked()
>         self.OUT_SOCKET.send(message)
>         assert self.lock.locked()
>         response = self.OUT_SOCKET.recv(Agent.MESSAGE_BUFFER_SIZE)
>         assert self.lock.locked()  ## FAILS
> --------------------------------------
> self.lock is an object of type threading.Lock, which was previously
> acquired ( lock.acquire() ).  The first two assertions pass, the
> third one fails.  can anybody give me some insight why?

Some other thread called lock.release().

> I had thought that once aquired, the lock will remain lock until

That part's right.

> the locking thread releases it.

But that part's not -- any thread can release the lock.  It's impossible to
guess what's in the rest of your app, but it *may* be that you wanted a
threading.RLock here instead of a threading.Lock (for the relevant
difference here, read the docs -- they're really quite clear about this

More information about the Python-list mailing list