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

Commit 4518c79

Browse files
committed
Block signals while allocating DSM memory.
On Linux, we call posix_fallocate() on shm_open()'d memory to avoid later potential SIGBUS (see commit 899bd78). Based on field reports of systems stuck in an EINTR retry loop there, there, we made it possible to break out of that loop via slightly odd coding where the CHECK_FOR_INTERRUPTS() call was somewhat removed from the loop (see commit 422952e). On further reflection, that was not a great choice for at least two reasons: 1. If interrupts were held, the CHECK_FOR_INTERRUPTS() would do nothing and the EINTR error would be surfaced to the user. 2. If EINTR was reported but neither QueryCancelPending nor ProcDiePending was set, then we'd dutifully retry, but with a bit more understanding of how posix_fallocate() works, it's now clear that you can get into a loop that never terminates. posix_fallocate() is not a function that can do some of the job and tell you about progress if it's interrupted, it has to undo what it's done so far and report EINTR, and if signals keep arriving faster than it can complete (cf recovery conflict signals), you're stuck. Therefore, for now, we'll simply block most signals to guarantee progress. SIGQUIT is not blocked (see InitPostmasterChild()), because its expected handler doesn't return, and unblockable signals like SIGCONT are not expected to arrive at a high rate. For good measure, we'll include the ftruncate() call in the blocked region, and add a retry loop. Back-patch to all supported releases. Reported-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Reported-by: Nicola Contu <nicola.contu@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20220701154105.jjfutmngoedgiad3%40alvherre.pgsql
1 parent 82785ef commit 4518c79

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/backend/storage/ipc/dsm_impl.c

+22-13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#endif
6363

6464
#include "common/file_perm.h"
65+
#include "libpq/pqsignal.h" /* for PG_SETMASK macro */
6566
#include "miscadmin.h"
6667
#include "pgstat.h"
6768
#include "portability/mem.h"
@@ -306,14 +307,6 @@ dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size,
306307
shm_unlink(name);
307308
errno = save_errno;
308309

309-
/*
310-
* If we received a query cancel or termination signal, we will have
311-
* EINTR set here. If the caller said that errors are OK here, check
312-
* for interrupts immediately.
313-
*/
314-
if (errno == EINTR && elevel >= ERROR)
315-
CHECK_FOR_INTERRUPTS();
316-
317310
ereport(elevel,
318311
(errcode_for_dynamic_shared_memory(),
319312
errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
@@ -361,9 +354,21 @@ static int
361354
dsm_impl_posix_resize(int fd, off_t size)
362355
{
363356
int rc;
357+
int save_errno;
358+
359+
/*
360+
* Block all blockable signals, except SIGQUIT. posix_fallocate() can run
361+
* for quite a long time, and is an all-or-nothing operation. If we
362+
* allowed SIGUSR1 to interrupt us repeatedly (for example, due to recovery
363+
* conflicts), the retry loop might never succeed.
364+
*/
365+
PG_SETMASK(&BlockSig);
364366

365367
/* Truncate (or extend) the file to the requested size. */
366-
rc = ftruncate(fd, size);
368+
do
369+
{
370+
rc = ftruncate(fd, size);
371+
} while (rc < 0 && errno == EINTR);
367372

368373
/*
369374
* On Linux, a shm_open fd is backed by a tmpfs file. After resizing with
@@ -377,15 +382,15 @@ dsm_impl_posix_resize(int fd, off_t size)
377382
if (rc == 0)
378383
{
379384
/*
380-
* We may get interrupted. If so, just retry unless there is an
381-
* interrupt pending. This avoids the possibility of looping forever
382-
* if another backend is repeatedly trying to interrupt us.
385+
* We still use a traditional EINTR retry loop to handle SIGCONT.
386+
* posix_fallocate() doesn't restart automatically, and we don't want
387+
* this to fail if you attach a debugger.
383388
*/
384389
pgstat_report_wait_start(WAIT_EVENT_DSM_FILL_ZERO_WRITE);
385390
do
386391
{
387392
rc = posix_fallocate(fd, 0, size);
388-
} while (rc == EINTR && !(ProcDiePending || QueryCancelPending));
393+
} while (rc == EINTR);
389394
pgstat_report_wait_end();
390395

391396
/*
@@ -397,6 +402,10 @@ dsm_impl_posix_resize(int fd, off_t size)
397402
}
398403
#endif /* HAVE_POSIX_FALLOCATE && __linux__ */
399404

405+
save_errno = errno;
406+
PG_SETMASK(&UnBlockSig);
407+
errno = save_errno;
408+
400409
return rc;
401410
}
402411

0 commit comments

Comments
 (0)