andriydruk / RxDNSSD

Android version of mDNSResponder
Apache License 2.0
297 stars 75 forks source link

MulticastLock under-locked and Too many open files #59

Open githubforzjm opened 5 years ago

githubforzjm commented 5 years ago

Hi, Andriy Druk First of all, thanks for your RxDNSSD open source library using Apple mDNS and the help you provided.

I have some questions that you need to confirm below: When there are 30 services on the network, I can find these services by your library. Then I call DNSSD.resolve and DNSSD.queryRecord to get service's ip and port. Finally, I use ip and port to establish connection with each service using SoketChannel and it works well.

But I have some problems and need you to confirm.

  1. Import your module to my project image

  2. Reference your library. catch6c89 or catch73b5

I have two issues about your library:

1. MulticastLock under-locked com.github.druk.dnssd.DNSSD
2. Too many Open Files

when I use implementation 'com.github.andriydruk:rx2dnssd:0.9.11' and I repeatly call DNSSD.browse() and DNSSDService.stop(), after a while crash occured java.lang.RuntimeException: MulticastLock under-locked com.github.druk.dnssd.DNSSD at android.net.wifi.WifiManager$MulticastLock.release(WifiManager.java:2465) at com.github.druk.dnssd.DNSSD.onServiceStopped(DNSSD.java:578) at com.github.druk.dnssd.DNSSDBindable.onServiceStopped(DNSSDBindable.java:44) at com.github.druk.dnssd.InternalDNSSDService.stop(InternalDNSSDService.java:21) at com.linkplay.bonjour.presenter.LPBonjourServiceManager.stopLPBonjourDiscovery(LPBonjourServiceManager.java:187) at com.linkplay.dnssd.DNSSDActivity.stop(DNSSDActivity.java:184) at com.linkplay.dnssd.DNSSDActivity.access$300(DNSSDActivity.java:24) at com.linkplay.dnssd.DNSSDActivity$5.run(DNSSDActivity.java:157) at java.lang.Thread.run(Thread.java:831)


I believe this is caused by WifiManager.MulticastLock multicastLock in DNSSD.java

The strange thing is this crash does not happend when I use implementation project(':rx2dnssd')



when I use implementation project(':rx2dnssd') and I repeatly call DNSSD.browse() and DNSSDService.stop(), after a while Too many open files error comes out when I establish connection with service using ip and port. The strange thing is this error does not happend when I use implementation 'com.github.andriydruk:rx2dnssd:0.9.11'

Is there any deference between implementation 'com.github.andriydruk:rx2dnssd:0.9.11' and implementation project(':rx2dnssd')? Because I found this: image

image

andriydruk commented 5 years ago

Hi @githubforzjm We have a problem with an unbalanced call at wifi multicast lock in 0.9.11. I'm now trying to fix it in master, but work hasn't finished yet.

For now, I would recommend the last stable version - 0.9.7. Please try this version with own wifi multicast-lock in your app.

andriydruk commented 5 years ago

Please check 0.9.12 build

githubforzjm commented 5 years ago

@andriydruk I use 0.9.12 build in my project and it still not work

use 0.9.7 has never seen this crash

java.lang.RuntimeException: MulticastLock under-locked com.github.druk.dnssd.DNSSD at android.net.wifi.WifiManager$MulticastLock.release(WifiManager.java:3390) at com.github.druk.dnssd.DNSSD.onServiceStopped(DNSSD.java:578) at com.github.druk.dnssd.DNSSDBindable.onServiceStopped(DNSSDBindable.java:44) at com.github.druk.dnssd.InternalDNSSDService.stop(InternalDNSSDService.java:21) at com.github.druk.dnssd.DNSSD.lambda$resolve$0(DNSSD.java:230) at com.github.druk.dnssd.-$$Lambda$DNSSD$c9RIVhhcCfgpYtyUpMg_ToIEu6c.run(Unknown Source:2) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)

andriydruk commented 5 years ago

Hm, it looks very weird. You stacktrace looks like from the previous version of RxDNSSD - 0.9.11.

InternalDNSSDService should call DNSSDBindable.onServiceStopped at line 24 screen shot 2019-01-25 at 10 13 58 am

At your stackrace this call at line 21, like in dnssd 0.9.11

Do you use gradle for dependency managment? Could you confirm that you have dnssd version 0.9.12 in your gradle dependency graph, please

githubforzjm commented 5 years ago

@andriydruk Thanks a lot! I clean my project and use 2 phones to test for about 2 hours and there was no crash.

But I found another serious issue with SAMSUNG SM-C7100.

I use this phone which can discovery the service on my office network, but when I turn off Wi-Fi and turn on Wi-Fi again I could not discovery any service.

I have tried to stop the discovery when the Wi-Fi was disconnected and start discovery again when the Wi-Fi reconnect to my office network, but it did not work, the only way is kill the app and reopen it.

In addition, I use network service discovery (supported by Google) and the same thing happened

I feel that there will be problems when wifi changes on Samsung phones.

Is there any solution to this situation?

Thanks.

image image

andriydruk commented 5 years ago

@githubforzjm hm, probably it can be an issue. Please try to add android.net.conn.CONNECTIVITY_CHANGE broadcast listener and add WifiManager.MulticastLock when wifi appear and release when disapear. If it helps you I will add this solution to the library.

githubforzjm commented 5 years ago

@andriydruk Thank you for your suggestion I have tried that but it did not work. I run your Service Browser on the Samsung mobile and the same thing happened.

andriydruk commented 5 years ago

@githubforzjm another problem can occur with Samsung - they broke bindable version from time to time. Last time this happened in Android 4.2.1 build. Maybe they did it again. Please try the embedded version with this device (0.9.13). It starts a little bit slower but should work at any device.

githubforzjm commented 5 years ago

@andriydruk Although service discovery is a little bit slower than previous version 0.9.12, but it looks like it solves the issue by using embedded version 0.9.13. (I have to called stop discovery when phone's Wi-Fi was disconnected and start discovery again when phone's Wi-Fi reconnect to my office network

But Service is still not found when I turn off wifi and turn on wifi immediately and the phone reconnects to my office network within a few seconds.

The log is as follows: 2019-02-13 16:35:47.312 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:35:50.549 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:35:50.551 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:35:50.553 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:35:50.555 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:35:50.557 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started


If I turn off wifi and turn on wifi after a few secons (about 4~5 seconds), service discovery is normal.

The log is as follows:
2019-02-13 16:34:05.431 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:34:12.240 4154-4217/com.linkplay.bonjour I/DNSSDEmbedded: finish with code: 4 2019-02-13 16:34:14.231 4154-7287/com.linkplay.bonjour I/DNSSDEmbedded: init 2019-02-13 16:34:14.237 4154-7287/com.linkplay.bonjour I/DNSSDEmbedded: start 2019-02-13 16:34:14.239 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:34:14.241 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:34:14.243 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: post exit 2019-02-13 16:34:14.244 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:34:14.331 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:34:14.334 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:34:14.573 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started 2019-02-13 16:34:14.590 4154-4154/com.linkplay.bonjour I/DNSSDEmbedded: already started

Look at code and there's Handler#postDelayed about 5 sencons to invoke a native method

image

image

andriydruk commented 5 years ago

I see, probably we should handle WIFI inside the library. For now, you can change this mStopTimerDelay through DNSSDEmbedded constructor. Please try to change it to 0.

https://github.com/andriydruk/RxDNSSD/blob/master/dnssd/src/main/java/com/github/druk/dnssd/DNSSDEmbedded.java#L42