Notes on Thread Safety

We recently had a problem with our central config management. Two identically configured servers sometimes got bad config files and assigned wrong listening ports. The errors seemed to flap and a very loose correlation to a time window could be seen as well.

After days of debugging a colleague of mine finally found the source of the problem. Nearly all methods of python's socket module use the non thread-safe versions of the underlying glibc. The generated apache2 configs, that listened on port 88 instead of 80, as socket.getservbyname("http") returned the result of the call of the other thread socket.getservbyname("kerberos").

There is currently no other implementation of python's socket module that uses the reentrant methods available in glibc, so we wrote a simple script that parsed /etc/services instead of the getservbyname call.

So, stay safe even if you just use the socket helpermethods in the socket module!

social