Commit ffb3379
committed
Cygwin: open: Fix deadlock for opening fifo both side in a process
Currently, opening both side of fifo in a process hangs if the
read side is opened first. The following test case exhibit the
hang while it works in linux.
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#define fifo1 "/tmp/fifo-test"
void *thr1(void *)
{
int fd;
usleep(100000);
fd = open(fifo1, O_WRONLY);
write(fd, "A", 1);
usleep(100000);
close(fd);
return NULL;
}
int main()
{
int fd;
pthread_t th;
char c;
mkfifo(fifo1, 0600);
pthread_create(&th, NULL, thr1, NULL);
fd = open(fifo1, O_RDONLY);
pthread_join(th, NULL);
read(fd, &c, 1);
write(1, &c, 1);
close(fd);
unlink(fifo1);
return 0;
}
The mechanism of hang is as follows. The main thread tries to open
the fifo for reading, but fhandler_fifo::open blocks until it detects
that someone is opening the fifo for writing. The other thread wants
to do that, but it never gets to the point of calling fhandler_fifo::
open because it is stuck waiting for the lock on cygheap->fdtab.
To fix this, this patch delays the construction of the cygheap_fdnew
object fd until after fhandler_fifo::open has been called.
Fixes: df63bd4 ("* cygheap.h (cygheap_fdmanip): New class: simplifies locking and retrieval of fds from cygheap->fdtab.")
Reviewd-by: Ken Brown <[email protected]>
Signed-off-by: Takashi Yano <[email protected]>
(cherry picked from commit cec8a66)1 parent 249b8e8 commit ffb3379
2 files changed
+9
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1472 | 1472 | | |
1473 | 1473 | | |
1474 | 1474 | | |
1475 | | - | |
1476 | | - | |
1477 | | - | |
1478 | | - | |
1479 | | - | |
1480 | 1475 | | |
1481 | 1476 | | |
1482 | 1477 | | |
| |||
1577 | 1572 | | |
1578 | 1573 | | |
1579 | 1574 | | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
1580 | 1581 | | |
1581 | 1582 | | |
1582 | 1583 | | |
| |||
0 commit comments