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
100 stars 43 forks source link

java.net.SocketTimeoutException: Poll timed out leads to crash on android few seconds after discovering service #82

Closed nassbe closed 5 months ago

nassbe commented 5 months ago

Describe the bug I had to upgrade my flutter version (now 3.16.8) to be able to use connectivity_plus 5.0.0 (that was necessary to avoid an instant crash on iOS) together with bonsoir, which had to be upgraded to at least 4.0.0. If i use these versions on my android phone (android 12) i always run into the same error 9-10 seconds after the service is found:

E/AndroidRuntime(24385): FATAL EXCEPTION: DefaultDispatcher-worker-2 E/AndroidRuntime(24385): Process: com.example.bonsoirtest, PID: 24385 E/AndroidRuntime(24385): java.net.SocketTimeoutException: Poll timed out E/AndroidRuntime(24385): at libcore.io.IoBridge.poll(IoBridge.java:866) E/AndroidRuntime(24385): at java.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:151) E/AndroidRuntime(24385): at java.net.PlainDatagramSocketImpl.receive0(PlainDatagramSocketImpl.java:142) E/AndroidRuntime(24385): at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:164) E/AndroidRuntime(24385): at java.net.DatagramSocket.receive(DatagramSocket.java:849) E/AndroidRuntime(24385): at fr.skyost.bonsoir.discovery.TXTRecord$Companion.resolveTXTRecord(TXTRecord.kt:80) E/AndroidRuntime(24385): at fr.skyost.bonsoir.discovery.TXTRecord$Companion.resolveTXTRecord$default(TXTRecord.kt:55) E/AndroidRuntime(24385): at fr.skyost.bonsoir.discovery.BonsoirServiceDiscovery$queryTXTRecord$1.invokeSuspend(BonsoirServiceDiscovery.kt:133) E/AndroidRuntime(24385): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) E/AndroidRuntime(24385): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) E/AndroidRuntime(24385): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) E/AndroidRuntime(24385): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) E/AndroidRuntime(24385): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) E/AndroidRuntime(24385): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) I/Process (24385): Sending signal. PID: 24385 SIG: 9

This leads to an instant crash of my app. I tried upgrading bonsoir up to the latest version but that didn't change anything.

To Reproduce Steps to reproduce the behavior: Use the versions as described before and start service discovery in your app: _bonsoirDiscovery = BonsoirDiscovery(type: "type"); await _bonsoirDiscovery!.ready; var eventStream = _bonsoirDiscovery!.eventStream; if (eventStream!=null) { _bonsoirDiscovery!.eventStream!.listen(_onEventOccurred); try { await _bonsoirDiscovery!.start(); }

Wait for 9-10 seconds and the app crashes.

Expected behavior App should keep running normally.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Skyost commented 5 months ago

Well, it's an error related to how we query TXT records on Android (see https://github.com/Skyost/Bonsoir/blob/master/packages/bonsoir_android/android/src/main/kotlin/fr/skyost/bonsoir/discovery/TXTRecord.kt#L56). This should not lead to a crash, even if it fails.

nassbe commented 5 months ago

Ok, i did a few more experiments with the android app that crashes and my mock server that uses bonsoir as well for the service broadcast. I can define the error now more precisely: My bonsoir service had a name that includes a dot like BonsoirService service = BonsoirService( name: "the.name", .... This service is found, but leads to a crash after 9-10 seconds on an android device. That can be reproduced with any other name that contains a dot. This problem doesn't exist in bonsoir versions <4.0.0. There is also an error in the iOS version, but it gets caught there. This leads to the error that the service can't be found, but at least there is no app crash.

So basically this is just an error with resolving a service name that contains a dot. Maybe not too important, but the app crash is a bit annoying :)

Skyost commented 5 months ago

Thanks for the details. So it doesn't crash if there is no dot in the service name ?

nassbe commented 5 months ago

Yes, everything works perfectly normal if there is no dot in the service name. So i just changed the name in my mock server and all problems are gone :D

Skyost commented 5 months ago

This should now be fixed. Feel free to try and tell me :wink:

nassbe commented 4 months ago

I now get a message for the wrong service identifier from Bonsoir as i would expect it. Thanks 😄