Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Let the exception flow.
[simgrid.git] / src / bindings / java / jmsg_task.cpp
index 14764bd..ede08bb 100644 (file)
@@ -5,6 +5,7 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "simgrid/Exception.hpp"
 #include "simgrid/s4u/Host.hpp"
 #include "src/kernel/context/Context.hpp"
 
@@ -127,9 +128,9 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject j
     return;
   }
   msg_error_t rv;
-  simgrid::kernel::context::try_n_catch_stoprequest(
-      [&rv, &task]() { rv = MSG_task_execute(task); },
-      [&env] { jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed"); });
+  if (not simgrid::ForcefulKillException::try_n_catch([&rv, &task]() { rv = MSG_task_execute(task); })) {
+    jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
+  }
 
   if (env->ExceptionOccurred())
     return;
@@ -286,11 +287,10 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass
 
   const char *alias = env->GetStringUTFChars(jalias, 0);
   msg_error_t rv;
-  simgrid::kernel::context::try_n_catch_stoprequest(
-      [&rv, &task, &alias, &jtimeout]() {
-        rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr);
-      },
-      [&env]() { jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed"); });
+  if (not simgrid::ForcefulKillException::try_n_catch(
+          [&rv, &task, &alias, &jtimeout]() { rv = MSG_task_receive_with_timeout(&task, alias, (double)jtimeout); })) {
+    jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
+  }
   env->ReleaseStringUTFChars(jalias, alias);
   if (env->ExceptionOccurred())
     return nullptr;
@@ -470,11 +470,16 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeFinalize(JNIEnv * env, jo
 
 static void msg_task_cancel_on_failed_dsend(void*t) {
   msg_task_t task = (msg_task_t) t;
-  JNIEnv *env =get_current_thread_env();
-  jobject jtask_global = (jobject) MSG_task_get_data(task);
-
-  /* Destroy the global ref so that the JVM can free the stuff */
-  env->DeleteGlobalRef(jtask_global);
+  JNIEnv* env     = get_current_thread_env();
+  if (env) {
+    jobject jtask_global = (jobject)MSG_task_get_data(task);
+    /* Destroy the global ref so that the JVM can free the stuff */
+    env->DeleteGlobalRef(jtask_global);
+    /* Don't free the C data here, to avoid a race condition with the GC also sometimes doing so.
+     * A rare memleak is seen as preferable to a rare "free(): invalid pointer" failure that
+     * proves really hard to debug.
+     */
+  }
   MSG_task_set_data(task, nullptr);
 }