diff --git a/core/src/main/cpp/defer.cpp b/core/src/main/cpp/defer.cpp index 6c64580..b406716 100644 --- a/core/src/main/cpp/defer.cpp +++ b/core/src/main/cpp/defer.cpp @@ -7,6 +7,8 @@ static std::pair completableFutureWithToken(Master::Context * jobject completableFuture = context->newGlobalReference(context->newCompletableFuture()); EventQueue::getInstance()->registerHandler(COMPLETE, token, [completableFuture](const event_t *event) { + EventQueue::getInstance()->unregisterHandler(COMPLETE, event->token); + Master::runWithAttached([&](JNIEnv *env) -> int { Master::runWithContext(env, [&](Master::Context *context) { if ( strlen(event->payload) == 0 ) { @@ -20,8 +22,6 @@ static std::pair completableFutureWithToken(Master::Context * return 0; }); - - EventQueue::getInstance()->unregisterHandler(COMPLETE, event->token); }); return {completableFuture, token}; diff --git a/core/src/main/cpp/tun.cpp b/core/src/main/cpp/tun.cpp index 2c08bb3..c7b9a70 100644 --- a/core/src/main/cpp/tun.cpp +++ b/core/src/main/cpp/tun.cpp @@ -29,6 +29,11 @@ Java_com_github_kr328_clash_core_bridge_Bridge_startTunDevice(JNIEnv *env, jclas }); EventQueue::getInstance()->registerHandler(TUN_STOP, token, [callbackGlobal](const event_t *e) { + auto queue = EventQueue::getInstance(); + + queue->unregisterHandler(NEW_SOCKET, e->token); + queue->unregisterHandler(TUN_STOP, e->token); + Master::runWithAttached([&](JNIEnv *env) -> int { Master::runWithContext(env, [&](Master::Context *context) { context->tunCallbackStop(callbackGlobal); @@ -37,11 +42,6 @@ Java_com_github_kr328_clash_core_bridge_Bridge_startTunDevice(JNIEnv *env, jclas return 0; }); - - auto queue = EventQueue::getInstance(); - - queue->unregisterHandler(NEW_SOCKET, e->token); - queue->unregisterHandler(TUN_STOP, e->token); }); char *exception = startTunDevice(fd, mtu, gatewayString, mirrorString, dnsString, token);