ohmae / touch-icon-extractor

Library for Java/Kotlin to extract touch (web clip) icon information from the website
MIT License
11 stars 3 forks source link

Strict Mode Violation: Closable Resource #1

Open charles-hennge opened 3 years ago

charles-hennge commented 3 years ago

When using TouchIconExtractor with a url under strict mode, it appears that a closable resource (a GzipSource?, HTTPURLConnection?) is not being properly closed and is potentially leaking.

Example Code:

val extractor = TouchIconExtractor()
val icons = TouchIconExtractor.fromPage(url, true)

Example Crash Log: StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877) at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286) at java.util.zip.Inflater.finalize(Inflater.java:407) at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289) at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276) at java.lang.Daemons$Daemon.run(Daemons.java:137) at java.lang.Thread.run(Thread.java:919) Caused by: java.lang.Throwable: Explicit termination method 'end' not called at dalvik.system.CloseGuard.open(CloseGuard.java:237) at java.util.zip.Inflater.(Inflater.java:122) at com.android.okhttp.okio.GzipSource.(GzipSource.java:64) at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30) at oj0.(SimpleHttpResponse.kt:18) at mj0.a(SimpleHttpClientAdapter.kt:31) at yi0.f(ExtractFromPage.kt:44) at yi0.g(ExtractFromPage.kt:25) at ej0.a(TouchIconExtractor.kt:79)

charles-hennge commented 3 years ago

Follow up:

It looks like the get() call in fetch here generates an HttpResponse object that needs to be closed after parsing the its content into a string.

https://github.com/ohmae/touch-icon-extractor/blob/4209f27087c8fd72de599a9249d755b0ee8fd515/touchicon/src/main/java/net/mm2d/touchicon/ExtractFromPage.kt#L44

ohmae commented 3 years ago

Thank you. But it doesn't reproduce in my environment. And I don't think there's anything wrong with the part you pointed out. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html

ohmae commented 3 years ago

Does it happen on any device? Please tell me the OS version and device name that occur Does it occur at any URL? Please tell me the URL that occur

charles-hennge commented 3 years ago

Hello, sorry for the late reply.

I tried to build a small sample app that demonstrates the problem but I am unable to find sample links that actually detect as having favicon/touch icons.

On further checking of the issue when it occurs inside of my code, it appears that its related to running TouchIconExtractor.fromPage() on multiple URLs in succession. It looks like users need to prevent concurrent execution of this particular method. That might be worth putting in the documentation if its not already there.

ohmae commented 3 years ago

First of all, I want you to provide information that can be reproduced.