SuessLabs / Linux.Bluetooth

.NET Linux Bluetooth library for interfacing with peripherals using DBus BlueZ. Sponsored by Suess Labs
https://www.nuget.org/packages/Linux.Bluetooth/
Apache License 2.0
43 stars 5 forks source link

Crashing Mono when scanning devices in a C# .NET Framework 4.8 WinForms app running on Raspberry Pi 3B+ #17

Open DougShuffield opened 9 months ago

DougShuffield commented 9 months ago

Device: Raspberry Pi 3B+ OS: Raspbian GNU/Linux 11 (bullseye) 32-bit Mono: 6.12.0.200 Bluez: 5.55 .NET Framework: 4.8 Linux.Bluetooth: 5.64.0

I am attempting to make a simple WinForms app with a single button that scans for and prints Bluetooth devices to the console. As soon as adapter.StartDiscoveryAsync() is called it crashes mono. I have plans to use Linux.Bluetooth at a larger scale, but not until I can get this very simple example working correctly and robustly.

Any help would be much appreciated. Let me know if you need any additional information.

Here is the code from Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Linux.Bluetooth;
using Linux.Bluetooth.Extensions;

namespace scan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            int scanSeconds = 5;

            IAdapter1 adapter = (await BlueZManager.GetAdaptersAsync()).FirstOrDefault();

            var adapterPath = adapter.ObjectPath.ToString();
            var adapterName = adapterPath.Substring(adapterPath.LastIndexOf("/") + 1);
            Console.WriteLine($"Using Bluetooth adapter {adapterName}");
            Console.WriteLine($"Adapter's full path:    {adapterPath}");

            Console.WriteLine("Starting discovery...");
            await adapter.StartDiscoveryAsync();
            Console.WriteLine("Delaying 5 seconds...");
            await Task.Delay(TimeSpan.FromSeconds(scanSeconds));
            Console.WriteLine("Stopping discovery...");
            await adapter.StopDiscoveryAsync();

            // Print out the devices we already know about.
            Console.WriteLine();
            Console.WriteLine("Getting devices...");
            var devices = await adapter.GetDevicesAsync();
            foreach (var device in devices)
            {
                string deviceDescription = await GetDeviceDescriptionAsync(device);
                Console.WriteLine($" - {deviceDescription}");
            }

        }
        private static async Task<string> GetDeviceDescriptionAsync(IDevice1 device)
        {
            var deviceProperties = await device.GetAllAsync();
            return $"{deviceProperties.Alias} (Address: {deviceProperties.Address}, RSSI: {deviceProperties.RSSI})";
        }
    }
}

And here is the crash report from mono:

$ sudo ./scan.exe
Using Bluetooth adapter hci0
Adapter's full path:    /org/bluez/hci0
Starting discovery...
Delaying 5 seconds...

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
     (No frames) 

=================================================================
    Telemetry Dumper:
=================================================================
Pkilling 0x1889530880x from 0x1888478208x
Pkilling 0x1898968064x from 0x1888478208x
Pkilling 0x1985803264x from 0x1888478208x
Pkilling 0x1895822336x from 0x1888478208x
Pkilling 0x1905259520x from 0x1888478208x
Pkilling 0x1908343808x from 0x1888478208x
Pkilling 0x1892676608x from 0x1888478208x
Pkilling 0x1995820288x from 0x1888478208x
Pkilling 0x1902113792x from 0x1888478208x
Pkilling 0x1887425536x from 0x1888478208x
Entering thread summarizer pause from 0x1888478208x
Finished thread summarizer pause from 0x1888478208x.
Failed to create breadcrumb file (null)/crash_hash_0x0

Waiting for dumping threads to resume

=================================================================
    External Debugger Dump:
=================================================================
[New LWP 28518]
[New LWP 28519]
[New LWP 28520]
[New LWP 28521]
[New LWP 28522]
[New LWP 28523]
[New LWP 28524]
[New LWP 28525]
[New LWP 28526]
[New LWP 28527]
[New LWP 28528]
[New LWP 28552]
Mono support loaded.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
__GI___poll (timeout=1000, nfds=2, fds=0x76203938) at ../sysdeps/unix/sysv/linux/poll.c:29
29  ../sysdeps/unix/sysv/linux/poll.c: No such file or directory.
  Id   Target Id                                       Frame 
* 1    Thread 0x76f5cd00 (LWP 28516) "cli"             __GI___poll (timeout=1000, nfds=2, fds=0x76203938) at ../sysdeps/unix/sysv/linux/poll.c:29
  2    Thread 0x75fff400 (LWP 28518) "SGen worker"     futex_wait_cancelable (private=0, expected=0, futex_word=0x40d290) at ../sysdeps/nptl/futex-internal.h:186
  3    Thread 0x765cf400 (LWP 28519) "Finalizer"       futex_abstimed_wait_cancelable (private=0, abstime=0x0, clockid=0, expected=1, futex_word=0x405528) at ../sysdeps/nptl/futex-internal.h:323
  4    Thread 0x71bf0400 (LWP 28520) "Thread Pool I/O" __GI___poll (timeout=-1, nfds=2, fds=0xbdcbd0) at ../sysdeps/unix/sysv/linux/poll.c:29
  5    Thread 0x718ff400 (LWP 28521) "Thread Pool I/O" futex_abstimed_wait_cancelable (private=<optimized out>, abstime=0x718fecac, clockid=<optimized out>, expected=0, futex_word=0x40d6b8) at ../sysdeps/nptl/futex-internal.h:323
  6    Thread 0x715ff400 (LWP 28522) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x715fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  7    Thread 0x712ff400 (LWP 28523) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x712fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  8    Thread 0x70fff400 (LWP 28524) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x70ffed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  9    Thread 0x70cff400 (LWP 28525) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x70cfed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  10   Thread 0x709ff400 (LWP 28526) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x709fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  11   Thread 0x708fe400 (LWP 28527) "Thread Pool Wor" 0x76bb1824 in __GI___wait4 (pid=28658, stat_loc=0x708fbf54, options=0, usage=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
  12   Thread 0x707fd400 (LWP 28528) "Thread Pool Wor" futex_abstimed_wait_cancelable (private=0, abstime=0x707fcd5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
  13   Thread 0x6fd72400 (LWP 28552) "Timer-Scheduler" futex_abstimed_wait_cancelable (private=<optimized out>, abstime=0x6fd712d4, clockid=<optimized out>, expected=0, futex_word=0x6b1da0) at ../sysdeps/nptl/futex-internal.h:323

Thread 13 (Thread 0x6fd72400 (LWP 28552) "Timer-Scheduler"):
#0  futex_abstimed_wait_cancelable (private=<optimized out>, abstime=0x6fd712d4, clockid=<optimized out>, expected=0, futex_word=0x6b1da0) at ../sysdeps/nptl/futex-internal.h:323
#1  __pthread_cond_wait_common (abstime=0x6fd712d4, clockid=<optimized out>, mutex=0x85ed, cond=0x6b1d78) at pthread_cond_wait.c:520
#2  __pthread_cond_timedwait (cond=0x6b1d78, mutex=0x85ed, abstime=0x6fd712d4) at pthread_cond_wait.c:656
#3  0x00303b0c in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 12 (Thread 0x707fd400 (LWP 28528) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x707fcd5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x707fcd5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x707fcd5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 11 (Thread 0x708fe400 (LWP 28527) "Thread Pool Wor"):
#0  0x76bb1824 in __GI___wait4 (pid=28658, stat_loc=0x708fbf54, options=0, usage=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x0009d030 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 10 (Thread 0x709ff400 (LWP 28526) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x709fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x709fed5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x709fed5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 9 (Thread 0x70cff400 (LWP 28525) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x70cfed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x70cfed5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x70cfed5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 8 (Thread 0x70fff400 (LWP 28524) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x70ffed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x70ffed5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x70ffed5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 7 (Thread 0x712ff400 (LWP 28523) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x712fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x712fed5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x712fed5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 6 (Thread 0x715ff400 (LWP 28522) "Thread Pool Wor"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x715fed5c, clockid=0, expected=1, futex_word=0x405a88) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405a88, abstime=abstime@entry=0x715fed5c, clockid=0) at sem_waitcommon.c:117
#2  0x76e98078 in __new_sem_wait_slow (sem=0x405a88, abstime=0x715fed5c, clockid=0) at sem_waitcommon.c:285
#3  0x002a36b0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 5 (Thread 0x718ff400 (LWP 28521) "Thread Pool I/O"):
#0  futex_abstimed_wait_cancelable (private=<optimized out>, abstime=0x718fecac, clockid=<optimized out>, expected=0, futex_word=0x40d6b8) at ../sysdeps/nptl/futex-internal.h:323
#1  __pthread_cond_wait_common (abstime=0x718fecac, clockid=<optimized out>, mutex=0x9ddf, cond=0x40d690) at pthread_cond_wait.c:520
#2  __pthread_cond_timedwait (cond=0x40d690, mutex=0x9ddf, abstime=0x718fecac) at pthread_cond_wait.c:656
#3  0x00303b0c in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 4 (Thread 0x71bf0400 (LWP 28520) "Thread Pool I/O"):
#0  __GI___poll (timeout=-1, nfds=2, fds=0xbdcbd0) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  __GI___poll (fds=0xbdcbd0, nfds=2, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:26
#2  0x002532f4 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 3 (Thread 0x765cf400 (LWP 28519) "Finalizer"):
#0  futex_abstimed_wait_cancelable (private=0, abstime=0x0, clockid=0, expected=1, futex_word=0x405528) at ../sysdeps/nptl/futex-internal.h:323
#1  do_futex_wait (sem=sem@entry=0x405528, abstime=0x0, clockid=0) at sem_waitcommon.c:117
#2  0x76e97c90 in __new_sem_wait_slow (sem=0x405528, abstime=0x0, clockid=0) at sem_waitcommon.c:285
#3  0x00292b38 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 2 (Thread 0x75fff400 (LWP 28518) "SGen worker"):
#0  futex_wait_cancelable (private=0, expected=0, futex_word=0x40d290) at ../sysdeps/nptl/futex-internal.h:186
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x0, cond=0x40d268) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=0x40d268, mutex=0x0) at pthread_cond_wait.c:638
#3  0x002f512c in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Thread 1 (Thread 0x76f5cd00 (LWP 28516) "cli"):
#0  __GI___poll (timeout=1000, nfds=2, fds=0x76203938) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  __GI___poll (fds=0x76203938, nfds=2, timeout=1000) at ../sysdeps/unix/sysv/linux/poll.c:26
#2  0x7214a778 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
[Inferior 1 (process 28516) detached]
Stopping discovery...

=================================================================
    Basic Fault Address Reporting
=================================================================
instruction pointer is NULL, skip dumping
=================================================================
    Managed Stacktrace:
=================================================================
=================================================================
Aborted
DougShuffield commented 9 months ago

I was able to get the following using gdb on the crash:

(gdb) run --debug scan.exe
Starting program: /usr/bin/mono --debug scan.exe
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
[New Thread 0x75fff400 (LWP 17834)]
[New Thread 0x743bc400 (LWP 17835)]
[New Thread 0x716e5400 (LWP 17836)]
[New Thread 0x713ff400 (LWP 17837)]
[New Thread 0x710ff400 (LWP 17838)]
[New Thread 0x70dff400 (LWP 17839)]
[New Thread 0x70aff400 (LWP 17840)]
[New Thread 0x707ff400 (LWP 17841)]
[New Thread 0x704ff400 (LWP 17842)]
[New Thread 0x703fe400 (LWP 17843)]
[New Thread 0x702fd400 (LWP 17844)]
Using Bluetooth adapter hci0
Adapter's full path:    /org/bluez/hci0
Starting discovery...
Delaying 5 seconds...
[New Thread 0x701fc400 (LWP 17846)]

Thread 9 "Thread Pool Wor" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x707ff400 (LWP 17841)]
0x00000000 in ?? ()
(gdb) where
#0  0x00000000 in ?? ()
#1  0x715c3748 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) p (char*)mono_pmip (0x715c3748)
[Thread 0x702fd400 (LWP 17844) exited]
[Thread 0x710ff400 (LWP 17838) exited]
$2 = 0x7052ed40 " (wrapper delegate-invoke) Tmds.DBus.CodeGen.ReadMethodDelegate`1<Tmds.DBus.PropertyChanges>:invoke_T_MessageReader (Tmds.DBus.Protocol.MessageReader) [{0x7051e510} + 0xf0]  (0x715c3658 0x715c38d8) [0"...

Hopefully, that can help locate the issue. Any help is appreciated.

Doug

DamianSuess commented 9 months ago

At this time, Linux Bluetooth has only been tested against .NET 5, .NET 6, and .NET 7, not the classic Mono (.NET Framework).

The most recent gdb crash log was most helpful, the folks over at Tmds.Dbus may have more insight into this issue.

DougShuffield commented 9 months ago

I believe the version of Tmds.Dbus that is used in Linux.Bluetooth is 0.11.0? Not the most current 0.15.0? I expect that will likely be the first question they ask.

Doug

DamianSuess commented 8 months ago

I agree, an update with the latest Tmds.Dbus version will be published soon

DamianSuess commented 8 months ago

Hi @DougShuffield, the latest package v5.66 has been published which uses Tmds v0.15.0.

DamianSuess commented 1 week ago

@DougShuffield I updated to the package to use the latest Tmds.DBus to v0.20. Give it a shot and let me know if you're still having issues