mercury-hpc / mercury

Mercury is a C library for implementing RPC, optimized for HPC.
http://www.mcs.anl.gov/projects/mercury/
BSD 3-Clause "New" or "Revised" License
168 stars 62 forks source link

Mercury leaking memory after a fork() #705

Closed mdorier closed 5 months ago

mdorier commented 1 year ago

Describe the bug

I have a program that does a fork() after having initialized an hg_class and an hg_context (in the real scenario the child immediately destroys the class and context, before creating new ones that it can start using). When the parent destroys its class and context and terminates, with address sanitizer enabled, I see that it is leaking a bunch of things out of Mercury functions.

To Reproduce

Here is an example code, to compile with CFLAGS="-fsanitize=address -fno-omit-frame-pointer" and LDFLAGS="-fsanitize=address -fno-omit-frame-pointer":

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <mercury.h>
#include <sys/wait.h>

int main(int argc, char** argv) {
      hg_class_t* hg_class = HG_Init("na+sm", false);
      hg_context_t* hg_context = HG_Context_create(hg_class);
      pid_t pid = fork();
      if(pid != 0) {
          waitpid(pid, NULL, 0);
      }
      HG_Context_destroy(hg_context);
      HG_Finalize(hg_class);
      return 0;
}

If you run with ASAN_OPTIONS="log_path=asan.log" you will get an asan.log.XXXX file where XXXX is the pid of the parent (the child doesn't seem to be leaking memory, but if you want to confirm that, simply add an else clause for the child and make it segfault, you will end up with another asan.log.YYYY for it).

Expected behavior

I would expect Mercury not to leak memory.

Platform (please complete the following information):

mdorier commented 1 year ago

Actually the process that leaks memory is whoever finalizes second. Here is an example where the child is the one finalizing second:

if(pid != 0) {
      HG_Context_destroy(hg_context);
      HG_Finalize(hg_class);
      waitpid(pid, NULL, 0);
} else {
      sleep(1);
      HG_Context_destroy(hg_context);
      HG_Finalize(hg_class);
}
soumagne commented 1 year ago

I don't see anything being generated from asan on my machine... and I compiled mercury with CMAKE_BUILD_TYPE=Asan

soumagne commented 5 months ago

Closing for now, please re-open if you see any more issues.