Skip to contentSkip to conversation

[2.2] pipe_write can block even with non-blocking fd

PATCH New Peter Benie 1 participant 1 message Dec 7, 2002 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 [2.2] pipe_write can block even with non-blocking fd

Please apply the following patch. This makes non-blocking writes to pipe actually non-blocking.

A program that demonstrates the bug can be found at:
http://www.chiark.greenend.org.uk/~peterb/linux/nonblock/pipe-testnonblock.c

fs/pipe.c renamed from fs/pipe.c.orig +11 -3
@@ -105,9 +105,17 @@
 	else
 		free = 1; /* can't do it atomically, wait for any free space */
 	up(&inode->i_sem);
-	if (down_interruptible(&inode->i_atomic_write)) {
-		down(&inode->i_sem);
-		return -ERESTARTSYS;
+	if (filp->f_flags & O_NONBLOCK) {
+		if (down_trylock(&inode->i_atomic_write)) {
+			down(&inode->i_sem);
+			return -ERESTARTSYS;
+		}
+	}
+	else {
+		if (down_interruptible(&inode->i_atomic_write)) {
+			down(&inode->i_sem);
+			return -ERESTARTSYS;
+		}
 	}
 	while (count>0) {
 		while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {