Skip to content

Null pointer dereference in N_DoMessageLoopWork after CEF shutdown #559

Open

Description

@jakub258

Crash

SIGSEGV at pthread_mutex_lock(0x10) on the AWT-EventQueue-0 thread, during rapid CEF init/close stress testing.

Abridged stack:

libsystem_pthread  pthread_mutex_lock+0x14          ← argument x0 = 0x10
libjcef            LockImpl::Lock()
libjcef            AutoLock::AutoLock(Lock&)
libjcef            ThreadCheckerImpl::EnsureThreadIdAssigned()
libjcef            Context::DoMessageLoopWork()
libjcef            Java_org_cef_CefApp_N_1DoMessageLoopWork
jcef               CefApp$6$1.actionPerformed        ← Swing Timer
java.desktop       javax.swing.Timer.fireActionPerformed

Root cause

Java_org_cef_CefApp_N_1DoMessageLoopWork calls Context::GetInstance()->DoMessageLoopWork() without null-checking GetInstance(). After N_Shutdown() calls Context::Destroy(), which sets g_context = nullptr, a Swing Timer event already queued in the AWT EDT can fire N_DoMessageLoopWork. GetInstance() returns null, the method call dereferences null, and the crash manifests as pthread_mutex_lock(0x10) because ThreadCheckerImpl::lock_ sits at offset 0x10 from the start of the Context object.

To Reproduce
This happened to me only one time when I was stress testing init/close sequence.

Expected behavior
Shutdown should be done without crash.

Versions:

  • OS: MacOS 26.x
  • Java Version: Eclipse Temurin 25.0.3
  • JCEF Version: g708dc14
  • CEF Version: 152.0.6

Fix

Add a null guard before calling DoMessageLoopWork().

Patch (native/CefApp.cpp):

diff --git a/native/CefApp.cpp b/native/CefApp.cpp
index 3adb696..158d0ed 100644
--- a/native/CefApp.cpp
+++ b/native/CefApp.cpp
@@ -45,7 +45,9 @@ JNIEXPORT void JNICALL Java_org_cef_CefApp_N_1Shutdown(JNIEnv* env, jobject) {
 
 JNIEXPORT void JNICALL Java_org_cef_CefApp_N_1DoMessageLoopWork(JNIEnv* env,
                                                                 jobject) {
-  Context::GetInstance()->DoMessageLoopWork();
+  if (Context::GetInstance()) {
+    Context::GetInstance()->DoMessageLoopWork();
+  }
 }

Full patch file: fix_domessageloopwork_null_guard.patch

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug report

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions