J2V8 error: Invalid V8 thread access

博主使用j2v8在Java中执行JavaScript代码时遇到错误。经跟踪发现,是比对V8Runtime创建时的线程与当前执行线程不一致导致。V8Locker提供release和acquire方法,解决方案是在创建v8Runtime的线程中释放锁定线程,在使用v8的线程中将当前线程绑定到v8Locker,且二者缺一不可。

今天使用j2v8在java中执行JavaScript代码时,遇到了一个错误:

Exception in thread "Thread-2" java.lang.Error: Invalid V8 thread access
	at com.eclipsesource.v8.V8Locker.checkThread(V8Locker.java:56)
	at com.eclipsesource.v8.V8.checkThread(V8.java:676)

一个简单的报错示例:

    @Test
    public void test() throws InterruptedException {
        AtomicReference<V8> v8 = new AtomicReference<>();
        new Thread(() -> {
            v8.set(V8.createV8Runtime());
        }).start();
        Thread.sleep(5000);
        v8.get().executeScript("const i = 0;"); // 此处报错
        v8.get().release();
    }

跟踪checkThread方法:

    public void checkThread() {
        if (this.thread != Thread.currentThread()) {
            throw new Error("Invalid V8 thread access");
        }
    }

发现在比对 this.threadThread.currentThread 是否一致,Thread.currentThread 是当前正在执行的线程,而 this.thread 则是V8Runtime创建时的线程,当两个线程不一致时就会抛出这个错误,而 this.thread 存在于V8Locker,v8locker提供两个跟 this.thread 相关的方法,release和acquire:

    public synchronized void acquire() {
        if (this.thread != null && this.thread != Thread.currentThread()) {
            throw new Error("Invalid V8 thread access");
        } else {
            this.thread = Thread.currentThread();
        }
    }

    public synchronized void release() {
        this.checkThread();
        this.thread = null;
    }

所以可以得出解决方案,在创建v8Runtime的线程中释放v8Locker中锁定的线程,然后在使用v8的线程中将当前线程绑定到v8Locker中。

上边报错示例的解决方案:

    @Test
    public void test() throws InterruptedException {
        AtomicReference<V8> v8 = new AtomicReference<>();
        new Thread(() -> {
            V8 v=V8.createV8Runtime();
            v.getLocker().release(); // 在创建runtime的线程中release
            v8.set(v);
        }).start();
        Thread.sleep(5000);
        v8.get().getLocker().acquire();// 在使用runtime的线程中acquire
        v8.get().executeScript("const i = 0;");
        v8.get().release();
    }

注:release和acquire二者缺一不可。

级别 时间 进程 信息 错误 2025-11-8 1:00 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 1:00 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 1:00 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 1:37 kernel sd 0:0:0:0: [sda] Asking for cache data failed 错误 2025-11-8 1:37 kernel sd 0:0:0:0: [sda] Assuming drive cache: write through 错误 2025-11-8 1:37 kernel sd 1:0:0:0: [sdb] Asking for cache data failed 错误 2025-11-8 1:37 kernel sd 1:0:0:0: [sdb] Assuming drive cache: write through 错误 2025-11-8 1:37 kernel sd 2:0:0:0: [sdc] Asking for cache data failed 错误 2025-11-8 1:37 kernel sd 2:0:0:0: [sdc] Assuming drive cache: write through 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 1:37 kernel parport_pc parport_pc.956: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 1:37 kernel parport_pc parport_pc.888: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 1:37 kernel parport_pc parport_pc.632: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 1:37 kernel ipmi_si IPI0001:00: IRQ index 0 not found 错误 2025-11-8 1:37 kernel ipmi_si IPI0001:00: Interface detection failed 错误 2025-11-8 1:37 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 1:37 kernel Bluetooth: hci0: RTL: firmware file rtl_bt/rtl8761bu_fw not found 错误 2025-11-8 1:37 systemd[1] Failed to mount /mnt/24136ca5-0ec5-4b34-b2de-885a454f49e8. 错误 2025-11-8 1:37 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 1:37 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 1:37 systemd[1] Failed to mount /mnt/a10ad006-a33d-43ee-bd54-4152effbe297. 错误 2025-11-8 1:37 dbus-daemon[1969] [system] [limitCtl]:limit config org.ukui.GreeterDaemon.limit is corrupted, whitelist invalid. Update or reinstall corresponding software package! 错误 2025-11-8 1:37 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 1:37 kernel XFS (sda17): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 1:37 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 1:37 kernel EXT4-fs (sdc2): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 1:37 secRiskBox[5093] connect(signal_create):state:1 错误 2025-11-8 1:37 secRiskBox[5093] connect(signal_show):state:1 错误 2025-11-8 1:45 kernel Bluetooth: hci0: RTL: firmware file rtl_bt/rtl8761bu_fw not found 错误 2025-11-8 1:51 systemd-coredump[20374] Process 9037 (lbrowser) of user 1000 dumped core. 错误 2025-11-8 1:51 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 1:58 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 2:01 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 2:04 systemd-coredump[41188] Process 41172 (CIndexMgr) of user 1000 dumped core. 错误 2025-11-8 2:09 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 2:09 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 2:11 lightdm[46291] pam_systemd(lightdm-autologin:session): Failed to create session: Access denied 错误 2025-11-8 2:11 kysec-fixuserkid[46296] func:_set_priv_dir_kid_with_skel, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:11 kysec-fixuserkid[46296] func:_set_priv_dir_kid_with_skel, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:_set_priv_dir_kid_with_skel, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:_set_priv_dir_kid_with_skel, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [_set_priv_dir_kid_with_skel] failed to update kid(1) to table: No such file or directory 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [set_priv_dir_kid_with_xdg] failed to update kid(1) to table: No such file or directory 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [set_priv_dir_kid_with_xdg] failed to update kid(2) to table: No such file or directory 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [set_priv_dir_kid_with_xdg] failed to update kid(3) to table: No such file or directory 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [set_priv_dir_kid_with_xdg] failed to update kid(4) to table: No such file or directory 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 2 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, ret val::: ret1: -4, ret2: -1, ret3: -1 错误 2025-11-8 2:12 kysec-fixuserkid[46296] func:set_priv_dir_kid_with_xdg, retry count::: retry 3 错误 2025-11-8 2:12 kysec-fixuserkid[46296] [set_priv_dir_kid_with_xdg] failed to update kid(5) to table: No such file or directory 错误 2025-11-8 2:20 systemd-coredump[52944] Process 46598 (iflyime-qim) of user 1000 dumped core. 错误 2025-11-8 2:20 dbus-daemon[1969] [system] [limitCtl]在reaklink处报错了,退出,No such file or directory 错误 2025-11-8 2:20 systemd[3662] Failed to start System upgrade background detection program. 错误 2025-11-8 2:20 systemd[1] Failed unmounting /root.-- Reboot -- 错误 2025-11-8 9:29 kernel sd 0:0:0:0: [sda] Asking for cache data failed 错误 2025-11-8 9:29 kernel sd 0:0:0:0: [sda] Assuming drive cache: write through 错误 2025-11-8 9:29 kernel sd 1:0:0:0: [sdb] Asking for cache data failed 错误 2025-11-8 9:29 kernel sd 1:0:0:0: [sdb] Assuming drive cache: write through 错误 2025-11-8 9:29 kernel sd 2:0:0:0: [sdc] Asking for cache data failed 错误 2025-11-8 9:29 kernel sd 2:0:0:0: [sdc] Assuming drive cache: write through 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:29 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:29 kernel parport_pc parport_pc.956: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:29 kernel parport_pc parport_pc.888: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:29 kernel parport_pc parport_pc.632: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:29 kernel ipmi_si IPI0001:00: IRQ index 0 not found 错误 2025-11-8 9:29 kernel ipmi_si IPI0001:00: Interface detection failed 错误 2025-11-8 9:29 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 9:29 kernel Bluetooth: hci0: RTL: firmware file rtl_bt/rtl8761bu_fw not found 错误 2025-11-8 9:29 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:29 systemd[1] Failed to mount /mnt/24136ca5-0ec5-4b34-b2de-885a454f49e8. 错误 2025-11-8 9:29 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:29 systemd[1] Failed to mount /mnt/a10ad006-a33d-43ee-bd54-4152effbe297. 错误 2025-11-8 9:29 dbus-daemon[1687] [system] [limitCtl]:limit config org.ukui.GreeterDaemon.limit is corrupted, whitelist invalid. Update or reinstall corresponding software package! 错误 2025-11-8 9:29 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:29 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:29 kernel XFS (sda17): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:29 kernel EXT4-fs (sdc2): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:29 secRiskBox[4530] connect(signal_create):state:1 错误 2025-11-8 9:29 secRiskBox[4530] connect(signal_show):state:1 错误 2025-11-8 9:33 systemd-coredump[11963] Process 4515 (ukui-menu) of user 1000 dumped core. 错误 2025-11-8 9:36 kernel [drm:amdgpu_job_timedout [amdgpu]] *ERROR* ring gfx timeout, signaled seq=13990, emitted seq=13992 错误 2025-11-8 9:36 kernel [drm:amdgpu_job_timedout [amdgpu]] *ERROR* Process information: process Xorg pid 2208 thread Xorg:cs0 pid 2950 错误 2025-11-8 9:36 kernel [drm:atom_op_jump [amdgpu]] *ERROR* atombios stuck in loop for more than 5secs aborting 错误 2025-11-8 9:36 kernel [drm:amdgpu_atom_execute_table_locked [amdgpu]] *ERROR* atombios stuck executing E7BE (len 761, WS 0, PS 0) @ 0xE7EE 错误 2025-11-8 9:36 kernel amdgpu 0000:0b:00.0: [drm:amdgpu_ring_test_helper [amdgpu]] *ERROR* ring kiq_2.1.0 test failed (-110) 错误 2025-11-8 9:36 kernel [drm:gfx_v8_0_hw_fini [amdgpu]] *ERROR* KCQ disable failed 错误 2025-11-8 9:36 kernel cp is busy, skip halt cp 错误 2025-11-8 9:36 kernel rlc is busy, skip halt rlc 错误 2025-11-8 9:36 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:36 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:36 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:36 agetty[17805] tty2: invalid character 0x1b in login name 错误 2025-11-8 9:36 agetty[18009] tty2: invalid character 0x1b in login name 错误 2025-11-8 9:42 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 9:44 kernel sd 0:0:0:0: [sda] Asking for cache data failed 错误 2025-11-8 9:44 kernel sd 0:0:0:0: [sda] Assuming drive cache: write through 错误 2025-11-8 9:44 kernel sd 1:0:0:0: [sdb] Asking for cache data failed 错误 2025-11-8 9:44 kernel sd 1:0:0:0: [sdb] Assuming drive cache: write through 错误 2025-11-8 9:44 kernel sd 2:0:0:0: [sdc] Asking for cache data failed 错误 2025-11-8 9:44 kernel sd 2:0:0:0: [sdc] Assuming drive cache: write through 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel amdgpu: [powerplay] No valid PCIE lane width reported 错误 2025-11-8 9:44 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:44 kernel parport_pc parport_pc.956: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:44 kernel parport_pc parport_pc.888: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:44 kernel parport_pc parport_pc.632: Unable to set coherent dma mask: disabling DMA 错误 2025-11-8 9:45 kernel ipmi_si IPI0001:00: IRQ index 0 not found 错误 2025-11-8 9:45 kernel ipmi_si IPI0001:00: Interface detection failed 错误 2025-11-8 9:45 systemd[1] /lib/systemd/system/bluetooth.service:5: Failed to add dependency on lightdm.sevice, ignoring: Invalid argument 错误 2025-11-8 9:45 kernel Bluetooth: hci0: RTL: firmware file rtl_bt/rtl8761bu_fw not found 错误 2025-11-8 9:45 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:45 systemd[1] Failed to mount /mnt/24136ca5-0ec5-4b34-b2de-885a454f49e8. 错误 2025-11-8 9:45 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:45 systemd[1] Failed to mount /mnt/a10ad006-a33d-43ee-bd54-4152effbe297. 错误 2025-11-8 9:45 dbus-daemon[1975] [system] [limitCtl]:limit config org.ukui.GreeterDaemon.limit is corrupted, whitelist invalid. Update or reinstall corresponding software package! 错误 2025-11-8 9:45 kernel XFS (sda17): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:45 secRiskBox[5009] connect(signal_create):state:1 错误 2025-11-8 9:45 secRiskBox[5009] connect(signal_show):state:1 错误 2025-11-8 9:45 kernel XFS (sda4): Superblock has unknown read-only compatible features (0x8) enabled. 错误 2025-11-8 9:45 kernel EXT4-fs (sda16): couldn't mount RDWR because of unsupported optional features (10000) 错误 2025-11-8 9:45 kernel EXT4-fs (sdc2): couldn't mount RDWR because of unsupported optional features (10000)
最新发布
11-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值