Skip to content

Commit 93871b6

Browse files
committed
Use _exit(2) for SIGQUIT during ProcessStartupPacket, too.
Bring the signal handling for startup-packet collection into line with the policy established in commits bedadc7 and 8e19a82, namely don't risk running atexit callbacks when handling SIGQUIT. Ideally, we'd not do so for SIGTERM or timeout interrupts either, but that change seems a bit too risky for the back branches. For now, just improve the comments in this area to describe the risk. Also relocate where BackendInitialize re-disables these interrupts, to minimize the code span where they're active. This doesn't buy a whole lot of safety, but it can't hurt. In passing, rename startup_die() to remove confusion about whether it is for the startup process. Like the previous commits, back-patch to all supported branches. Discussion: https://postgr.es/m/1850884.1599601164@sss.pgh.pa.us
1 parent d0b585e commit 93871b6

File tree

1 file changed

+74
-32
lines changed

1 file changed

+74
-32
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ static void SIGHUP_handler(SIGNAL_ARGS);
410410
static void pmdie(SIGNAL_ARGS);
411411
static void reaper(SIGNAL_ARGS);
412412
static void sigusr1_handler(SIGNAL_ARGS);
413-
static void startup_die(SIGNAL_ARGS);
413+
static void process_startup_packet_die(SIGNAL_ARGS);
414+
static void process_startup_packet_quickdie(SIGNAL_ARGS);
414415
static void dummy_handler(SIGNAL_ARGS);
415416
static void StartupPacketTimeoutHandler(void);
416417
static void CleanupBackend(int pid, int exitstatus);
@@ -4151,22 +4152,30 @@ BackendInitialize(Port *port)
41514152
whereToSendOutput = DestRemote; /* now safe to ereport to client */
41524153

41534154
/*
4154-
* We arrange for a simple exit(1) if we receive SIGTERM or SIGQUIT or
4155-
* timeout while trying to collect the startup packet. Otherwise the
4156-
* postmaster cannot shutdown the database FAST or IMMED cleanly if a
4157-
* buggy client fails to send the packet promptly. XXX it follows that
4158-
* the remainder of this function must tolerate losing control at any
4159-
* instant. Likewise, any pg_on_exit_callback registered before or during
4160-
* this function must be prepared to execute at any instant between here
4161-
* and the end of this function. Furthermore, affected callbacks execute
4162-
* partially or not at all when a second exit-inducing signal arrives
4163-
* after proc_exit_prepare() decrements on_proc_exit_index. (Thanks to
4164-
* that mechanic, callbacks need not anticipate more than one call.) This
4165-
* is fragile; it ought to instead follow the norm of handling interrupts
4166-
* at selected, safe opportunities.
4167-
*/
4168-
pqsignal(SIGTERM, startup_die);
4169-
pqsignal(SIGQUIT, startup_die);
4155+
* We arrange to do proc_exit(1) if we receive SIGTERM or timeout while
4156+
* trying to collect the startup packet; while SIGQUIT results in
4157+
* _exit(2). Otherwise the postmaster cannot shutdown the database FAST
4158+
* or IMMED cleanly if a buggy client fails to send the packet promptly.
4159+
*
4160+
* XXX this is pretty dangerous; signal handlers should not call anything
4161+
* as complex as proc_exit() directly. We minimize the hazard by not
4162+
* keeping these handlers active for longer than we must. However, it
4163+
* seems necessary to be able to escape out of DNS lookups as well as the
4164+
* startup packet reception proper, so we can't narrow the scope further
4165+
* than is done here.
4166+
*
4167+
* XXX it follows that the remainder of this function must tolerate losing
4168+
* control at any instant. Likewise, any pg_on_exit_callback registered
4169+
* before or during this function must be prepared to execute at any
4170+
* instant between here and the end of this function. Furthermore,
4171+
* affected callbacks execute partially or not at all when a second
4172+
* exit-inducing signal arrives after proc_exit_prepare() decrements
4173+
* on_proc_exit_index. (Thanks to that mechanic, callbacks need not
4174+
* anticipate more than one call.) This is fragile; it ought to instead
4175+
* follow the norm of handling interrupts at selected, safe opportunities.
4176+
*/
4177+
pqsignal(SIGTERM, process_startup_packet_die);
4178+
pqsignal(SIGQUIT, process_startup_packet_quickdie);
41704179
InitializeTimeouts(); /* establishes SIGALRM handler */
41714180
PG_SETMASK(&StartupBlockSig);
41724181

@@ -4226,8 +4235,8 @@ BackendInitialize(Port *port)
42264235
port->remote_hostname = strdup(remote_host);
42274236

42284237
/*
4229-
* Ready to begin client interaction. We will give up and exit(1) after a
4230-
* time delay, so that a broken client can't hog a connection
4238+
* Ready to begin client interaction. We will give up and proc_exit(1)
4239+
* after a time delay, so that a broken client can't hog a connection
42314240
* indefinitely. PreAuthDelay and any DNS interactions above don't count
42324241
* against the time limit.
42334242
*
@@ -4249,6 +4258,12 @@ BackendInitialize(Port *port)
42494258
*/
42504259
status = ProcessStartupPacket(port, false);
42514260

4261+
/*
4262+
* Disable the timeout, and prevent SIGTERM/SIGQUIT again.
4263+
*/
4264+
disable_timeout(STARTUP_PACKET_TIMEOUT, false);
4265+
PG_SETMASK(&BlockSig);
4266+
42524267
/*
42534268
* Stop here if it was bad or a cancel packet. ProcessStartupPacket
42544269
* already did any appropriate error reporting.
@@ -4274,12 +4289,6 @@ BackendInitialize(Port *port)
42744289
else
42754290
init_ps_display(port->user_name, port->database_name, remote_ps_data,
42764291
update_process_title ? "authentication" : "");
4277-
4278-
/*
4279-
* Disable the timeout, and prevent SIGTERM/SIGQUIT again.
4280-
*/
4281-
disable_timeout(STARTUP_PACKET_TIMEOUT, false);
4282-
PG_SETMASK(&BlockSig);
42834292
}
42844293

42854294

@@ -5168,20 +5177,49 @@ sigusr1_handler(SIGNAL_ARGS)
51685177
}
51695178

51705179
/*
5171-
* SIGTERM or SIGQUIT while processing startup packet.
5180+
* SIGTERM while processing startup packet.
51725181
* Clean up and exit(1).
51735182
*
5174-
* XXX: possible future improvement: try to send a message indicating
5175-
* why we are disconnecting. Problem is to be sure we don't block while
5176-
* doing so, nor mess up SSL initialization. In practice, if the client
5177-
* has wedged here, it probably couldn't do anything with the message anyway.
5183+
* Running proc_exit() from a signal handler is pretty unsafe, since we
5184+
* can't know what code we've interrupted. But the alternative of using
5185+
* _exit(2) is also unpalatable, since it'd mean that a "fast shutdown"
5186+
* would cause a database crash cycle (forcing WAL replay at restart)
5187+
* if any sessions are in authentication. So we live with it for now.
5188+
*
5189+
* One might be tempted to try to send a message indicating why we are
5190+
* disconnecting. However, that would make this even more unsafe. Also,
5191+
* it seems undesirable to provide clues about the database's state to
5192+
* a client that has not yet completed authentication.
51785193
*/
51795194
static void
5180-
startup_die(SIGNAL_ARGS)
5195+
process_startup_packet_die(SIGNAL_ARGS)
51815196
{
51825197
proc_exit(1);
51835198
}
51845199

5200+
/*
5201+
* SIGQUIT while processing startup packet.
5202+
*
5203+
* Some backend has bought the farm,
5204+
* so we need to stop what we're doing and exit.
5205+
*/
5206+
static void
5207+
process_startup_packet_quickdie(SIGNAL_ARGS)
5208+
{
5209+
/*
5210+
* We DO NOT want to run proc_exit() or atexit() callbacks; they wouldn't
5211+
* be safe to run from a signal handler. Just nail the windows shut and
5212+
* get out of town.
5213+
*
5214+
* Note we do _exit(2) not _exit(1). This is to force the postmaster into
5215+
* a system reset cycle if someone sends a manual SIGQUIT to a random
5216+
* backend. (While it might be safe to do _exit(1), since this session
5217+
* shouldn't have touched shared memory yet, there seems little point in
5218+
* taking any risks.)
5219+
*/
5220+
_exit(2);
5221+
}
5222+
51855223
/*
51865224
* Dummy signal handler
51875225
*
@@ -5198,7 +5236,11 @@ dummy_handler(SIGNAL_ARGS)
51985236

51995237
/*
52005238
* Timeout while processing startup packet.
5201-
* As for startup_die(), we clean up and exit(1).
5239+
* As for process_startup_packet_die(), we clean up and exit(1).
5240+
*
5241+
* This is theoretically just as hazardous as in process_startup_packet_die(),
5242+
* although in practice we're almost certainly waiting for client input,
5243+
* which greatly reduces the risk.
52025244
*/
52035245
static void
52045246
StartupPacketTimeoutHandler(void)

0 commit comments

Comments
 (0)