gradle/native-platform

Native file watcher threads stay attached to the JVM even after watcher stops

Closed this issue · 0 comments

lptr commented

In both the Windows and the macOS file watcher the native thread is attached to the JVM every time an event is received. This has two problems:

  1. we are doing unnecessary work, slowing down event delivery

  2. since we never detach the native thread, the JVM is left alive even after all other threads exit, as the (already stopped) native thread is still represented as a RUNNING JVM thread:

    "DestroyJavaVM@679" prio=5 tid=0x16 nid=NA runnable
      java.lang.Thread.State: RUNNABLE
    
    "Thread-0@677" prio=5 tid=0x15 nid=NA runnable
      java.lang.Thread.State: RUNNABLE
    

Attaching should happen when we start the watcher thread, and detaching should happen when we stop the thread.

https://github.com/adammurdoch/native-platform/blob/79a56f78ea83f3bc0a9e85303814e3c553de15ad/src/main/cpp/apple_fsnotifier.cpp#L40-L51