Skip to content

Commit 63efab4

Browse files
committed
Allow DSM allocation to be interrupted.
Chris Travers reported that the startup process can repeatedly try to cancel a backend that is in a posix_fallocate()/EINTR loop and cause it to loop forever. Teach the retry loop to give up if an interrupt is pending. Don't actually check for interrupts in that loop though, because a non-local exit would skip some clean-up code in the caller. Back-patch to 9.4 where DSM was added (and posix_fallocate() was later back-patched). Author: Chris Travers Reviewed-by: Ildar Musin, Murat Kabilov, Oleksii Kliukin Tested-by: Oleksii Kliukin Discussion: https://postgr.es/m/CAN-RpxB-oeZve_J3SM_6%3DHXPmvEG%3DHX%2B9V9pi8g2YR7YW0rBBg%40mail.gmail.com
1 parent 3d65e40 commit 63efab4

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/backend/storage/ipc/dsm_impl.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
*/
4848

4949
#include "postgres.h"
50+
#include "miscadmin.h"
5051

5152
#include <fcntl.h>
5253
#include <unistd.h>
@@ -333,6 +334,14 @@ dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size,
333334
shm_unlink(name);
334335
errno = save_errno;
335336

337+
/*
338+
* If we received a query cancel or termination signal, we will have
339+
* EINTR set here. If the caller said that errors are OK here, check
340+
* for interrupts immediately.
341+
*/
342+
if (errno == EINTR && elevel >= ERROR)
343+
CHECK_FOR_INTERRUPTS();
344+
336345
ereport(elevel,
337346
(errcode_for_dynamic_shared_memory(),
338347
errmsg("could not resize shared memory segment \"%s\" to %zu bytes: %m",
@@ -422,11 +431,15 @@ dsm_impl_posix_resize(int fd, off_t size)
422431
#if defined(HAVE_POSIX_FALLOCATE) && defined(__linux__)
423432
if (rc == 0)
424433
{
425-
/* We may get interrupted, if so just retry. */
434+
/*
435+
* We may get interrupted. If so, just retry unless there is an
436+
* interrupt pending. This avoids the possibility of looping forever
437+
* if another backend is repeatedly trying to interrupt us.
438+
*/
426439
do
427440
{
428441
rc = posix_fallocate(fd, 0, size);
429-
} while (rc == EINTR);
442+
} while (rc == EINTR && !(ProcDiePending || QueryCancelPending));
430443

431444
/*
432445
* The caller expects errno to be set, but posix_fallocate() doesn't

0 commit comments

Comments
 (0)