tinygo-org / bluetooth

Cross-platform Bluetooth API for Go and TinyGo. Supports Linux, macOS, Windows, and bare metal using Nordic SoftDevice or HCI
https://tinygo.org
Other
744 stars 137 forks source link

Crashes on Windows if run in a GoRoutine #294

Open iKadmium opened 1 month ago

iKadmium commented 1 month ago

On Windows, calls to Connect(), DiscoverServices(), DiscoverCharacteristics() or EnableNotifications() crash if not run from the main thread. Example stack trace at the end. The same code runs fine on Linux ARM, but on Windows will only run if the calls are wrapped in mainthread.Call.

Calls to Scan, WriteWithoutResponse or Read seem to work fine, however.

runtime: newstack at runtime.printlock+0x53 sp=0xc000152cf0 stack=[0xc000150000, 0xc000154000]
    morebuf={pc:0xf86334 sp:0xc000152cf8 lr:0x0}
    sched={pc:0xfbe733 sp:0xc000152cf0 lr:0x0 ctxt:0x0}
runtime.callbackUpdateSystemStack(0xc000076008, 0xb2a9ff320, 0x0)
    C:/Program Files/Go/src/runtime/cgocall.go:257 +0x94 fp=0xc000152d58 sp=0xc000152cf8 pc=0xf86334
runtime.cgocallbackg(0xfda260, 0xb2a9ff360, 0x0)
    C:/Program Files/Go/src/runtime/cgocall.go:322 +0x71 fp=0xc000152dd8 sp=0xc000152d58 pc=0xf86551
runtime.cgocallbackg(0xfda260, 0xb2a9ff360, 0x0)
    <autogenerated>:1 +0x2e fp=0xc000152e00 sp=0xc000152dd8 pc=0xffd76e
runtime.cgocallback(0xc000152e90, 0xfeb592, 0xffa420)
    C:/Program Files/Go/src/runtime/asm_amd64.s:1084 +0xec fp=0xc000152e28 sp=0xc000152e00 pc=0xff89ec
runtime.systemstack_switch()
    C:/Program Files/Go/src/runtime/asm_amd64.s:479 +0x8 fp=0xc000152e38 sp=0xc000152e28 pc=0xff6c28
runtime.cgocall(0xffa420, 0xc000076330)
    C:/Program Files/Go/src/runtime/cgocall.go:185 +0x72 fp=0xc000152ea0 sp=0xc000152e38 pc=0xfeb592
syscall.SyscallN(0x1e04cd62660?, {0xc000196050?, 0x1e04cd639f8?, 0xc000152ed0?})
    C:/Program Files/Go/src/runtime/syscall_windows.go:519 +0x5d fp=0xc000152ec8 sp=0xc000152ea0 pc=0xff4b3d
github.com/saltosystems/winrt-go/windows/foundation.(*IAsyncOperation).SetCompleted(0x1e04cd62660, 0x1e04cd639f0)
    C:/Users/jesse/go/pkg/mod/github.com/saltosystems/winrt-go@v0.0.0-20240509164145-4f7860a3bd2b/windows/foundation/iasyncoperation.go:35 +0xe5 fp=0xc000152f90 sp=0xc000152ec8 pc=0x10eebc5
tinygo.org/x/bluetooth.awaitAsyncOperation(0x1e04cd62660, {0x115f4fb, 0x76})
    C:/Users/jesse/go/pkg/mod/tinygo.org/x/bluetooth@v0.10.0/adapter_windows.go:52 +0x246 fp=0xc0001530d8 sp=0xc000152f90 pc=0x10ff0c6
tinygo.org/x/bluetooth.DeviceService.DiscoverCharacteristics({{0x24dcca99, 0xe0a9e50e, 0xb5a3f393, 0x69400001}, 0x1e04cd02810, {{{{...}, 0x0}}, 0x1e04cd1ed80, 0x1e04cd43b70}}, {0xc000153458, ...})
    C:/Users/jesse/go/pkg/mod/tinygo.org/x/bluetooth@v0.10.0/gattc_windows.go:160 +0x168 fp=0xc0001533c0 sp=0xc0001530d8 pc=0x11029a8
main.(*Light).Connect(0xc0000aa040, {{{{0x67, 0xa3, 0x33, 0x33, 0x11, 0xcb}, 0x0}}, 0xffb4, {0x116ca60, ...}}, ...)
    C:/Work/Code/sacn-neewer-lite-go/src/light.go:124 +0x1011 fp=0xc000153788 sp=0xc0001533c0 pc=0x110a7b1
main.(*LightController).FindLightLoop.func1.1(0xc00009e0d8, {{{{0x67, 0xa3, 0x33, 0x33, 0x11, 0xcb}, 0x0}}, 0xffb4, {0x116ca60, ...}})
    C:/Work/Code/sacn-neewer-lite-go/src/light_controller.go:89 +0x290 fp=0xc000153848 sp=0xc000153788 pc=0x110ced0
tinygo.org/x/bluetooth.(*Adapter).Scan.func2(0x1e04ccec040, 0x1e04ccf0070, 0x1e04cd085d0)
    C:/Users/jesse/go/pkg/mod/tinygo.org/x/bluetooth@v0.10.0/gap_windows.go:160 +0x7e fp=0xc0001538b8 sp=0xc000153848 pc=0x110025e
github.com/saltosystems/winrt-go/windows/foundation.(*TypedEventHandler).Invoke(0x1e04ccec040, 0x1e04ccec040, 0x1e04ccf0070, 0x1e04cd085d0, 0x0, 0x1e04cd19a30, 0x7ffd06a48f72, 0x1e04ccec040, 0x1e04cd09130, 0x1e04cd08788, ...)
    C:/Users/jesse/go/pkg/mod/github.com/saltosystems/winrt-go@v0.0.0-20240509164145-4f7860a3bd2b/windows/foundation/typedeventhandler.go:114 +0x102 fp=0xc000153928 sp=0xc0001538b8 pc=0x10ef5a2
github.com/saltosystems/winrt-go/internal/delegate.invoke(0x1e04ccec040, 0x1e04ccf0070, 0x1e04cd085d0, 0x0, 0x1e04cd19a30, 0x7ffd06a48f72, 0x1e04ccec040, 0x1e04cd09130, 0x1e04cd08788, 0x7ffd0696def8)
    C:/Users/jesse/go/pkg/mod/github.com/saltosystems/winrt-go@v0.0.0-20240509164145-4f7860a3bd2b/internal/delegate/delegate.go:113 +0x15c fp=0xc0001539d8 sp=0xc000153928 pc=0x10ecf5c
runtime.call128(0x0, 0x11604f8, 0xc000153a98, 0x8, 0x8, 0x50, 0xc000153cd0)
    C:/Program Files/Go/src/runtime/asm_amd64.s:778 +0x5d fp=0xc000153a68 sp=0xc0001539d8 pc=0xff733d
runtime.callbackWrap(0xb2a9ff920)
    C:/Program Files/Go/src/runtime/syscall_windows.go:396 +0x266 fp=0xc000153e58 sp=0xc000153a68 pc=0xfda4c6
runtime.cgocallbackg1(0xfda260, 0xb2a9ff920, 0x0)
    C:/Program Files/Go/src/runtime/cgocall.go:442 +0x234 fp=0xc000153f10 sp=0xc000153e58 pc=0xf868f4
runtime.cgocallbackg(0xfda260, 0xb2a9ff920, 0x0)
    C:/Program Files/Go/src/runtime/cgocall.go:361 +0x127 fp=0xc000153f90 sp=0xc000153f10 pc=0xf86607
runtime.cgocallbackg(0xfda260, 0xb2a9ff920, 0x0)
    <autogenerated>:1 +0x2e fp=0xc000153fb8 sp=0xc000153f90 pc=0xffd76e
runtime.cgocallback(0x0, 0x0, 0x0)
    C:/Program Files/Go/src/runtime/asm_amd64.s:1084 +0xec fp=0xc000153fe0 sp=0xc000153fb8 pc=0xff89ec
runtime.goexit({})
    C:/Program Files/Go/src/runtime/asm_amd64.s:1700 +0x1 fp=0xc000153fe8 sp=0xc000153fe0 pc=0xff8c41