isolationtester: Use atexit()
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 5 Jan 2019 14:05:49 +0000 (15:05 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 7 Jan 2019 15:25:16 +0000 (16:25 +0100)
Replace exit_nicely() calls with standard exit() and register the
cleanup actions using atexit().

Reviewed-by: Alvaro Herrera <alvherre@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/ec4135ba-84e9-28bf-b584-0e78d47448d5@2ndquadrant.com/

src/test/isolation/isolationtester.c

index 9134b0505eb7596c80e5cda512875999f5cf7e25..e59cf667bad44d882326e24203ceae65f031fd78 100644 (file)
@@ -32,7 +32,6 @@ static int    nconns = 0;
 /* In dry run only output permutations to be run by the tester. */
 static int dry_run = false;
 
-static void exit_nicely(void) pg_attribute_noreturn();
 static void run_testspec(TestSpec *testspec);
 static void run_all_permutations(TestSpec *testspec);
 static void run_all_permutations_recurse(TestSpec *testspec, int nsteps,
@@ -51,15 +50,14 @@ static void printResultSet(PGresult *res);
 static void isotesterNoticeProcessor(void *arg, const char *message);
 static void blackholeNoticeProcessor(void *arg, const char *message);
 
-/* close all connections and exit */
 static void
-exit_nicely(void)
+disconnect_atexit(void)
 {
    int         i;
 
    for (i = 0; i < nconns; i++)
-       PQfinish(conns[i]);
-   exit(1);
+       if (conns[i])
+           PQfinish(conns[i]);
 }
 
 int
@@ -140,7 +138,7 @@ main(int argc, char **argv)
        {
            fprintf(stderr, "duplicate step name: %s\n",
                    testspec->allsteps[i]->name);
-           exit_nicely();
+           exit(1);
        }
    }
 
@@ -162,6 +160,7 @@ main(int argc, char **argv)
     */
    nconns = 1 + testspec->nsessions;
    conns = calloc(nconns, sizeof(PGconn *));
+   atexit(disconnect_atexit);
    backend_pids = calloc(nconns, sizeof(*backend_pids));
    for (i = 0; i < nconns; i++)
    {
@@ -170,7 +169,7 @@ main(int argc, char **argv)
        {
            fprintf(stderr, "Connection %d to database failed: %s",
                    i, PQerrorMessage(conns[i]));
-           exit_nicely();
+           exit(1);
        }
 
        /*
@@ -196,7 +195,7 @@ main(int argc, char **argv)
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "message level setup failed: %s", PQerrorMessage(conns[i]));
-           exit_nicely();
+           exit(1);
        }
        PQclear(res);
 
@@ -210,14 +209,14 @@ main(int argc, char **argv)
            {
                fprintf(stderr, "backend pid query returned %d rows and %d columns, expected 1 row and 1 column",
                        PQntuples(res), PQnfields(res));
-               exit_nicely();
+               exit(1);
            }
        }
        else
        {
            fprintf(stderr, "backend pid query failed: %s",
                    PQerrorMessage(conns[i]));
-           exit_nicely();
+           exit(1);
        }
        PQclear(res);
    }
@@ -254,7 +253,7 @@ main(int argc, char **argv)
    {
        fprintf(stderr, "prepare of lock wait query failed: %s",
                PQerrorMessage(conns[0]));
-       exit_nicely();
+       exit(1);
    }
    PQclear(res);
    termPQExpBuffer(&wait_query);
@@ -265,9 +264,6 @@ main(int argc, char **argv)
     */
    run_testspec(testspec);
 
-   /* Clean up and exit */
-   for (i = 0; i < nconns; i++)
-       PQfinish(conns[i]);
    return 0;
 }
 
@@ -375,7 +371,7 @@ run_named_permutations(TestSpec *testspec)
            {
                fprintf(stderr, "undefined step \"%s\" specified in permutation\n",
                        p->stepnames[j]);
-               exit_nicely();
+               exit(1);
            }
            steps[j] = *this;
        }
@@ -510,7 +506,7 @@ run_permutation(TestSpec *testspec, int nsteps, Step **steps)
        else if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "setup failed: %s", PQerrorMessage(conns[0]));
-           exit_nicely();
+           exit(1);
        }
        PQclear(res);
    }
@@ -530,7 +526,7 @@ run_permutation(TestSpec *testspec, int nsteps, Step **steps)
                fprintf(stderr, "setup of session %s failed: %s",
                        testspec->sessions[i]->name,
                        PQerrorMessage(conns[i + 1]));
-               exit_nicely();
+               exit(1);
            }
            PQclear(res);
        }
@@ -612,7 +608,7 @@ run_permutation(TestSpec *testspec, int nsteps, Step **steps)
        {
            fprintf(stdout, "failed to send query for step %s: %s\n",
                    step->name, PQerrorMessage(conn));
-           exit_nicely();
+           exit(1);
        }
 
        /* Try to complete this step without blocking.  */
@@ -723,7 +719,7 @@ try_complete_step(Step *step, int flags)
    if (sock < 0)
    {
        fprintf(stderr, "invalid socket: %s", PQerrorMessage(conn));
-       exit_nicely();
+       exit(1);
    }
 
    gettimeofday(&start_time, NULL);
@@ -741,7 +737,7 @@ try_complete_step(Step *step, int flags)
            if (errno == EINTR)
                continue;
            fprintf(stderr, "select failed: %s\n", strerror(errno));
-           exit_nicely();
+           exit(1);
        }
        else if (ret == 0)      /* select() timeout: check for lock wait */
        {
@@ -761,7 +757,7 @@ try_complete_step(Step *step, int flags)
                {
                    fprintf(stderr, "lock wait query failed: %s",
                            PQerrorMessage(conns[0]));
-                   exit_nicely();
+                   exit(1);
                }
                waiting = ((PQgetvalue(res, 0, 0))[0] == 't');
                PQclear(res);
@@ -818,14 +814,14 @@ try_complete_step(Step *step, int flags)
            {
                fprintf(stderr, "step %s timed out after 200 seconds\n",
                        step->name);
-               exit_nicely();
+               exit(1);
            }
        }
        else if (!PQconsumeInput(conn)) /* select(): data available */
        {
            fprintf(stderr, "PQconsumeInput failed: %s\n",
                    PQerrorMessage(conn));
-           exit_nicely();
+           exit(1);
        }
    }