[bug & patch] early connection close with CGI

Laurent Bercot <[email protected]> Sun, 26 Feb 2006 03:53:21 +0100
Newsgroups gmane.comp.web.fnord
Message-ID <[email protected]>
--BJqKzeeY+Cxlo0R5
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

 Hi,

 Under Linux, when you're listening on a pipe with poll(), and
the writing end of the pipe closes, poll() returns POLLHUP, even
if some data remains in the kernel buffer. It's unreliable to
end the event loop when receiving POLLHUP; you have to keep
reading until read() returns 0.
 fnord suffers from this problem: when retrieving an image through
a CGI script, I often get the image truncated.
 The attached patch corrects the problem. (It doesn't remove the
offending line, it just comments it out. Felix, do as you please.)

-- 
 Laurent

--BJqKzeeY+Cxlo0R5
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="httpd.patch"

--- httpd.c.old	2006-02-25 19:10:32.000000000 +0000
+++ httpd.c	2006-02-25 19:39:32.000000000 +0000
@@ -535,9 +535,9 @@
       if (post_len) ++nr;	/* have post data */
       else close(df[1]);	/* no post data */
 
-      while(poll(pfd,nr,-1)!=-1) {
+      while((poll(pfd,nr,-1)!=-1) || (errno==EINTR)) {
 	/* read from cgi */
-	if (pfd[0].revents&POLLIN) {
+	if (pfd[0].revents&(POLLIN|POLLHUP)) {
 	  if (!(n=read(fd[0],ibuf,sizeof(ibuf)))) break;
 	  if (n<0) goto cgi_500;
 	  /* startup */
@@ -570,7 +570,7 @@
 	    buffer_put(buffer_1,ibuf,n);
 	  }
 	  size+=n;
-	  if (pfd[0].revents&POLLHUP) break;
+//	  if (pfd[0].revents&POLLHUP) break;
 	}
 	/* write to cgi the post data */
 	else if (nr>1 && pfd[1].revents&POLLOUT) {

--BJqKzeeY+Cxlo0R5--