Skyost / Bonsoir

A Zeroconf library that allows you to discover network services and to broadcast your own. Based on Apple Bonjour and Android NSD.
http://bonsoir.skyost.eu/
Other
116 stars 50 forks source link

[macOS/windows] Random crash when starting and stopping discovery #89

Open karniv00l opened 8 months ago

karniv00l commented 8 months ago

Describe the bug

Hi 👋

I was testing a way to force discovery to send more queries (or at higher frequency) using Riverpod's refresh/invalidate, because I had some issues with my embedded device being discovered and stumbled upon this issue. There is no stack trace provided, even when running flutter in verbose. This happens to me on macOS (14.3.1, M1 Max), but have reports indicating that it also happens on Windows (Windows 10 Pro).

To Reproduce Steps to reproduce the behavior:

Minimal repro project is here: https://github.com/karniv00l/bonsoir_issue

And code looks like that:

// ignore_for_file: avoid_print

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:bonsoir/bonsoir.dart';

final deviceDiscoveryProvider = Provider<BonsoirDiscovery>((ref) {
  final discovery = BonsoirDiscovery(type: '_testing._udp');

  discovery.ready.then((value) {
    discovery.eventStream!.listen(((event) => print('[Event] ${event.type}')));

    print('Starting');
    discovery.start();
  });

  ref.onDispose(() async {
    print('Stopping');
    await discovery.stop();
    print('Stopped');
  });

  return discovery;
});

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    ref.watch(deviceDiscoveryProvider);

    Timer.periodic(const Duration(seconds: 1), (timer) {
      ref.invalidate(deviceDiscoveryProvider);
    });

    return const MaterialApp(
      title: 'Bonsoir test',
      home: Scaffold(body: Center(child: Text('Hello World'))),
    );
  }
}

The app will randomly crash after a couple of seconds without any errors/stack trace.

Expected behavior

App should not crash without any errors

Desktop (please complete the following information):

Additional context

Going back to my initial problem - polling/querying for the service - is there any way this can be configured? It looks like the default behavior is exponential back off.

Skyost commented 8 months ago

Hey,

Can you try to run your project from Visual Studio on Windows using the local debugger ? I think you'll be able to get a crash log and a stacktrace 🙂

karniv00l commented 8 months ago

Thanks, I did a quick check on macOS, here's what I found:

https://github.com/Skyost/Bonsoir/blob/c2ca675b89a6f8d5603b5b5a4edcbc5ac94c14c5/packages/bonsoir_darwin/darwin/Classes/Discovery/BonsoirServiceDiscovery.swift#L153

Screenshot 2024-03-30 at 13 25 29
Skyost commented 7 months ago

Maybe it's a synchronization problem that occurs because of platforms have troubles dealing with a lot of browser instances or something like that.

Skyost commented 7 months ago

Hey,

I've merged #91 into master. Can you please try it and tell me if it solves your problem on Mac ?

dependencies:
  bonsoir:
    git:
      url: git@github.com:Skyost/Bonsoir.git
      ref: master
karniv00l commented 6 months ago

I've switched to Flutter's main channels, and now I see native stack trace in the stdout.

This, however, looks the same as on Bonsoir master and 5.1.9:

[previous prints]
...
flutter: [Event] BonsoirDiscoveryEventType.discoveryStarted
flutter: [Event] BonsoirDiscoveryEventType.discoveryStopped
flutter: Stopping
AddressSanitizer:DEADLYSIGNAL
=================================================================
==24639==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000054 (pc 0x00019ca92dec bp 0x00016f4d9f50 sp 0x00016f4d9f30 T0)
==24639==The signal is caused by a READ memory access.
==24639==Hint: address points to the zero page.
flutter: Starting
==24639==WARNING: external symbolizer didn't start up correctly!
==24639==WARNING: Can't write to symbolizer at fd 20
==24639==WARNING: external symbolizer didn't start up correctly!
==24639==WARNING: Can't write to symbolizer at fd 21
==24639==WARNING: external symbolizer didn't start up correctly!
==24639==WARNING: Can't write to symbolizer at fd 22
==24639==WARNING: external symbolizer didn't start up correctly!
==24639==WARNING: Can't write to symbolizer at fd 23
==24639==WARNING: external symbolizer didn't start up correctly!
==24639==WARNING: Failed to use and restart external symbolizer!
    #0 0x19ca92dec in dispatch_async+0xc0 (/usr/lib/system/libdispatch.dylib:arm64e+0x8dec)
    #1 0x101341e38 in wrap_dispatch_async+0x7c (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/libclang_rt.asan_osx_dynamic.dylib:arm64e+0x51e38)
    #2 0x1a445c614 in nw_browser_set_state_locked+0x238 (/System/Library/Frameworks/Network.framework/Versions/A/Network:arm64e+0x6a9614)
    #3 0x1a4465d44 in nw_browser_cancel+0x1f0 (/System/Library/Frameworks/Network.framework/Versions/A/Network:arm64e+0x6b2d44)
    #4 0x100e6c484 in bonsoir_darwin.BonsoirServiceDiscovery.dispose() -> ()+0x834 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/bonsoir_darwin.framework/Versions/A/bonsoir_darwin:arm64+0x2c484)
    #5 0x100e83a10 in bonsoir_darwin.SwiftBonsoirPlugin.handle(_: __C.FlutterMethodCall, result: (Swift.Optional<Any>) -> ()) -> ()+0x6154 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/bonsoir_darwin.framework/Versions/A/bonsoir_darwin:arm64+0x43a10)
    #6 0x100e84a20 in @objc bonsoir_darwin.SwiftBonsoirPlugin.handle(_: __C.FlutterMethodCall, result: (Swift.Optional<Any>) -> ()) -> ()+0x118 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/bonsoir_darwin.framework/Versions/A/bonsoir_darwin:arm64+0x44a20)
    #7 0x105c13384 in __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke+0xb0 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0xc63384)
    #8 0x104fc6470 in -[FlutterEngine engineCallbackOnPlatformMessage:]+0x19c (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x16470)
    #9 0x105835328 in std::_fl::__function::__func<FlutterEngineInitialize::$_34, std::_fl::allocator<FlutterEngineInitialize::$_34>, void (std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>)>::operator()(std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>&&)+0x7c (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x885328)
    #10 0x1058470ac in flutter::PlatformViewEmbedder::HandlePlatformMessage(std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>)+0x48 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x8970ac)
    #11 0x105847bc4 in std::_fl::__function::__func<fml::internal::CopyableLambda<flutter::PlatformViewEmbedder::EmbedderPlatformMessageHandler::HandlePlatformMessage(std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>)::'lambda'()>, std::_fl::allocator<fml::internal::CopyableLambda<flutter::PlatformViewEmbedder::EmbedderPlatformMessageHandler::HandlePlatformMessage(std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>)::'lambda'()>>, void ()>::operator()()+0x4c (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x897bc4)
    #12 0x1058446c8 in flutter::EmbedderTaskRunner::PostTask(unsigned long long)+0x288 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x8946c8)
    #13 0x10582d0b0 in FlutterEngineRunTask+0x20 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x87d0b0)
    #14 0x104fc9498 in -[FlutterEngine runTaskOnEmbedder:]+0x34 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x19498)
    #15 0x104fc964c in __60-[FlutterEngine postMainThreadTask:targetTimeInNanoseconds:]_block_invoke+0x3c (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS:arm64+0x1964c)
    #16 0x101341f0c in __wrap_dispatch_async_block_invoke+0xc0 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/Frameworks/libclang_rt.asan_osx_dynamic.dylib:arm64e+0x51f0c)
    #17 0x19ca8c74c in _dispatch_call_block_and_release+0x1c (/usr/lib/system/libdispatch.dylib:arm64e+0x274c)
    #18 0x19ca8e3e4 in _dispatch_client_callout+0x10 (/usr/lib/system/libdispatch.dylib:arm64e+0x43e4)
    #19 0x19ca9cbb4 in _dispatch_main_queue_drain+0x3d8 (/usr/lib/system/libdispatch.dylib:arm64e+0x12bb4)
    #20 0x19ca9c7c8 in _dispatch_main_queue_callback_4CF+0x28 (/usr/lib/system/libdispatch.dylib:arm64e+0x127c8)
    #21 0x19cd5f4a8 in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__+0xc (/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation:arm64e+0xbf4a8)
    #22 0x19cd1cc2c in __CFRunLoopRun+0x7c8 (/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation:arm64e+0x7cc2c)
    #23 0x19cd1be08 in CFRunLoopRunSpecific+0x25c (/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation:arm64e+0x7be08)
    #24 0x1a74b6ffc in RunCurrentEventLoopInMode+0x120 (/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox:arm64e+0x32ffc)
    #25 0x1a74b6e38 in ReceiveNextEventCommon+0x284 (/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox:arm64e+0x32e38)
    #26 0x1a74b6b90 in _BlockUntilNextEventMatchingListInModeWithFilter+0x48 (/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox:arm64e+0x32b90)
    #27 0x1a057496c in _DPSNextEvent+0x290 (/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit:arm64e+0x3a96c)
    #28 0x1a0d66de8 in -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]+0x2b8 (/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit:arm64e+0x82cde8)
    #29 0x1a0567cb4 in -[NSApplication run]+0x1d8 (/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit:arm64e+0x2dcb4)
    #30 0x1a053ef50 in NSApplicationMain+0x36c (/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit:arm64e+0x4f50)
    #31 0x100924144 in static (extension in AppKit):__C.NSApplicationDelegate.main() -> ()+0x24 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/MacOS/bonsoir_issue:arm64+0x100004144)
    #32 0x1009240a8 in static bonsoir_issue.AppDelegate.$main() -> ()+0x168 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/MacOS/bonsoir_issue:arm64+0x1000040a8)
    #33 0x100924434 in main+0x18 (/Users/piotrek/git/bonsoir_issue/build/macos/Build/Products/Debug/bonsoir_issue.app/Contents/MacOS/bonsoir_issue:arm64+0x100004434)
    #34 0x19c8b60dc  (<unknown module>)

==24639==Register values:
 x[0] = 0x00000001039d7310   x[1] = 0x00000001039dbb10   x[2] = 0x0000000000000007   x[3] = 0x00000001a3e6d914  
 x[4] = 0x000000010339be80   x[5] = 0x0000000000000001   x[6] = 0x000000016ece4000   x[7] = 0x0000000000000001  
 x[8] = 0x00000000000010ff   x[9] = 0x0000000000000000  x[10] = 0x0000000043000008  x[11] = 0x00000001039dbb18  
x[12] = 0x0000000043000006  x[13] = 0x0000000000000004  x[14] = 0x0000000000000000  x[15] = 0x0000000000000000  
x[16] = 0x000000019ca8c730  x[17] = 0x0000000101341e4c  x[18] = 0x0000000000000000  x[19] = 0x0000000000000000  
x[20] = 0x000000010a007500  x[21] = 0x000000016f4d9f78  x[22] = 0x000000020494bba0  x[23] = 0x0000000000000000  
x[24] = 0x0000000000000002  x[25] = 0x000000010582c624  x[26] = 0x000000010a007500  x[27] = 0x000000000000000f  
x[28] = 0x0000000000000000     fp = 0x000000016f4d9f50     lr = 0x000000019ca92d64     sp = 0x000000016f4d9f30  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/lib/system/libdispatch.dylib:arm64e+0x8dec) in dispatch_async+0xc0
==24639==ABORTING
Lost connection to device.

Exited.
rohanjariwala03 commented 4 months ago

Any update on random crash bug? Due to this package my app is crashing on windows machine when I start and stop discovery continuously. The warning before crash I'm seeing is :

The `fr.skyost.bonsoir.discovery.60365` channel sent a message from native to Flutter on a non-platform thread. Platform channel message must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel

Skyost commented 3 months ago

@rohanjariwala03 This has nothing to do with a crash (see #78).

thiagokisaki commented 3 months ago

I'm also experiencing crashes on Windows. It happens when quickly starting and stopping the service discovery.

Demo (expand the video):

https://github.com/user-attachments/assets/211fe7eb-7edf-488c-9285-de633f187730

The issue seems to be in the following line: https://github.com/Skyost/Bonsoir/blob/a165bdfc55409d9776742256aa86b2e3ca488e7a/packages/bonsoir_windows/windows/bonsoir_discovery.cpp#L43

dustin-auby commented 1 month ago

HAs anyone figured out how to fix this. i get the same error and after that the discovery stops and will not resolve the host

Thefr.skyost.bonsoir.discovery.60365channel sent a message from native to Flutter on a non-platform thread. Platform channel message must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel

Skyost commented 1 month ago

@dustin-auby See #78.

Alvish0407 commented 3 weeks ago
dependencies:
  bonsoir:
    git:
      url: git@github.com:Skyost/Bonsoir.git
      ref: master
Error on line 1, column 7: "name" field doesn't match expected name "bonsoir".
  â•·
1 │ name: bonsoir_workspace
  │       ^^^^^^^^^^^^^^^^^
  ╵
exit code 65
Alvish0407 commented 3 weeks ago
dependencies:
  bonsoir:
    git:
      url: git@github.com:Skyost/Bonsoir.git
      ref: master
Error on line 1, column 7: "name" field doesn't match expected name "bonsoir".
  â•·
1 │ name: bonsoir_workspace
  │       ^^^^^^^^^^^^^^^^^
  ╵
exit code 65
dependencies:
  bonsoir:
    git:
      url: https://github.com/Skyost/Bonsoir
      ref: master
+     path: packages/bonsoir
Alvish0407 commented 3 weeks ago

Hey,

I've merged #91 into master. Can you please try it and tell me if it solves your problem on Mac ?

dependencies:
  bonsoir:
    git:
      url: git@github.com:Skyost/Bonsoir.git
      ref: master

this doesn't solve the problem.