24
24
*
25
25
*
26
26
* IDENTIFICATION
27
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.21 1998/09/03 02:10:50 momjian Exp $
27
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.22 1998/09/20 04:51:12 momjian Exp $
28
28
*
29
29
*-------------------------------------------------------------------------
30
30
*/
@@ -366,6 +366,11 @@ pqReadData(PGconn *conn)
366
366
#if defined(EWOULDBLOCK ) && (!defined(EAGAIN ) || (EWOULDBLOCK != EAGAIN ))
367
367
if (errno == EWOULDBLOCK )
368
368
return 0 ;
369
+ #endif
370
+ /* We might get ECONNRESET here if using TCP and backend died */
371
+ #ifdef ECONNRESET
372
+ if (errno == ECONNRESET )
373
+ goto definitelyFailed ;
369
374
#endif
370
375
sprintf (conn -> errorMessage ,
371
376
"pqReadData() -- read() failed: errno=%d\n%s\n" ,
@@ -409,6 +414,11 @@ pqReadData(PGconn *conn)
409
414
#if defined(EWOULDBLOCK ) && (!defined(EAGAIN ) || (EWOULDBLOCK != EAGAIN ))
410
415
if (errno == EWOULDBLOCK )
411
416
return 0 ;
417
+ #endif
418
+ /* We might get ECONNRESET here if using TCP and backend died */
419
+ #ifdef ECONNRESET
420
+ if (errno == ECONNRESET )
421
+ goto definitelyFailed ;
412
422
#endif
413
423
sprintf (conn -> errorMessage ,
414
424
"pqReadData() -- read() failed: errno=%d\n%s\n" ,
@@ -425,6 +435,7 @@ pqReadData(PGconn *conn)
425
435
* OK, we are getting a zero read even though select() says ready.
426
436
* This means the connection has been closed. Cope.
427
437
*/
438
+ definitelyFailed :
428
439
sprintf (conn -> errorMessage ,
429
440
"pqReadData() -- backend closed the channel unexpectedly.\n"
430
441
"\tThis probably means the backend terminated abnormally"
@@ -460,7 +471,6 @@ pqFlush(PGconn *conn)
460
471
/* Prevent being SIGPIPEd if backend has closed the connection. */
461
472
#ifndef WIN32
462
473
pqsigfunc oldsighandler = pqsignal (SIGPIPE , SIG_IGN );
463
-
464
474
#endif
465
475
466
476
int sent = send (conn -> sock , ptr , len , 0 );
@@ -471,7 +481,11 @@ pqFlush(PGconn *conn)
471
481
472
482
if (sent < 0 )
473
483
{
474
- /* Anything except EAGAIN or EWOULDBLOCK is trouble */
484
+ /*
485
+ * Anything except EAGAIN or EWOULDBLOCK is trouble.
486
+ * If it's EPIPE or ECONNRESET, assume we've lost the
487
+ * backend connection permanently.
488
+ */
475
489
switch (errno )
476
490
{
477
491
#ifdef EAGAIN
@@ -482,10 +496,27 @@ pqFlush(PGconn *conn)
482
496
case EWOULDBLOCK :
483
497
break ;
484
498
#endif
499
+ case EPIPE :
500
+ #ifdef ECONNRESET
501
+ case ECONNRESET :
502
+ #endif
503
+ sprintf (conn -> errorMessage ,
504
+ "pqFlush() -- backend closed the channel unexpectedly.\n"
505
+ "\tThis probably means the backend terminated abnormally"
506
+ " before or while processing the request.\n" );
507
+ conn -> status = CONNECTION_BAD ; /* No more connection */
508
+ #ifdef WIN32
509
+ closesocket (conn -> sock );
510
+ #else
511
+ close (conn -> sock );
512
+ #endif
513
+ conn -> sock = -1 ;
514
+ return EOF ;
485
515
default :
486
516
sprintf (conn -> errorMessage ,
487
- "pqFlush() -- couldn't send data: errno=%d\n%s\n" ,
517
+ "pqFlush() -- couldn't send data: errno=%d\n%s\n" ,
488
518
errno , strerror (errno ));
519
+ /* We don't assume it's a fatal error... */
489
520
return EOF ;
490
521
}
491
522
}
0 commit comments