Module Name:    src
Committed By:   hannken
Date:           Sat Oct  2 07:35:41 UTC 2021

Modified Files:
        src/sys/kern: sys_pipe.c

Log Message:
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"


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/sys/kern/sys_pipe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.156 src/sys/kern/sys_pipe.c:1.157
--- src/sys/kern/sys_pipe.c:1.156	Mon Sep 27 00:51:10 2021
+++ src/sys/kern/sys_pipe.c	Sat Oct  2 07:35:40 2021
@@ -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 @@ pipe_write(file_t *fp, off_t *offset, st
 				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 @@ pipe_write(file_t *fp, off_t *offset, st
 				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;
 		}
 	}

Reply via email to