Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Segfault at exit on Linux arm64 in Electron #156

Closed
fstanchina opened this issue Feb 7, 2022 · 2 comments
Closed

Segfault at exit on Linux arm64 in Electron #156

fstanchina opened this issue Feb 7, 2022 · 2 comments

Comments

@fstanchina
Copy link

fstanchina commented Feb 7, 2022

The usb-detection native module causes a segfault in udev_monitor_receive_device() at exit on Linux arm64. It appears that cbWork() gets called with an invalid monitor object, I guess that on shutdown it's released before the worker stops.

This could explain some sporadic crashes we've seen on Intel. Probably the less powerful hardware causes a race that is rarer on faster machines.

Here's a patch that fixes it, set mon to NULL after releasing (plus some diagnostics). I've actually seen the "Error: udev monitor is null in cbWork()!" message. Probably best would be to do auto tmp = mon; mon = NULL; udev_monitor_unref(tmp); to guarantee that the worker never sees an invalid mon.

diff -ur ../usb-detection_save/src/detection_linux.cpp node_modules/usb-detection/src/detection_linux.cpp
--- ../usb-detection_save/src/detection_linux.cpp	2022-02-07 16:12:26.198991223 +0100
+++ node_modules/usb-detection/src/detection_linux.cpp	2022-02-07 16:23:07.020071344 +0100
@@ -98,7 +98,9 @@
 	uv_cond_destroy(&notifyDeviceHandled);
 
 	udev_monitor_unref(mon);
+	mon = NULL;
 	udev_unref(udev);
+	udev = NULL;
 }
 
 void InitDetection() {
@@ -112,6 +114,12 @@
 
 	/* Set up a monitor to monitor devices */
 	mon = udev_monitor_new_from_netlink(udev, "udev");
+	if (!mon)
+	{
+		printf("Can't create udev monitor from netlink\n");
+		return;
+	}
+
 	udev_monitor_enable_receiving(mon);
 
 	/* Get the file descriptor (fd) for the monitor.
@@ -226,6 +234,12 @@
 		if (!ret) continue;
 		if (ret < 0) break;
 
+		if (!mon)
+		{
+			printf("Error: udev monitor is null in cbWork()!\n");
+			return;
+		}
+
 		dev = udev_monitor_receive_device(mon);
 		if (dev) {
 			if(udev_device_get_devtype(dev) && strcmp(udev_device_get_devtype(dev), DEVICE_TYPE_DEVICE) == 0) {
@MadLittleMods
Copy link
Owner

Hey @fstanchina, it looks like you're fixing the same problem that #138 is trying to address.

And similar to the patch that @kfazz mentioned here #57 (comment)

I'm going to close this in favor of those. But an updated PR welcome with the review feedback addressed is welcome!

@fstanchina
Copy link
Author

OK, I agree. Sorry, I read thru the existing issues too fast.

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

No branches or pull requests

2 participants