NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
kern/61010: EBADF on pty operations during high pty load
>Number: 61010
>Category: kern
>Synopsis: EBADF on pty operations during high pty load
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: kern-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sat Nov 14 22:20:00 +0000 2026
>Originator: John Goerzen
>Release: 11.1
>Organization:
>Environment:
NetBSD [redacted] 11.1 NetBSD 11.1 (GENERIC) #0: Mon Nov 2 16:34:32 UTC 2026 mkrepro%mkrepro.NetBSD.org@localhost:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
While many processes are opening and closing ptys simultaneously, I observed two situations in which normal pty operations, which have no reason to fail, return EBADF>
1. read() on a slave pty and write() on a master pty can fail with EBADF
2. ioctl(TIOCPTMGET) on an open /dev/ptm fd fails with EBADF, and occasionally this ioctl fails with ENOENT.
Case 2 occurs within openpty(). When examining the situation with ktrace, it appears that the ioctl fails as indicated above, then openpty() tries to use /dev/ptyp0, which doesn't exist (giving ENOENT). openpty() then returns that ENOENT from attempting to use /dev/ptyp0, rather then the EBADF from the ioctl.
I should note that even when EBADF is returned, the file descriptor remains open.
The test program below has no more than 8 ptys open at a time, so pty or fd limits aren't at issue here. However, as a side note, openpty also returns ENOENT in the case of exhaustion.
I also recently reported kern/60993 in which pty churn can lead to EIO failures. I can't say for sure, but perhaps these are related. The other bug I reported in the pty system, kern/60935, seems less likely to be related.
I discovered this by running the C-Kermit test suite on NetBSD under parallelization. This causes a lot of ptys to be opened and closed. I have a replication case below.
This was duplicated on an amd64 4-CPU system running under qemu+KVM. Unlike the other two pty bugs I submitted, I was unable to reproduce this one on my single-CPU 600MHz alpha. Whether that is because the Alpha is much slower, or because it has only one core, I can't say.
>How-To-Repeat:
I have a program, included below, that provokes these errors. It creates processes that open and close ptys rapidly, inducing churn. It also has processes that read and write down ptys. When run with no churning processes, there are no failurs. In a test run on my amd64 machine, I saw this:
cc -o ptyebadf ptyebadf.c
./ptyebadf | sort | uniq -c
360 churner ioctl(TIOCPTMGET): Bad file descriptor
7 churner ioctl(TIOCPTMGET): No such file or directory
7 read() from slave: Bad file descriptor
78 worker ioctl(TIOCPTMGET): Bad file descriptor
7 write() to master: Bad file descriptor
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#define PROCS 4
#define TRIALS 5000
static int
getpty(int ptm, const char *who, int *m, int *s)
{
struct ptmget pt;
if (ioctl(ptm, TIOCPTMGET, &pt) == -1) {
printf("%s ioctl(TIOCPTMGET): %s\n", who, strerror(errno));
return -1;
}
*m = pt.cfd;
*s = pt.sfd;
return 0;
}
static void
churn(int ptm)
{
int t, m, s;
for (t = 0; t < 4 * TRIALS; t++) {
if (getpty(ptm, "churner", &m, &s) == -1)
continue;
close(m);
close(s);
}
_exit(0);
}
static void
work(int ptm)
{
struct termios tio;
char buf[64];
int t, m, s;
for (t = 0; t < TRIALS; t++) {
if (getpty(ptm, "worker", &m, &s) == -1)
continue;
tcgetattr(s, &tio);
tio.c_lflag &= ~ECHO;
tcsetattr(s, TCSANOW, &tio);
if (fork() == 0) {
close(m);
alarm(10);
if (read(s, buf, sizeof(buf)) == -1)
printf("read() from slave: %s\n",
strerror(errno));
_exit(0);
}
close(s);
if (write(m, "x\n", 2) == -1)
printf("write() to master: %s\n", strerror(errno));
wait(NULL);
close(m);
}
_exit(0);
}
int
main(void)
{
int i, ptm;
setvbuf(stdout, NULL, _IOLBF, 0);
if ((ptm = open("/dev/ptm", O_RDWR)) == -1) {
perror("/dev/ptm");
return 1;
}
for (i = 0; i < PROCS; i++) {
if (fork() == 0)
churn(ptm);
if (fork() == 0)
work(ptm);
}
while (wait(NULL) > 0)
;
return 0;
}
>Fix:
I don't know
Home |
Main Index |
Thread Index |
Old Index