Skip to content

Commit d00391e

Browse files
committed
Sigh, I'm an idiot ... I broke the async startup logic a couple days ago,
by creating a race condition. It wasn't waiting for select() to say write-ready immediately after connect, which meant that you might get an unhelpful 'broken pipe' error message if connect failed, rather than the intended error message.
1 parent fdc85f5 commit d00391e

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.110 2000/01/15 05:37:21 ishii Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.111 2000/01/16 21:18:52 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -931,10 +931,16 @@ connectDBStart(PGconn *conn)
931931
static int
932932
connectDBComplete(PGconn *conn)
933933
{
934-
PostgresPollingStatusType flag;
934+
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
935+
936+
if (conn == NULL || conn->status == CONNECTION_BAD)
937+
return 0;
935938

936939
for (;;) {
937-
flag = PQconnectPoll(conn);
940+
/*
941+
* Wait, if necessary. Note that the initial state (just after
942+
* PQconnectStart) is to wait for the socket to select for writing.
943+
*/
938944
switch (flag)
939945
{
940946
case PGRES_POLLING_ACTIVE:
@@ -964,6 +970,10 @@ connectDBComplete(PGconn *conn)
964970
conn->status = CONNECTION_BAD;
965971
return 0;
966972
}
973+
/*
974+
* Now try to advance the state machine.
975+
*/
976+
flag = PQconnectPoll(conn);
967977
}
968978
}
969979

@@ -1347,12 +1357,16 @@ PQconnectPoll(PGconn *conn)
13471357
* Starts the process of passing the values of a standard set of environment
13481358
* variables to the backend.
13491359
*
1350-
* ---------------- */
1360+
* ----------------
1361+
*/
13511362
PGsetenvHandle
13521363
PQsetenvStart(PGconn *conn)
13531364
{
13541365
struct pg_setenv_state *handle;
13551366

1367+
if (conn == NULL || conn->status == CONNECTION_BAD)
1368+
return NULL;
1369+
13561370
if ((handle = malloc(sizeof(struct pg_setenv_state))) == NULL)
13571371
{
13581372
printfPQExpBuffer(&conn->errorMessage,
@@ -1621,13 +1635,16 @@ int
16211635
PQsetenv(PGconn *conn)
16221636
{
16231637
PGsetenvHandle handle;
1624-
PostgresPollingStatusType flag;
1638+
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
16251639

16261640
if ((handle = PQsetenvStart(conn)) == NULL)
16271641
return 0;
16281642

16291643
for (;;) {
1630-
flag = PQsetenvPoll(conn);
1644+
/*
1645+
* Wait, if necessary. Note that the initial state (just after
1646+
* PQsetenvStart) is to wait for the socket to select for writing.
1647+
*/
16311648
switch (flag)
16321649
{
16331650
case PGRES_POLLING_ACTIVE:
@@ -1653,10 +1670,14 @@ PQsetenv(PGconn *conn)
16531670
break;
16541671

16551672
default:
1656-
/* Just in case we failed to set it in PQconnectPoll */
1673+
/* Just in case we failed to set it in PQsetenvPoll */
16571674
conn->status = CONNECTION_BAD;
16581675
return 0;
16591676
}
1677+
/*
1678+
* Now try to advance the state machine.
1679+
*/
1680+
flag = PQsetenvPoll(conn);
16601681
}
16611682
}
16621683

0 commit comments

Comments
 (0)