File tree Expand file tree Collapse file tree 4 files changed +25
-1
lines changed
Expand file tree Collapse file tree 4 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -1247,9 +1247,13 @@ internal_flush(void)
12471247
12481248 /*
12491249 * We drop the buffered data anyway so that processing can
1250- * continue, even though we'll probably quit soon.
1250+ * continue, even though we'll probably quit soon. We also
1251+ * set a flag that'll cause the next CHECK_FOR_INTERRUPTS
1252+ * to terminate the connection.
12511253 */
12521254 PqSendStart = PqSendPointer = 0 ;
1255+ ClientConnectionLost = 1 ;
1256+ InterruptPending = 1 ;
12531257 return EOF ;
12541258 }
12551259
Original file line number Diff line number Diff line change @@ -2823,6 +2823,18 @@ ProcessInterrupts(void)
28232823 (errcode (ERRCODE_ADMIN_SHUTDOWN ),
28242824 errmsg ("terminating connection due to administrator command" )));
28252825 }
2826+ if (ClientConnectionLost )
2827+ {
2828+ QueryCancelPending = false; /* lost connection trumps QueryCancel */
2829+ ImmediateInterruptOK = false; /* not idle anymore */
2830+ DisableNotifyInterrupt ();
2831+ DisableCatchupInterrupt ();
2832+ /* don't send to client, we already know the connection to be dead. */
2833+ whereToSendOutput = DestNone ;
2834+ ereport (FATAL ,
2835+ (errcode (ERRCODE_CONNECTION_FAILURE ),
2836+ errmsg ("connection to client lost" )));
2837+ }
28262838 if (QueryCancelPending )
28272839 {
28282840 QueryCancelPending = false;
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ ProtocolVersion FrontendProtocol;
2929volatile bool InterruptPending = false;
3030volatile bool QueryCancelPending = false;
3131volatile bool ProcDiePending = false;
32+ volatile bool ClientConnectionLost = false;
3233volatile bool ImmediateInterruptOK = false;
3334volatile uint32 InterruptHoldoffCount = 0 ;
3435volatile uint32 CritSectionCount = 0 ;
Original file line number Diff line number Diff line change 5555 * course, only if the interrupt holdoff counter is zero). See the
5656 * related code for details.
5757 *
58+ * A lost connection is handled similarly, although the loss of connection
59+ * does not raise a signal, but is detected when we fail to write to the
60+ * socket. If there was a signal for a broken connection, we could make use of
61+ * it by setting ClientConnectionLost in the signal handler.
62+ *
5863 * A related, but conceptually distinct, mechanism is the "critical section"
5964 * mechanism. A critical section not only holds off cancel/die interrupts,
6065 * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
@@ -70,6 +75,8 @@ extern PGDLLIMPORT volatile bool InterruptPending;
7075extern volatile bool QueryCancelPending ;
7176extern volatile bool ProcDiePending ;
7277
78+ extern volatile bool ClientConnectionLost ;
79+
7380/* these are marked volatile because they are examined by signal handlers: */
7481extern volatile bool ImmediateInterruptOK ;
7582extern PGDLLIMPORT volatile uint32 InterruptHoldoffCount ;
You can’t perform that action at this time.
0 commit comments