Do not return from PQrequestCancel until postmaster has finished
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Oct 2003 19:52:44 +0000 (19:52 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 2 Oct 2003 19:52:44 +0000 (19:52 +0000)
processing the request; this ensures that the request won't be taken
to cancel a subsequently-issued query.  Race condition originally
noted by Oliver Jowett in the context of JDBC, but libpq has it too.

src/interfaces/libpq/fe-connect.c

index 9108697f5c3b316ccee2c1dc6f5d8d2c6a994ee4..5bd42f24df2d2bb29ff34dc44f13a1e93528031e 100644 (file)
@@ -2262,7 +2262,23 @@ retry4:
                goto cancel_errReturn;
        }
 
-       /* Sent it, done */
+       /*
+        * Wait for the postmaster to close the connection, which indicates that
+        * it's processed the request.  Without this delay, we might issue another
+        * command only to find that our cancel zaps that command instead of the
+        * one we thought we were canceling.  Note we don't actually expect this
+        * read to obtain any data, we are just waiting for EOF to be signaled.
+        */
+retry5:
+       if (recv(tmpsock, (char *) &crp, 1, 0) < 0)
+       {
+               if (SOCK_ERRNO == EINTR)
+                       /* Interrupted system call - we'll just try again */
+                       goto retry5;
+               /* we ignore other error conditions */
+       }
+
+       /* All done */
        closesocket(tmpsock);
 #ifdef WIN32
        WSASetLastError(save_errno);