Alexey Izbyshev <izbys...@ispras.ru> 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, 
https://bitbucket.org/tildeslash/monit/issues/649/init_env-fails-if-open-2-returns-an

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: https://bugs.python.org/file47452/repro.c

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32849>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to