29
29
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
30
30
* Portions Copyright (c) 1994, Regents of the University of California
31
31
*
32
- * $Id: pqcomm.c,v 1.123 2001/11/05 17:46:25 momjian Exp $
32
+ * $Id: pqcomm.c,v 1.124 2001/11/12 04:54:08 tgl Exp $
33
33
*
34
34
*-------------------------------------------------------------------------
35
35
*/
@@ -503,19 +503,18 @@ pq_recvbuf(void)
503
503
continue ; /* Ok if interrupted */
504
504
505
505
/*
506
- * We would like to use elog() here, but dare not because elog
507
- * tries to write to the client, which will cause problems if
508
- * we have a hard communications failure ... So just write the
509
- * message to the postmaster log .
506
+ * Careful: an elog() that tries to write to the client
507
+ * would cause recursion to here, leading to stack overflow
508
+ * and core dump! This message must go *only* to the postmaster
509
+ * log. elog(DEBUG) is presently safe .
510
510
*/
511
- fprintf (stderr , "pq_recvbuf: recv() failed: %s\n" ,
512
- strerror (errno ));
511
+ elog (DEBUG , "pq_recvbuf: recv() failed: %m" );
513
512
return EOF ;
514
513
}
515
514
if (r == 0 )
516
515
{
517
- /* as above, elog not safe */
518
- fprintf ( stderr , "pq_recvbuf: unexpected EOF on client connection\n " );
516
+ /* as above, only write to postmaster log */
517
+ elog ( DEBUG , "pq_recvbuf: unexpected EOF on client connection" );
519
518
return EOF ;
520
519
}
521
520
/* r contains number of bytes read, so just incr length */
@@ -655,6 +654,8 @@ pq_putbytes(const char *s, size_t len)
655
654
int
656
655
pq_flush (void )
657
656
{
657
+ static int last_reported_send_errno = 0 ;
658
+
658
659
unsigned char * bufptr = PqSendBuffer ;
659
660
unsigned char * bufend = PqSendBuffer + PqSendPointer ;
660
661
@@ -675,12 +676,20 @@ pq_flush(void)
675
676
continue ; /* Ok if we were interrupted */
676
677
677
678
/*
678
- * We would like to use elog() here, but cannot because elog
679
- * tries to write to the client, which would cause a recursive
680
- * flush attempt! So just write it out to the postmaster log.
679
+ * Careful: an elog() that tries to write to the client
680
+ * would cause recursion to here, leading to stack overflow
681
+ * and core dump! This message must go *only* to the postmaster
682
+ * log. elog(DEBUG) is presently safe.
683
+ *
684
+ * If a client disconnects while we're in the midst of output,
685
+ * we might write quite a bit of data before we get to a safe
686
+ * query abort point. So, suppress duplicate log messages.
681
687
*/
682
- fprintf (stderr , "pq_flush: send() failed: %s\n" ,
683
- strerror (errno ));
688
+ if (errno != last_reported_send_errno )
689
+ {
690
+ last_reported_send_errno = errno ;
691
+ elog (DEBUG , "pq_flush: send() failed: %m" );
692
+ }
684
693
685
694
/*
686
695
* We drop the buffered data anyway so that processing can
@@ -689,8 +698,11 @@ pq_flush(void)
689
698
PqSendPointer = 0 ;
690
699
return EOF ;
691
700
}
701
+
702
+ last_reported_send_errno = 0 ; /* reset after any successful send */
692
703
bufptr += r ;
693
704
}
705
+
694
706
PqSendPointer = 0 ;
695
707
return 0 ;
696
708
}
@@ -709,8 +721,8 @@ pq_eof(void)
709
721
710
722
if (res < 0 )
711
723
{
712
- /* don't try to elog here... */
713
- fprintf ( stderr , "pq_eof: recv() failed: %s\n" , strerror ( errno ) );
724
+ /* can log to postmaster log only */
725
+ elog ( DEBUG , "pq_eof: recv() failed: %m" );
714
726
return EOF ;
715
727
}
716
728
if (res == 0 )
0 commit comments