File 10852.patch of Package squid-beta

---------------------
PatchSet 10852 
Date: 2007/06/19 20:27:00
Author: rousskov
Branch: HEAD
Tag: (none) 
Log:
Bug #1981 fix: Tell FwdState that an unregistered and failed Server is
gone.

When a Server unregisters its fd (by calling FwdState::unregister) and
then fails, call FwdState::handleUnregisteredServerEnd to tell FwdState
that the server is done working, so that the transaction does not get
stuck waiting for the server to call FwdState::complete. The old code
would just set Server's FwdState pointer to NULL which was only enough
if FwdState::self was already NULL due to aborts.

This change fixed the bug in my limited ICAP tests.

The whole FwdState relationship with Servers needs a serious revision as
similar bugs probably do (or will) exist. We probably need to maintain a
state variable representing the relationship. The following Server
states are possible, at least: got valid FD and working, closed FD but
still working, stopped working (failure or success). FwdState needs to
be notified when we stopped working. Currently, when Server closes the
FD, deep down the Server stack, it may not be clear whether the Server
is still working or not.

Finally, it may be a good idea for FwdState to notify Server when
FwdState is aborted. Currently, the Server must check that the store
entry is still valid to notice the abort, and it sometimes forgets to do
that, causing store assertions.

Members: 
	src/forward.cc:1.165->1.166 
	src/forward.h:1.11->1.12 
	src/ftp.cc:1.424->1.425 
	src/http.cc:1.524->1.525 

Index: squid3/src/forward.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/forward.cc,v
retrieving revision 1.165
retrieving revision 1.166
diff -u -r1.165 -r1.166
--- squid3/src/forward.cc	26 May 2007 06:38:04 -0000	1.165
+++ squid3/src/forward.cc	19 Jun 2007 20:27:00 -0000	1.166
@@ -1,6 +1,6 @@
 
 /*
- * $Id: forward.cc,v 1.165 2007/05/26 06:38:04 wessels Exp $
+ * $Id: forward.cc,v 1.166 2007/06/19 20:27:00 rousskov Exp $
  *
  * DEBUG: section 17    Request Forwarding
  * AUTHOR: Duane Wessels
@@ -497,6 +497,14 @@
     assert(server_fd == fd);
     server_fd = -1;
 
+    retryOrBail();
+}
+
+void
+FwdState::retryOrBail() {
+    if (!self) // we have aborted before the server called us back
+        return; // we are destroyed when the server clears its Pointer to us
+
     if (checkRetry()) {
         int originserver = (servers->_peer == NULL);
         debugs(17, 3, "fwdServerClosed: re-forwarding (" << n_tries << " tries, " << (squid_curtime - start_t) << " secs)");
@@ -535,6 +543,16 @@
     self = NULL;	// refcounted
 }
 
+// called by the server that failed after calling unregister()
+void
+FwdState::handleUnregisteredServerEnd()
+{
+    debugs(17, 2, "handleUnregisteredServerEnd: self=" << self <<
+        " err=" << err << ' ' << entry->url());
+    assert(server_fd < 0);
+    retryOrBail();
+}
+
 #if USE_SSL
 void
 FwdState::negotiateSSL(int fd)
Index: squid3/src/forward.h
===================================================================
RCS file: /cvsroot/squid/squid3/src/forward.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- squid3/src/forward.h	15 Apr 2007 14:46:16 -0000	1.11
+++ squid3/src/forward.h	19 Jun 2007 20:27:00 -0000	1.12
@@ -32,6 +32,7 @@
     void fail(ErrorState *err);
     void unregister(int fd);
     void complete();
+    void handleUnregisteredServerEnd();
     int reforward();
     bool reforwardableStatus(http_status s);
     void serverClosed(int fd);
@@ -62,6 +63,7 @@
 
     static void logReplyStatus(int tries, http_status status);
     void completed();
+    void retryOrBail();
 
 #if WIP_FWD_LOG
 
Index: squid3/src/ftp.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/ftp.cc,v
retrieving revision 1.424
retrieving revision 1.425
diff -u -r1.424 -r1.425
--- squid3/src/ftp.cc	29 May 2007 13:31:39 -0000	1.424
+++ squid3/src/ftp.cc	19 Jun 2007 20:27:00 -0000	1.425
@@ -1,6 +1,6 @@
 
 /*
- * $Id: ftp.cc,v 1.424 2007/05/29 13:31:39 amosjeffries Exp $
+ * $Id: ftp.cc,v 1.425 2007/06/19 20:27:00 rousskov Exp $
  *
  * DEBUG: section 9     File Transfer Protocol (FTP)
  * AUTHOR: Harvest Derived
@@ -3378,10 +3378,13 @@
 {
     debugs(9,5,HERE << "aborting transaction for " << reason <<
         "; FD " << ctrl.fd << ", Data FD " << data.fd << ", this " << this);
-    if (ctrl.fd >= 0)
+    if (ctrl.fd >= 0) {
         comm_close(ctrl.fd);
-    else
-        delete this;
+        return;
+    }
+    
+    fwd->handleUnregisteredServerEnd();
+    delete this;
 }
 
 #if ICAP_CLIENT
Index: squid3/src/http.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/http.cc,v
retrieving revision 1.524
retrieving revision 1.525
diff -u -r1.524 -r1.525
--- squid3/src/http.cc	17 Jun 2007 21:48:20 -0000	1.524
+++ squid3/src/http.cc	19 Jun 2007 20:27:00 -0000	1.525
@@ -1,6 +1,6 @@
 
 /*
- * $Id: http.cc,v 1.524 2007/06/17 21:48:20 hno Exp $
+ * $Id: http.cc,v 1.525 2007/06/19 20:27:00 rousskov Exp $
  *
  * DEBUG: section 11    Hypertext Transfer Protocol (HTTP)
  * AUTHOR: Harvest Derived
@@ -1935,10 +1935,13 @@
     debugs(11,5, HERE << "aborting transaction for " << reason <<
            "; FD " << fd << ", this " << this);
 
-    if (fd >= 0)
+    if (fd >= 0) {
         comm_close(fd);
-    else
-        delete this;
+        return;
+	}
+
+    fwd->handleUnregisteredServerEnd();
+    delete this;
 }
 
 void