Hi Jerry,

thanks for taking over the coarray stuff.

About unlinking the shared segment: Keeping the file existing and usable after
unlink is a Linux special. Other OS do not support this (when I am not mistaken
all of the BSD family removes the file). I fell for that, so you might want to
branch OS dependent here.

Keep on the work,
        Andre

On Tue, 13 Oct 2026 12:09:59 -0700
Jerry DeLisle <[email protected]> wrote:

> See attached patch. The supervisor created the shared memory object
> with shm_open and unlinked it only on a normal exit, so when it was
> stopped by a signal (a testsuite timeout, ^C) the object stayed in
> /dev/shm. The fix passes the open descriptor to the images in
> GFORTRAN_SHMEM_FD and unlinks the name right after the object is sized,
> so the kernel frees it when the last process closes it, however the
> program ends.
> 
> All of it is in shared_memory_init and shared_memory_cleanup in
> shared_memory.c. In the supervisor branch, clearing FD_CLOEXEC keeps
> the descriptor across the execve of the images; if that fails the
> name is kept and the old shm_open path is used. The image branch takes
> the descriptor from the environment. MADV_DONTDUMP is needed because
> the kernel dumps an unlinked object like anonymous memory: without it
> an image that aborts writes the whole pool into its core file.
> shared_memory_cleanup skips shm_unlink when the name is already gone.
> The code is under HAVE_SETENV, so other targets are unchanged.
> 
> There is no test, since it needs a signal sent to the program. By
> hand: a program that never ends, run under "timeout 2", leaves no
> /dev/shm/gfor-shm-* behind. A signal sent to the supervisor's pid alone
> still leaves the images running, because the supervisor has no signal
> handler; that is a separate, older problem, and with this patch the
> object goes away once those images are killed.
> 
> Regression tested on x86_64.
> 
> OK for mainline?
> 
> Regards,
> 
> Jerry
> 
> ---
> 
> libgfortran: [PR127753] caf_shmem: do not leak the shared memory object
> 
> When the supervisor was stopped by a signal, for example by a timeout or
> ^C, it exited without unlinking the shared memory object, so the object
> was left behind in /dev/shm.
> 
> The supervisor now passes the descriptor of the object to the images and
> removes its name right after creating it, so that the object goes away
> with the last process however the program ends.  The mapping is excluded
> from core dumps, because the kernel dumps an unlinked object like
> anonymous memory, which would write out the whole pool.
> 
> Assisted-by: Claude Opus 5.5
> 
>       PR libfortran/127753
> 
> libgfortran/ChangeLog:
> 
>       * caf/shmem/shared_memory.c (ENV_FD): New.
>       (shared_memory_init): Pass the descriptor to the images through
>       ENV_FD and unlink the object.  Use the inherited descriptor in an
>       image.  Exclude the mapping from core dumps.
>       (shared_memory_cleanup): Do not unlink an unlinked object.


-- 
Andre Vehreschild * Email: vehre ad gmx dot de 

Reply via email to