Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application Crashes When onInputEvent is Used in Electron MacOS Environment #129

Open
lie5860 opened this issue Mar 21, 2024 · 4 comments
Open

Comments

@lie5860
Copy link

lie5860 commented Mar 21, 2024

The rdev is wrapped with @napi-rs/cli for nodejs use

Here is the code

use napi::{
    bindgen_prelude::*,
    threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
    Result,
};
use rdev::{listen};
use std::{
    thread::spawn,
};

#[napi(ts_args_type = "callback: (event: string) => void")]
pub fn on_input_event(callback: JsFunction) -> Result<()> {
    let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> =
        callback.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))?;
    spawn(|| {
        if let Err(error) = listen(move |event| {
            jsfn.call(
                serde_json::to_string(&event).unwrap(),
                ThreadsafeFunctionCallMode::NonBlocking,
            );
        }) {
            println!("Error: {:?}", error)
        }
    });
    Ok(())
}

There's no problem using it in nodejs alone.

Install electron main.js and fill in some simple sample code. At the beginning, the capture seems to be normal. The focus is placed on the new window. As soon as a button is pressed, the application crashes.

/Users/test/Documents/code/rust-items/untitled/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron exited with signal SIGTRAP

Below is the simplest reproducible demo
main.js

const {app, BrowserWindow} = require('electron');
// demo https://github.com/lie5860/biz-napi
const {onInputEvent} = require('@lie5860/biz-napi');
const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600
    })
    win.show()
}
console.log(`app whenReady`);
app.whenReady().then(() => {
    createWindow();
    onInputEvent((e) => {
        console.log(`onInputEvent`);
    })
})

package.json

{
  "name": "untitled",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "electron ./main.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@lie5860/biz-napi": "0.2.0",
  },
  "devDependencies": {
    "electron": "^29.1.4"
  }
}

Or, is there a way to only monitor the mouse... skip the keyboard events...

Appendix: Issue Report

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

Process:               Electron [56479]
Path:                  /Users/USER/Documents/*/Electron.app/Contents/MacOS/Electron
Identifier:            com.github.Electron
Version:               29.1.4 (29.1.4)
Code Type:             ARM-64 (Native)
Parent Process:        Exited process [56478]
Responsible:           webstorm [19754]
User ID:               501

Date/Time:             2024-03-21 11:49:14.5797 +0800
OS Version:            macOS 14.4 (23E214)
Report Version:        12
Anonymous UUID:        0C9C8BA6-B998-7477-949D-8607938F77F0

Sleep/Wake UUID:       D10F9B0D-C82E-4A05-AC97-E10148912D54

Time Awake Since Boot: 680000 seconds
Time Since Wake:       86900 seconds

System Integrity Protection: enabled

Crashed Thread:        32

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

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

Thread 0:: CrBrowserMain Dispatch queue: com.apple.main-thread
@Barrierml
Copy link

Barrierml commented Jun 20, 2024

same error, Did you find any solution?

@Barrierml
Copy link

I found a temporarily solution, you can use fork of child_process to isolate them env, it's work
like this

// child.js
const { startListener } = require('node-rdev');

startListener((dataStr) => {
  process.send(dataStr);
});
// your electron som code ...
const childProcess = fork(join(app.getAppPath(), './dist/main/child/index.js'));

childProcess.on('message', (message: string) => {
  console.log(message)
});

@lie5860
Copy link
Author

lie5860 commented Jun 20, 2024

I found a temporarily solution, you can use fork of child_process to isolate them env, it's work like this

// child.js
const { startListener } = require('node-rdev');

startListener((dataStr) => {
  process.send(dataStr);
});
// your electron som code ...
const childProcess = fork(join(app.getAppPath(), './dist/main/child/index.js'));

childProcess.on('message', (message: string) => {
  console.log(message)
});

Sorry for the late reply. I handle it exactly like that. It works fine now on a regular Mac, but it will fail if the Mac is being controlled by remote software.

@lie5860
Copy link
Author

lie5860 commented Jun 20, 2024

I haven't found a reason for it yet, and I haven't found a suitable alternative monitoring method either. The priority of the issue of Mac being remotely controlled by the product is also relatively low. So sad~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants