tidev / node-ios-device

Queries connected iOS devices and installs apps
Other
122 stars 37 forks source link

Crash without error message in the Electron application. #58

Closed zcmgyu closed 4 years ago

zcmgyu commented 4 years ago

After about 1 week of debugging to figure out the cause of the application closing itself without any error message, I discovered that the main cause of crashing is from the node-ios-device library. Everything works normally, but after about 1 hour, a crash occurs. What I think is strange is that a crash occurs even iosDevice.watch() was not called. Or just by importing the node-ios-devices library can a crash occur.

Sometimes, I got this message after crashing as well.

[21194:0607/225605.295914:FATAL:message_pump_kqueue.cc(387)] : Bad file descriptor (9)

This is a great library, I would appreciate it if you could show me how to fix this problem. (bow)

Reproduction: https://github.com/zcmgyu/crash-electron-node-ios-device/blob/master/main.js#L22-L28

const handle = iosDevice.watch();
  handle.on('change', devices => {
    console.log('Connected devices:', devices);
  });
  handle.on('error', console.error);

  iosDevice.on('log', msg => console.log(msg));

This is logs output from iosDevice.on('log')

$ electron .
Creating device list with 1 devices
Connected devices: [
  {
    udid: 'XXXX',
    interfaces: [ 'Wi-Fi' ],
    name: 'ZC X',
    buildVersion: '17F75',
    cpuArchitecture: 'arm64',
    deviceClass: 'iPhone',
    deviceColor: 'Black',
    hardwareModel: 'D22AP',
    modelNumber: 'MQAY2',
    productType: 'iPhone10,3',
    productVersion: '13.5',
    serialNumber: 'FK1VVTGLJCLL',
    trustedHostAttached: false
  }
]
Adding listener
Creating device list with 1 devices
Resetting timer due to new device notification
Device XXXX disconnected via Wi-Fi
Connected devices: []
Creating device list with 0 devices
Dispatching device changes to 1 listener (thread 2978043419396287625)
Resetting timer due to new device notification
Device XXXX connected via Wi-Fi
Getting device info for XXXX
Connecting to device: XXXX
Pairing device: XXXX
Validating device pairing
Starting session: XXXX
Stopping session: XXXX
Disconnecting from device: XXXX
Connected devices: [
  {
    udid: 'XXXX',
    interfaces: [ 'Wi-Fi' ],
    name: 'ZC X',
    buildVersion: '17F75',
    cpuArchitecture: 'arm64',
    deviceClass: 'iPhone',
    deviceColor: 'Black',
    hardwareModel: 'D22AP',
    modelNumber: 'MQAY2',
    productType: 'iPhone10,3',
    productVersion: '13.5',
    serialNumber: 'FK1VVTGLJCLL',
    trustedHostAttached: false
  }
]
Creating device list with 1 devices
Dispatching device changes to 1 listener (thread 2978043419396287625)
Resetting timer due to new device notification
Device XXXX disconnected via Wi-Fi
Connected devices: []
✨  Done in 225.28s.

node-ios-device: 2.0.2 electron: 9.0.2 node: 14.3.0

cb1kenobi commented 4 years ago

Thank you for bringing this issue to my attention. I have not yet tested it against Node 14, so there could be some issue with the latest libuv or something. I'll take a peek.

zcmgyu commented 4 years ago

Hi @cb1kenobi, I tried with version 1.7.1 that crash does not occur anymore, but it makes for 4 instances of Electron application. This issue occurs when upgrading from 0.8.3 to 0.9.5 (between versions not work): https://github.com/appcelerator/node-ios-device/compare/0.8.3%E2%80%A60.9.5

Reproduction: https://github.com/zcmgyu/crash-electron-node-ios-device/tree/1.7.1

Screen Shot 2020-06-09 at 10 07 56

The first one shows UI, others show nothing.

Update: (2019/6/9)

Add below code inside ipcMain.on resolved create 4 instances issue.

ipcMain.on(('EVENT_NAME') => {
    // continuously watch for devices to be connected or disconnected
    iosDevice
    .trackDevices()
    .on('devices', function (devices) {
        console.log('Connected devices:');
        console.log(devices);
    })
    .on('error', function (err) {
        console.error('Error!', err);
    });
})

Otherwise, I tested node-ios-device with Node 14 and I don't think this issue related to Node 14. It just occurs when used in the Electron project.

cb1kenobi commented 4 years ago

@zcmgyu OK, I did a build with Electron compatibility and published v2.1.0. Can you please give it a try? Thanks!

zcmgyu commented 4 years ago

OMG. I will try it and will response ASAP

zcmgyu commented 4 years ago

Hi, @cb1kenobi I've checked it. After 10989.90s (another check 939.25s.) application crashes again.

cb1kenobi commented 4 years ago

@zcmgyu Not sure what to tell you.

I have never tested node-ios-device with an Electron app and supporting Electron is not something that I regretfully have the time to do right now. I was hoping building node-ios-device@2.1.0 with the --electron-compat flag would have fixed it.

I have run node-ios-device with Node.js for days without issue, though I should test that again with Node 14.

Your code snippets look right, so I don't think you're using node-ios-device incorrectly.

The bad file descriptor error is coming from Chromium, which could be caused by node-ios-device, but it could also be caused by whatever secret sauce Electron is using to glue Node to Chromium.

node-ios-device relies on Node.js's libuv for multithreading and message loops. libuv does use kqueues on macOS. Perhaps Electron has altered libuv or its Node.js since it needs to coordinate with Chromium and ensure that things stay alive between the two runtimes. I have no clue.

Since you can reproduce the issue, you will probably have to do some fiddling and see if you can find the problem. Maybe there's a known issue with native addons and Electron? Maybe you need to build node-ios-device yourself?

I wish I had more bandwidth to install Electron and attempt to reproduce. Best of luck and let me know if there's anything you find.

zcmgyu commented 4 years ago

Currently, 1.7.1 worked for me, I've made a ticket on Electron already. Thanks for your support.

cb1kenobi commented 4 years ago

@zcmgyu I thought you might be interested to hear that I just release node-ios-device 3.0.0. I found a segfault and fixed it. I'm not convinced it has anything to do with the bad file descriptor error you ran into, but figured you'd want to know. v3 also has a few other breaking changes that probably won't affect you. Here's the release notes: https://github.com/appcelerator/node-ios-device/releases/tag/v3.0.0. Cheers!

zcmgyu commented 4 years ago

@cb1kenobi OMG, you are awesome. I will check it on a business day. <3

zcmgyu commented 4 years ago

Hi @cb1kenobi. I've been testing the Electron application crashing with a 3.0.0 version for a couple of days. It's still working without crashing. Thanks for your hard work. <3 I will close this ticket.

cb1kenobi commented 4 years ago

@zcmgyu This is the best news I've heard all day! Thank you for your assistance and best of luck with your project! Cheers!