Alexey Izbyshev <> added the comment:

OK, never mind with the test. I've finally got to a FreeBSD box and reproduced 
the problem. It has to do with 'revoke' feature of *BSD. When revoke is called 
on a terminal device (as part of logout process, for example), all descriptors 
associated with it are invalidated. They can be dup'ed, but any I/O (including 
fstat) will fail with EBADF. The attached 'repro.c' demonstrates the same 
behavior as Python in your ktrace log.

# sleep 5; ./repro >&err.txt &
# exit
(login again)
# cat err.txt
isatty: Inappropriate ioctl for device
dup ok: 3
fstat: Bad file descriptor

So it seems that in your case the parent of your Python processes passed a 
descriptor referring to the terminal as fd 0, and then terminal got revoked at 
some point. People have stumbled on that, for example,

As for Python, it seems OK to fix it as in #30225 since the fd is unusable for 
I/O anyway. I think that we can even drop dup-based validation from 
is_valid_fd() since there is a corner case for Linux too: if a descriptor 
opened with O_PATH inherited as a standard one, dup() will succeed but fstat() 
will fail in kernels before 3.6. And we do fstat() almost immediately after 
is_valid_fd() to get blksize, so the dup-based optimization doesn't seem worth 
the trouble.

Victor, do you have an opinion on that?

components: +FreeBSD, IO
nosy: +koobs
versions: +Python 3.7
Added file:

Python tracker <>
Python-bugs-list mailing list

Reply via email to