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

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

Open
karniv00l opened this issue Mar 28, 2024 · 8 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@karniv00l
Copy link

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):

  • OS: 14.3.1, M1 Max and Windows 10 Pro

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.

@karniv00l karniv00l added the bug Something isn't working label Mar 28, 2024
@Skyost
Copy link
Owner

Skyost commented Mar 30, 2024

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
Copy link
Author

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

Screenshot 2024-03-30 at 13 25 29

@Skyost
Copy link
Owner

Skyost commented Apr 6, 2024

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
Copy link
Owner

Skyost commented Apr 18, 2024

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
Copy link
Author

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
Copy link

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
Copy link
Owner

Skyost commented Aug 5, 2024

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

@thiagokisaki
Copy link

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

Demo (expand the video):

Screen.Recording.2024-08-15.150032.mp4

The issue seems to be in the following line:

for (auto &found_service : this->services) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants