Fix isolationtester race condition for notices sent before blocking.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 10 Sep 2019 02:53:51 +0000 (22:53 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 10 Sep 2019 02:53:51 +0000 (22:53 -0400)
If a test sends a notice just before blocking, it's possible on
slow machines for isolationtester to detect the blocked state before
it's consumed the notice.  (For this to happen, the notice would have
to arrive after isolationtester has waited for data for 10ms, so on
fast/lightly-loaded machines it's hard to reproduce the failure.)
But, if we have seen the backend as blocked, it's certainly already
sent any notices it's going to send.  Therefore, one more round of
PQconsumeInput and PQisBusy should be enough to collect and process
any such notices.

Back-patch of 30717637c into v12.  We're still discussing whether
to back-patch this further and/or back-patch some other recent
isolationtester fixes, but this much is provably necessary to
make the test cases added by 27cc7cd2b stable in v12.

Discussion: https://postgr.es/m/14616.1564251339@sss.pgh.pa.us
Discussion: https://postgr.es/m/E1i7IqC-0000Uc-5H@gemulon.postgresql.org

src/test/isolation/isolationtester.c

index 2f039b83eea8ad72be790134c5fb235a1c3d85f9..768b0bd3c14609971fe994fae15ec379d6d3cb76 100644 (file)
@@ -764,6 +764,28 @@ try_complete_step(Step *step, int flags)
 
                if (waiting)    /* waiting to acquire a lock */
                {
+                   /*
+                    * Since it takes time to perform the lock-check query,
+                    * some data --- notably, NOTICE messages --- might have
+                    * arrived since we looked.  We must call PQconsumeInput
+                    * and then PQisBusy to collect and process any such
+                    * messages.  In the (unlikely) case that PQisBusy then
+                    * returns false, we might as well go examine the
+                    * available result.
+                    */
+                   if (!PQconsumeInput(conn))
+                   {
+                       fprintf(stderr, "PQconsumeInput failed: %s\n",
+                               PQerrorMessage(conn));
+                       exit(1);
+                   }
+                   if (!PQisBusy(conn))
+                       break;
+
+                   /*
+                    * conn is still busy, so conclude that the step really is
+                    * waiting.
+                    */
                    if (!(flags & STEP_RETRY))
                        printf("step %s: %s <waiting ...>\n",
                               step->name, step->sql);