Multithreading: Errno 9 (EBADF) – Bad File Number

If you’re randomly hitting “Bad File Number” error (Errno: 9) in a multithreaded application on Unix, then most likely you’re trapped in “Opened once but closed twice” bug.

What is this “Opened once but closed twice” bug?

Check out the below code:

foo ()
     fd = open();
     // some processing.
     close (fd);
     // some processing.
     close (fd);

The above code will work smoothly for a single threaded application. But in case of multithreading, if a thread “X” call open() (either in same function foo or any other function bar), when thread “Y” is in between the two close() calls, then in that case, thread “X” will get the same fd for open which thread “Y” was using (since open always return the smallest unused descriptor). File descriptors are shared by all the threads. So when thread “Y” carries out its second close(), it actually closes the file descriptor of thread “X”, which was valid & in use.

FD numbers are shared by all the threads of the same process. That means when thread “Y” closes FD number “n”, twice. Then in the second case of closing descriptor “n”, can result closing in use descriptor “n” by thread “X”. When the thread “X” starts using the descriptor “n”, it gets error – “Bad File Number” (Errno: 9).

Thus closing a descriptor twice in multithreaded application leads to random “Bad File Number” error (Errno: 9).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s