Skip to content

Commit

Permalink
Merge pull request #922 from darshan-hpc/snyder/fix-fork-handler-crash
Browse files Browse the repository at this point in the history
BUG: fix potential segfault with apps that `fork()` and use Darshan app exclusions
  • Loading branch information
shanedsnyder authored Apr 14, 2023
2 parents 05efd1d + c3605f7 commit 59970a5
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions darshan-runtime/lib/darshan-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,6 @@ void darshan_core_initialize(int argc, char **argv)
/* set PID that initialized Darshan runtime */
init_core->pid = getpid();

/* setup fork handlers if not using MPI */
if(!using_mpi && !orig_parent_pid)
{
pthread_atfork(NULL, NULL, &darshan_core_fork_child_cb);
}

/* parse any user-supplied runtime configuration of Darshan */
/* NOTE: as the ordering implies, environment variables override any
* config file parameters
Expand Down Expand Up @@ -352,6 +346,12 @@ void darshan_core_initialize(int argc, char **argv)
init_core->config.mod_disabled = ~(init_core->config.mod_disabled & 0);
}

/* setup fork handlers if not using MPI */
if(!using_mpi && !orig_parent_pid)
{
pthread_atfork(NULL, NULL, &darshan_core_fork_child_cb);
}

/* if darshan was successfully initialized, set the global pointer
* and record absolute start time so that we can later generate
* relative times with this as a reference point.
Expand Down Expand Up @@ -2182,16 +2182,19 @@ static void darshan_core_cleanup(struct darshan_core_runtime* core)

static void darshan_core_fork_child_cb(void)
{
/* hold onto the original parent PID, which we will use as jobid if the user didn't
* provide a jobid env variable
*/
parent_pid = __darshan_core->pid;
if(!orig_parent_pid)
orig_parent_pid = parent_pid;
if(__darshan_core)
{
/* hold onto the original parent PID, which we will use as jobid if the user didn't
* provide a jobid env variable
*/
parent_pid = __darshan_core->pid;
if(!orig_parent_pid)
orig_parent_pid = parent_pid;

/* shutdown and re-init darshan, making sure to not write out a log file */
darshan_core_shutdown(0);
darshan_core_initialize(0, NULL);
/* shutdown and re-init darshan, making sure to not write out a log file */
darshan_core_shutdown(0);
darshan_core_initialize(0, NULL);
}

return;
}
Expand Down

0 comments on commit 59970a5

Please sign in to comment.