Description
Hi libzmq guys!
Issue
I have a test that spawns a few threads, creates a zmq context in each of them, does some boring stuff, then destroys the context and joins the threads. The test application leaks a few fd's to /dev/urandom in each iteration.
Env
Ubuntu 16.04 64bit
libzmq 4.2.2 + cppzmq
Root cause
The libzmq build I use has the ZMQ_USE_TWEETNACL undefined, so randombytes()
directly reads /dev/urandom storing the fd in global static variable here without any protection against concurrent execution. When calling the context constructor concurrently from multiple threads, the scoped_lock_t here is useless because the corresponding mutex is a class member (each instance has one mutex), and it doesn't prevent multiple concurrent context instances calling randombytes()
.
As a result multiple threads open the file but only one close is issued.