Skip to content

Commit

Permalink
Fix #242
Browse files Browse the repository at this point in the history
When processing events, AttachCurrentThread is called, but matching
DetachCurrentThread was not being called

This prevented main thread from exiting
  • Loading branch information
bostick committed Aug 4, 2023
1 parent dd0d606 commit dd45f9f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions test/ExitTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit dd45f9f

Please sign in to comment.