KCEF icon indicating copy to clipboard operation
KCEF copied to clipboard

Native crash with 2025.03.23 on mac

Open sdsantos opened this issue 8 months ago • 5 comments

Initialization code:

class WebViewSetup(
    private val filesDir: String,
    private val cacheDir: String,
    private val backgroundContext: CoroutineContext = Dispatchers.IO
) {

    private val _state = MutableStateFlow<WebViewState>(WebViewState.NotInitialized)
    val state = _state.asStateFlow()

    suspend fun initialize() {
        withContext(backgroundContext) {
            try {
                KCEF.init(builder = {
                    installDir(File(filesDir, "kcef-bundle"))

                    progress {
                        onDownloading {
                            _state.value = WebViewState.Downloading(it.coerceAtLeast(0f))
                        }
                        onInitialized {
                            _state.value = WebViewState.Initialized
                        }
                    }
                    settings {
                        cachePath = cacheDir
                    }
                }, onError = {
                    Logger.e("Error initializing WebView", it)
                    _state.value = WebViewState.Error
                }, onRestartRequired = {
                    _state.value = WebViewState.RestartRequired // Rare
                })
            } catch (e: Exception) {
                Logger.e("Error initializing WebView", e)
                _state.value = WebViewState.Error
            }
        }
    }
}

Gradle:

kotlin {
    jvm("desktop")
    jvmToolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
        vendor.set(JvmVendorSpec.JETBRAINS)
    }

    ...
}

...

compose.desktop {
    application {
        mainClass = "org.ooni.probe.MainKt"

        ...

        jvmArgs("--add-opens", "java.desktop/sun.awt=ALL-UNNAMED")
        jvmArgs("--add-opens", "java.desktop/java.awt.peer=ALL-UNNAMED") // recommended but not necessary

        if (System.getProperty("os.name").contains("Mac")) {
            jvmArgs("--add-opens", "java.desktop/sun.lwawt=ALL-UNNAMED")
            jvmArgs("--add-opens", "java.desktop/sun.lwawt.macosx=ALL-UNNAMED")
        }
    }
}

Logs:

JCEF_I(54:57:144): CefApp: set state NEW
JCEF_I(55:04:809): CefApp: set state INITIALIZING
JCEF_V(55:04:809): Initialize CefApp on Thread[CefInitialize-thread,5,main]
[0402/125504.990503:ERROR:icu_util.cc(178)] icudtl.dat not found in bundle
[0402/125504.993953:ERROR:icu_util.cc(240)] Invalid file descriptor to ICU data received.

Native crash Report:

Process:               java [16320]
Path:                  /Users/USER/Library/Java/JavaVirtualMachines/jbrsdk_jcef-17.0.14/Contents/Home/bin/java
Identifier:            java
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        java [13986]
Responsible:           studio [9028]
User ID:               501

Date/Time:             2025-04-02 12:55:06.0064 +0100
OS Version:            macOS 14.5 (23F79)
Report Version:        12
Anonymous UUID:        02F769A1-4DA8-03AC-CC61-6C780024304C

Time Awake Since Boot: 8800 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BREAKPOINT (SIGTRAP)
Exception Codes:       0x0000000000000001, 0x0000000303d0d9f8

Termination Reason:    Namespace SIGNAL, Code 5 Trace/BPT trap: 5
Terminating Process:   exc handler [16320]

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   Chromium Embedded Framework   	       0x303d0d9f8 _$LT$bool$u20$as$u20$cxx..shared_ptr..SharedPtrTarget$GT$::__get::h6dcb4d1a5991bed5 + 1233636
1   Chromium Embedded Framework   	       0x303420f94 std::process::ExitStatus::success::hb18a44bf284d51af + 18365212
2   Chromium Embedded Framework   	       0x30342000c std::process::ExitStatus::success::hb18a44bf284d51af + 18361236
3   Chromium Embedded Framework   	       0x300324a30 cxxbridge1$rust_vec$u8$set_len + 232176
4   Chromium Embedded Framework   	       0x3003247a0 cxxbridge1$rust_vec$u8$set_len + 231520
5   Chromium Embedded Framework   	       0x3002fbc0c cxxbridge1$rust_vec$u8$set_len + 64716
6   Chromium Embedded Framework   	       0x3002fb968 cxxbridge1$rust_vec$u8$set_len + 64040
7   Chromium Embedded Framework   	       0x300268700 cef_initialize + 312
8   libjcef.dylib                 	       0x11c4706f0 CefInitialize(CefMainArgs const&, CefStructBase<CefSettingsTraits> const&, scoped_refptr<CefApp>, void*) + 184
9   libjcef.dylib                 	       0x11c42d108 +[CefHandler initialize:] + 132
10  Foundation                    	       0x19f23e3f4 __NSThreadPerformPerform + 264
11  CoreFoundation                	       0x19e0ee4d8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
12  CoreFoundation                	       0x19e0ee46c __CFRunLoopDoSource0 + 176
13  CoreFoundation                	       0x19e0ee1dc __CFRunLoopDoSources0 + 244
14  CoreFoundation                	       0x19e0ecdc8 __CFRunLoopRun + 828
15  CoreFoundation                	       0x19e0ec434 CFRunLoopRunSpecific + 608
16  HIToolbox                     	       0x1a889019c RunCurrentEventLoopInMode + 292
17  HIToolbox                     	       0x1a888fe2c ReceiveNextEventCommon + 220
18  HIToolbox                     	       0x1a888fd30 _BlockUntilNextEventMatchingListInModeWithFilter + 76
19  AppKit                        	       0x1a194bd68 _DPSNextEvent + 660
20  AppKit                        	       0x1a2141808 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 700
21  libosxapp.dylib               	       0x1012fab04 -[NSApplicationAWT nextEventMatchingMask:untilDate:inMode:dequeue:] + 204
22  AppKit                        	       0x1a193f09c -[NSApplication run] + 476
23  libosxapp.dylib               	       0x1012fa8e0 +[NSApplicationAWT runAWTLoopWithApp:] + 132
24  libawt_lwawt.dylib            	       0x11c06de98 +[AWTStarter starter:headless:] + 376
25  libosxapp.dylib               	       0x1012fc490 +[ThreadUtilities invokeBlockCopy:] + 28
26  Foundation                    	       0x19f23e3f4 __NSThreadPerformPerform + 264
27  CoreFoundation                	       0x19e0ee4d8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
28  CoreFoundation                	       0x19e0ee46c __CFRunLoopDoSource0 + 176
29  CoreFoundation                	       0x19e0ee1dc __CFRunLoopDoSources0 + 244
30  CoreFoundation                	       0x19e0ecdc8 __CFRunLoopRun + 828
31  CoreFoundation                	       0x19e0ec434 CFRunLoopRunSpecific + 608
32  libjli.dylib                  	       0x1013e1184 CreateExecutionEnvironment + 404
33  libjli.dylib                  	       0x1013dd1b4 JLI_Launch + 1232
34  java                          	       0x100f4fba4 main + 408
35  dyld                          	       0x19dc860e0 start + 2360

sdsantos avatar Apr 02 '25 11:04 sdsantos

After long debugging, the only reason I can find is at https://github.com/DatL4g/KCEF/blob/0665269b7d6a91b0ee187f4432bb5be5ca41a112/kcef/src/main/kotlin/dev/datlag/kcef/step/init/CefInitializer.kt#L68 as this dylib doesn't exist. I can't find the exact moment when it's crashing, but this is the closest I got to the crash.

Alex-Bond avatar Apr 08 '25 06:04 Alex-Bond

@Alex-Bond we have logs from CefApp up until: https://github.com/DatL4g/jcef/blob/e6184bb3c97b57facd29dbabf5df56d562132779/java/org/cef/CefApp.java#L576-L578

sdsantos avatar Apr 08 '25 14:04 sdsantos

Good investigation so far @sdsantos and @Alex-Bond! Unfortunately I don't have much time to invest in this project at the moment.

However it seems like the cause of this issue is that JetBrains changed something on the packaging and there is no libcef.dylib anymore, but a libjcef.dylib.

Have anyone of you tried packaging your application with the JetBrains Runtime JDK?
And check if that works, as you both seem to load the binaries from the runtime downloaded package.
When packaged with the JDK it tries to resolve the packages automatically and no downloading is required: https://github.com/DatL4g/KCEF/blob/0665269b7d6a91b0ee187f4432bb5be5ca41a112/kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt#L305

Additionally you can try and test around if you load the packages yourself (or maybe copy and edit my initializer https://github.com/DatL4g/KCEF/blob/master/kcef/src/main/kotlin/dev/datlag/kcef/step/init/CefInitializer.kt) and then mark KCEF as initialized: https://github.com/DatL4g/KCEF/blob/0665269b7d6a91b0ee187f4432bb5be5ca41a112/kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt#L149-L161

DatL4g avatar Apr 08 '25 14:04 DatL4g

I was running the app with gradlew run and it was skipping the download fine, so I assume it was bundled correctly. But if I run gradlew runDistributable it starts downloading... 🤔

I tried loading and skipping the init with something like this:

try {
    System.loadLibrary("libcef")
} catch (e: UnsatisfiedLinkError) {
    try {
        System.loadLibrary("cef")
    } catch (e: UnsatisfiedLinkError) {
        try {
            System.loadLibrary("jcef")
        } catch (e: UnsatisfiedLinkError) {
            System.loadLibrary("libjcef")
        }
    }
}
KCEF.init(CefApp.getInstance())

But then once I try loading the WebView, it crashes:

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               java [11752]
Path:                  /Users/USER/Library/Java/JavaVirtualMachines/jbrsdk_jcef-17.0.14/Contents/Home/bin/java
Identifier:            java
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        java [3814]
Responsible:           studio [633]
User ID:               501

Date/Time:             2025-04-08 16:52:49.3997 +0100
OS Version:            macOS 14.5 (23F79)
Report Version:        12
Anonymous UUID:        02F769A1-4DA8-03AC-CC61-6C780024304C

Time Awake Since Boot: 23000 seconds

System Integrity Protection: enabled

Crashed Thread:        29  Java: AWT-EventQueue-0

Exception Type:        EXC_BAD_ACCESS (SIGABRT)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00000000aca337f8
Exception Codes:       0x0000000000000001, 0x00000000aca337f8

Termination Reason:    Namespace SIGNAL, Code 6 Abort trap: 6
Terminating Process:   java [11752]

VM Region Info: 0xaca337f8 is not in any region.  Bytes before following region: 1406273544
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      100754000-100758000    [   16K] r-x/r-x SM=COW  /Users/USER/Library/Java/JavaVirtualMachines/jbrsdk_jcef-17.0.14/Contents/Home/bin/java

Application Specific Information:
abort() called

Thread 0::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	       0x19d0ca1f4 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x19d0dcb24 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x19d0d2e34 mach_msg_overwrite + 476
3   libsystem_kernel.dylib        	       0x19d0ca578 mach_msg + 24
4   CoreFoundation                	       0x19d1ea680 __CFRunLoopServiceMachPort + 160
5   CoreFoundation                	       0x19d1e8f44 __CFRunLoopRun + 1208
6   CoreFoundation                	       0x19d1e8434 CFRunLoopRunSpecific + 608
7   HIToolbox                     	       0x1a798c19c RunCurrentEventLoopInMode + 292
8   HIToolbox                     	       0x1a798bfd8 ReceiveNextEventCommon + 648
9   HIToolbox                     	       0x1a798bd30 _BlockUntilNextEventMatchingListInModeWithFilter + 76
10  AppKit                        	       0x1a0a47d68 _DPSNextEvent + 660
11  AppKit                        	       0x1a123d808 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 700
12  libosxapp.dylib               	       0x100b02b04 -[NSApplicationAWT nextEventMatchingMask:untilDate:inMode:dequeue:] + 204
13  AppKit                        	       0x1a0a3b09c -[NSApplication run] + 476
14  libosxapp.dylib               	       0x100b028e0 +[NSApplicationAWT runAWTLoopWithApp:] + 132
15  libawt_lwawt.dylib            	       0x123c25e98 +[AWTStarter starter:headless:] + 376
16  libosxapp.dylib               	       0x100b04490 +[ThreadUtilities invokeBlockCopy:] + 28
17  Foundation                    	       0x19e33a3f4 __NSThreadPerformPerform + 264
18  CoreFoundation                	       0x19d1ea4d8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
19  CoreFoundation                	       0x19d1ea46c __CFRunLoopDoSource0 + 176
20  CoreFoundation                	       0x19d1ea1dc __CFRunLoopDoSources0 + 244
21  CoreFoundation                	       0x19d1e8dc8 __CFRunLoopRun + 828
22  CoreFoundation                	       0x19d1e8434 CFRunLoopRunSpecific + 608
23  libjli.dylib                  	       0x100be9184 CreateExecutionEnvironment + 404
24  libjli.dylib                  	       0x100be51b4 JLI_Launch + 1232
25  java                          	       0x100757ba4 main + 408
26  dyld                          	       0x19cd820e0 start + 2360

sdsantos avatar Apr 08 '25 15:04 sdsantos

I pretty much managed to get to the same point as @sdsantos where I bypassed the downloading and using the bundled version of jcef, but when trying to launch WebView, the application crashed. Has anyone managed a workaround yet?

designingEmergence avatar Jun 17 '25 14:06 designingEmergence