From 5a23c7cf3c5eccac6e6de775722bc1136a66be83 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Mon, 5 Jul 2021 17:54:45 +0100 Subject: [PATCH] ocaml: Call caml_shutdown when unloading the plugin This has several useful effects (taken from the OCaml documentation): * Running the functions that were registered with "Stdlib.at_exit". * Triggering finalization of allocated custom blocks. For example, "Stdlib.in_channel" and "Stdlib.out_channel" are represented by custom blocks that enclose file descriptors, which are to be released. * Unloading the dependent shared libraries that were loaded by the runtime, including "dynlink" plugins. * Freeing the memory blocks that were allocated by the runtime with "malloc". If the function is not present (for OCaml < 4.05) then we just skip this step. (cherry picked from commit 99140272a0675b3d123d2c42cb0a5ab73b09fba2) --- configure.ac | 18 ++++++++++++++++++ plugins/ocaml/plugin.c | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/configure.ac b/configure.ac index 9b171b7e..a7c4c8d3 100644 --- a/configure.ac +++ b/configure.ac @@ -857,6 +857,24 @@ EOF rm -f conftest.c conftest.o ]) +dnl Check if OCaml has caml_shutdown (added 2014). +AS_IF([test "x$OCAMLC" != "xno" && test "x$OCAMLFIND" != "xno" && \ + test "x$enable_ocaml" = "xyes"],[ + AC_MSG_CHECKING([for caml_shutdown]) + cat >conftest.c <<'EOF' +#include +int main () { char *p = (void *) caml_shutdown; return 0; } +EOF + AS_IF([$OCAMLC conftest.c >&AS_MESSAGE_LOG_FD 2>&1],[ + AC_MSG_RESULT([yes]) + AC_DEFINE([HAVE_CAML_SHUTDOWN],[1], + [caml_shutdown found at compile time.]) + ],[ + AC_MSG_RESULT([no]) + ]) + rm -f conftest.c conftest.o +]) + dnl For developing plugins in Rust, optional. AC_CHECK_PROG([CARGO],[cargo],[cargo],[no]) AC_ARG_ENABLE([rust], diff --git a/plugins/ocaml/plugin.c b/plugins/ocaml/plugin.c index 00959cb6..9d7d72ad 100644 --- a/plugins/ocaml/plugin.c +++ b/plugins/ocaml/plugin.c @@ -131,6 +131,10 @@ unload_wrapper (void) free ((char *) plugin.config_help); remove_roots (); + +#ifdef HAVE_CAML_SHUTDOWN + caml_shutdown (); +#endif } static void -- 2.31.1