Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3e87ba6

Browse files
committed
Fix pq_getbyte_if_available() function. It was confused on what it
returns if no data is immediately available. Patch by me with numerous fixes from Fujii Masao and Magnus Hagander.
1 parent 1a1ad63 commit 3e87ba6

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

src/backend/libpq/be-secure.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/libpq/be-secure.c,v 1.96 2010/01/15 09:19:02 heikki Exp $
14+
* $PostgreSQL: pgsql/src/backend/libpq/be-secure.c,v 1.97 2010/02/18 11:13:45 heikki Exp $
1515
*
1616
* Since the server static private key ($DataDir/server.key)
1717
* will normally be stored unencrypted so that the database
@@ -256,7 +256,11 @@ secure_read(Port *port, void *ptr, size_t len)
256256
case SSL_ERROR_WANT_READ:
257257
case SSL_ERROR_WANT_WRITE:
258258
if (port->noblock)
259-
return 0;
259+
{
260+
errno = EWOULDBLOCK;
261+
n = -1;
262+
break;
263+
}
260264
#ifdef WIN32
261265
pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
262266
(err == SSL_ERROR_WANT_READ) ?

src/backend/libpq/pqcomm.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.203 2010/02/16 19:26:02 mha Exp $
33+
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.204 2010/02/18 11:13:45 heikki Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -821,8 +821,8 @@ pq_peekbyte(void)
821821
* pq_getbyte_if_available - get a single byte from connection,
822822
* if available
823823
*
824-
* The received byte is stored in *c. Returns 1 if a byte was read, 0 if
825-
* if no data was available, or EOF.
824+
* The received byte is stored in *c. Returns 1 if a byte was read,
825+
* 0 if no data was available, or EOF if trouble.
826826
* --------------------------------
827827
*/
828828
int
@@ -848,6 +848,33 @@ pq_getbyte_if_available(unsigned char *c)
848848
PG_TRY();
849849
{
850850
r = secure_read(MyProcPort, c, 1);
851+
if (r < 0)
852+
{
853+
/*
854+
* Ok if no data available without blocking or interrupted
855+
* (though EINTR really shouldn't happen with a non-blocking
856+
* socket). Report other errors.
857+
*/
858+
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
859+
r = 0;
860+
else
861+
{
862+
/*
863+
* Careful: an ereport() that tries to write to the client would
864+
* cause recursion to here, leading to stack overflow and core
865+
* dump! This message must go *only* to the postmaster log.
866+
*/
867+
ereport(COMMERROR,
868+
(errcode_for_socket_access(),
869+
errmsg("could not receive data from client: %m")));
870+
r = EOF;
871+
}
872+
}
873+
else if (r == 0)
874+
{
875+
/* EOF detected */
876+
r = EOF;
877+
}
851878
}
852879
PG_CATCH();
853880
{

src/backend/replication/walsender.c

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
*
3232
* IDENTIFICATION
33-
* $PostgreSQL: pgsql/src/backend/replication/walsender.c,v 1.6 2010/02/17 04:19:39 tgl Exp $
33+
* $PostgreSQL: pgsql/src/backend/replication/walsender.c,v 1.7 2010/02/18 11:13:46 heikki Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -176,14 +176,7 @@ WalSndHandshake(void)
176176
ProcessConfigFile(PGC_SIGHUP);
177177
}
178178

179-
if (firstchar == EOF)
180-
{
181-
/* standby disconnected */
182-
ereport(COMMERROR,
183-
(errcode(ERRCODE_PROTOCOL_VIOLATION),
184-
errmsg("unexpected EOF on standby connection")));
185-
}
186-
else
179+
if (firstchar != EOF)
187180
{
188181
/*
189182
* Read the message contents. This is expected to be done without
@@ -193,9 +186,7 @@ WalSndHandshake(void)
193186
firstchar = EOF; /* suitable message already logged */
194187
}
195188

196-
197189
/* Handle the very limited subset of commands expected in this phase */
198-
199190
switch (firstchar)
200191
{
201192
case 'Q': /* Query message */
@@ -286,14 +277,16 @@ WalSndHandshake(void)
286277
break;
287278
}
288279

289-
/* 'X' means that the standby is closing the connection */
290280
case 'X':
281+
/* standby is closing the connection */
291282
proc_exit(0);
292283

293284
case EOF:
294-
ereport(ERROR,
285+
/* standby disconnected unexpectedly */
286+
ereport(COMMERROR,
295287
(errcode(ERRCODE_PROTOCOL_VIOLATION),
296288
errmsg("unexpected EOF on standby connection")));
289+
proc_exit(0);
297290

298291
default:
299292
ereport(FATAL,
@@ -315,36 +308,23 @@ CheckClosedConnection(void)
315308
r = pq_getbyte_if_available(&firstchar);
316309
if (r < 0)
317310
{
318-
/* no data available */
319-
if (errno == EAGAIN || errno == EWOULDBLOCK)
320-
return;
321-
322-
/*
323-
* Ok if interrupted, though it shouldn't really happen with
324-
* a non-blocking operation.
325-
*/
326-
if (errno == EINTR)
327-
return;
328-
311+
/* unexpected error or EOF */
329312
ereport(COMMERROR,
330-
(errcode_for_socket_access(),
331-
errmsg("could not receive data from client: %m")));
313+
(errcode(ERRCODE_PROTOCOL_VIOLATION),
314+
errmsg("unexpected EOF on standby connection")));
315+
proc_exit(0);
332316
}
333317
if (r == 0)
334318
{
335-
/* standby disconnected unexpectedly */
336-
ereport(ERROR,
337-
(errcode(ERRCODE_PROTOCOL_VIOLATION),
338-
errmsg("unexpected EOF on standby connection")));
319+
/* no data available without blocking */
320+
return;
339321
}
340322

341323
/* Handle the very limited subset of commands expected in this phase */
342324
switch (firstchar)
343325
{
344326
/*
345-
* 'X' means that the standby is closing down the socket. EOF means
346-
* unexpected loss of standby connection. Either way, perform normal
347-
* shutdown.
327+
* 'X' means that the standby is closing down the socket.
348328
*/
349329
case 'X':
350330
proc_exit(0);

0 commit comments

Comments
 (0)