On Sat, 2012-06-09 at 11:06 +0400, Denis Bilenko wrote:
> > but I get:
> > error: [Errno 9] Bad file descriptor
>
> This means the socket was closed by the server, maybe your application
> closes it somewhere.
>
That's one possible proximate cause. There are others.
EBADF means "the kernel doesn't have an open file associated with this
file descriptor".
* the FD has been closed
* the FD was never open
* the FD number is invalid, i.e. negative or too large
The usual cause is that your application close()d it (probably in
reaction to an EOF, i.e. the aforementioned server closing the file) and
then did not tell the other threads about it. This is reasonably OK, as
long as the closing thread also sets the file descriptor variable to -1,
which counts as "telling" in my book.
You need to use strace(8) (assuming Linux) to figure out whether this is
a real problem with your code. The call which returns EBADF either uses
-1 as its first parameter (OK) or some non-negative integer (definitely
not OK).
There is absolutely no way to go from a working file descriptor to one
which throws an EBADF without your application code (or a library it
uses) calling close().
(OK, OK – with the exception of exec*()/clone(), if the close-on-exec
flag is set on that descriptor.)
--
-- Matthias Urlichs