From dd45f9fb45c491b65e93743ae48788845ff2fc5e Mon Sep 17 00:00:00 2001 From: Brenton Bostick Date: Fri, 4 Aug 2023 10:27:50 -0400 Subject: [PATCH] Fix https://github.com/zerotier/libzt/issues/242 When processing events, AttachCurrentThread is called, but matching DetachCurrentThread was not being called This prevented main thread from exiting --- src/Events.cpp | 2 ++ test/ExitTest.java | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 test/ExitTest.java diff --git a/src/Events.cpp b/src/Events.cpp index dd62f3e4..57cdc20a 100644 --- a/src/Events.cpp +++ b/src/Events.cpp @@ -197,6 +197,8 @@ void Events::sendToUser(zts_event_msg_t* msg) id = msg->peer ? msg->peer->peer_id : 0; } env->CallVoidMethod(javaCbObjRef, javaCbMethodId, id, msg->event_code); + + jvm->DetachCurrentThread(); } #endif // ZTS_ENABLE_JAVA #ifdef ZTS_ENABLE_PINVOKE diff --git a/test/ExitTest.java b/test/ExitTest.java new file mode 100644 index 00000000..be5b28e1 --- /dev/null +++ b/test/ExitTest.java @@ -0,0 +1,77 @@ +import com.zerotier.sockets.*; + +// +// Test for: +// Java process does not exit after finishing libzt work +// https://github.com/zerotier/libzt/issues/242 +// +// Run and ensure that process exits +// +public class ExitTest { + + public static void main(String[] args) throws InterruptedException { + + long networkId = Long.parseUnsignedLong("ebe7fbd445e76ac6", 16); + String storagePath = "exittest_storage"; + + ZeroTierNode node = new ZeroTierNode(); + node.initFromStorage(storagePath); + node.initSetEventHandler(new ZeroTierEventListener() { + @Override + public void onZeroTierEvent(long l, int eventCode) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_UP) { + System.err.println("ZTS_EVENT_NODE_UP"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) { + System.err.println("ZTS_EVENT_NODE_ONLINE"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) { + System.err.println("ZTS_EVENT_NODE_OFFLINE"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_DOWN) { + System.err.println("ZTS_EVENT_NODE_DOWN"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) { + System.err.println("ZTS_EVENT_NETWORK_READY_IP4"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) { + System.err.println("ZTS_EVENT_NETWORK_READY_IP6"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) { + System.err.println("ZTS_EVENT_NETWORK_DOWN"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_OK) { + System.err.println("ZTS_EVENT_NETWORK_OK"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) { + System.err.println("ZTS_EVENT_NETWORK_ACCESS_DENIED"); + } + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) { + System.err.println("ZTS_EVENT_NETWORK_NOT_FOUND"); + } + } + }); + + System.err.println("start"); + node.start(); + + System.err.println("delay until online"); + while (!node.isOnline()) { + ZeroTierNative.zts_util_delay(50); + } + System.err.println("done delaying"); + + System.out.println("join"); + node.join(networkId); + + System.out.println("delaying until transport ready"); + while (! node.isNetworkTransportReady(networkId)) { + ZeroTierNative.zts_util_delay(50); + } + + System.err.println("stop"); + node.stop(); + + System.err.println("exiting"); + } +}