{"record":{"id":"0beef42941224b31","repo":"java-native-access/jna","slug":"jna-could-not-detach-thread-on-initial-load","errorCode":null,"errorMessage":"JNA: could not detach thread on initial load\n","messagePattern":"JNA: could not detach thread on initial load\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"native/dispatch.c","lineNumber":3373,"sourceCode":"\n  if (!attached) {\n    if ((*jvm)->AttachCurrentThread(jvm, (void *)&env, NULL) != JNI_OK) {\n      fprintf(stderr, \"JNA: Can't attach native thread to VM on load\\n\");\n      return 0;\n    }\n  }\n\n  if ((err = JNA_init(env)) != NULL) {\n    fprintf(stderr, \"JNA: Problems loading core IDs: %s\\n\", err);\n    result = 0;\n  }\n  else if ((err = JNA_callback_init(env)) != NULL) {\n    fprintf(stderr, \"JNA: Problems loading callback IDs: %s\\n\", err);\n    result = 0;\n  }\n  if (!attached) {\n    if ((*jvm)->DetachCurrentThread(jvm) != 0) {\n      fprintf(stderr, \"JNA: could not detach thread on initial load\\n\");\n    }\n  }\n\n  return result;\n}\n\nJNIEXPORT void JNICALL\nJNI_OnUnload(JavaVM *vm, void *UNUSED(reserved)) {\n  jobject* refs[] = {\n    &classObject, &classClass, &classMethod,\n    &classString,\n#ifndef NO_NIO_BUFFERS\n    &classBuffer, &classByteBuffer, &classCharBuffer,\n    &classShortBuffer, &classIntBuffer, &classLongBuffer,\n    &classFloatBuffer, &classDoubleBuffer,\n#endif /* NO_NIO_BUFFERS */\n    &classVoid, &classPrimitiveVoid,\n    &classBoolean, &classPrimitiveBoolean,","sourceCodeStart":3355,"sourceCodeEnd":3391,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/native/dispatch.c#L3355-L3391","documentation":"At the end of JNA native library load, if the loading thread was attached by JNA itself, it attempts DetachCurrentThread to restore the original state. This message is printed to stderr when DetachCurrentThread returns non-zero. Initialization itself has already completed (success or failure is conveyed by the return value); the symptom is the loading thread remaining attached to the VM.","triggerScenarios":"JNA native library loads on a non-Java thread; attach succeeded, JNA_init/JNA_callback_init ran, and then DetachCurrentThread failed — commonly because the VM is concurrently shutting down or the thread still holds JNI references from initialization.","commonSituations":"Native plugin systems loading JNA from their own threads; application servers shutting down while a late JNA load is in progress; repeated load/unload cycles accumulating attached threads when detach keeps failing.","solutions":["Load/initialize JNA from a Java-managed thread (e.g. in a static initializer or main-thread startup code) so JNA never needs to attach/detach.","Ensure JVM shutdown does not overlap with JNA library loading; complete library init before triggering shutdown sequences.","If failures repeat, monitor attached-thread counts and restart the JVM; otherwise treat as a benign stderr warning.","Confirm no other native code on that thread holds references preventing detachment, and release them before the load returns."],"exampleFix":"// before: load JNA from a native-owned thread\nnative_host_run(start_app); // app's first JNA touch happens on C thread\n// after: force early JNA init on the main Java thread\npublic static void main(String[] args) {\n  Native.getNativeLibrary(\"jnisample\"); // warms up JNA on a JVM thread\n  native_host_run(start_app);\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Perform first JNA use on a Java-launched thread so attach/detach is skipped.","Sequence library loading before any shutdown activity begins.","Log stderr from native load in CI to catch recurring detach failures."],"tags":["jni","jna","native","initialization","threads","detach"],"backgroundTag":"jni-thread-detach-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}