Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/kern Fix a deadlock where one thread writes to a pipe, h...



details:   https://anonhg.NetBSD.org/src/rev/488408249e17
branches:  trunk
changeset: 988187:488408249e17
user:      hannken <hannken%NetBSD.org@localhost>
date:      Sat Oct 02 07:35:40 2021 +0000

description:
Fix a deadlock where one thread writes to a pipe, has more data
and no space in the pipe and waits on "pipe_wcv" while the reader
is closing the pipe and waits on "pipe_draincv".

Swap the test for "PIPE_EOF" and the "cv_wait_sig()" in "pipe_write()".

PR bin/56422 "zgrep -l sometimes hangs"

diffstat:

 sys/kern/sys_pipe.c |  15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diffs (43 lines):

diff -r 0f4446eecaab -r 488408249e17 sys/kern/sys_pipe.c
--- a/sys/kern/sys_pipe.c       Sat Oct 02 02:07:41 2021 +0000
+++ b/sys/kern/sys_pipe.c       Sat Oct 02 07:35:40 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_pipe.c,v 1.156 2021/09/27 00:51:10 thorpej Exp $   */
+/*     $NetBSD: sys_pipe.c,v 1.157 2021/10/02 07:35:40 hannken Exp $   */
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.156 2021/09/27 00:51:10 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.157 2021/10/02 07:35:40 hannken Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -694,11 +694,6 @@
                                break;
                        }
 
-                       pipeunlock(wpipe);
-                       error = cv_wait_sig(&wpipe->pipe_wcv, lock);
-                       (void)pipelock(wpipe, false);
-                       if (error != 0)
-                               break;
                        /*
                         * If read side wants to go away, we just issue a signal
                         * to ourselves.
@@ -707,6 +702,12 @@
                                error = EPIPE;
                                break;
                        }
+
+                       pipeunlock(wpipe);
+                       error = cv_wait_sig(&wpipe->pipe_wcv, lock);
+                       (void)pipelock(wpipe, false);
+                       if (error != 0)
+                               break;
                        wakeup_state = wpipe->pipe_state;
                }
        }



Home | Main Index | Thread Index | Old Index