shazow/urllib3 — comment on commit 99cd0a3c68de889af1cbcf82580d24057ab8a29d
File: dummyserver/server.py line 58
By brandon-rhodes:

Fatal race condition. By the time `socket_server()` can even get started, it is quite possible that the test itself will already have grabbed the available lock.

You need here a “lock that starts out as locked”, which is to say that you need at least an `Event` from `threading`. This is exactly the situation for which it was designed: when “something is going to happen” (an “event” in concurrency-speak) “but I have to wait until it happens before I proceed.”

But, of course, I would suggest instead a `Queue` instead of an `Event` so that the socket's port number can be passed back to the caller — and you would accomplish port-independence and also the concurrent rendezvous all in one step!