- (djm) Suppress error messages on channel close shutdown() failurs
   works around Linux bug. Patch from Zack Weinberg <zack@wolery.cumb.org>
diff --git a/nchan.c b/nchan.c
index cef5649..51c5a4c 100644
--- a/nchan.c
+++ b/nchan.c
@@ -483,7 +483,14 @@
 		return;
 	debug("channel %d: close_read", c->self);
 	if (c->sock != -1) {
-		if (shutdown(c->sock, SHUT_RD) < 0)
+		/* 
+		 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
+		 * write side has been closed already. (bug on Linux)
+		 */
+		if (shutdown(c->sock, SHUT_RD) < 0
+		    && (errno != ENOTCONN
+			|| c->ostate == CHAN_OUTPUT_OPEN
+			|| c->ostate == CHAN_OUTPUT_WAIT_DRAIN))
 			error("channel %d: chan_shutdown_read: shutdown() failed for fd%d [i%d o%d]: %.100s",
 			    c->self, c->sock, c->istate, c->ostate, strerror(errno));
 	} else {