Skip to content

Commit 3d0d4ed

Browse files
committed
fix: 修复destroy时stream close出现的线程错误崩溃
1 parent 1b735db commit 3d0d4ed

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

android/JDCache/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
# https://developer.android.com/topic/libraries/support-library/androidx-rn
1717
groupId=com.jingdong.wireless.jdsdk
1818
artifactId=jdhybrid-cache
19-
version=1.0.0-beta-2
19+
version=1.0.0-beta-3
2020

android/JDCache/src/main/java/com/jd/jdcache/match/PreReadInputStream.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class PreReadInputStream(
5454
this.unreadStreamFinish = false
5555
}
5656

57+
fun isClosed(): Boolean {
58+
return closed.get()
59+
}
60+
5761
/**
5862
* Call this when you would like to start pre-read the stream
5963
*/

android/JDCache/src/main/java/com/jd/jdcache/match/impl/PreloadHtmlMatcher.kt

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.jd.jdcache.service.DelegateManager
1414
import com.jd.jdcache.service.base.*
1515
import com.jd.jdcache.util.*
1616
import com.jd.jdcache.util.CoroutineHelper.launchCoroutine
17+
import com.jd.jdcache.util.CoroutineHelper.runOnIo
1718
import com.jd.jdcache.util.JDCacheLog.d
1819
import com.jd.jdcache.util.JDCacheLog.e
1920
import com.jd.jdcache.util.UrlHelper.convertHeader
@@ -203,30 +204,54 @@ open class PreloadHtmlMatcher : JDCacheResourceMatcher() {
203204

204205
protected open fun geDownloadLocalResp() : JDCacheLocalResp?{
205206
return waitingChannel?.let {
206-
runBlocking {
207-
try {
208-
log { d(name, "Waiting for receiving pre-download html file.") }
209-
//等待下载完成
210-
withTimeout(2000L) {
211-
waitingChannel?.receive()
207+
if (!it.isClosedForReceive) {
208+
runBlocking {
209+
try {
210+
log { d(name, "Waiting for receiving pre-download html file.") }
211+
//等待下载完成
212+
withTimeout(2000L) {
213+
it.receive()
214+
}
215+
} catch (e: TimeoutCancellationException) {
216+
log { d(name, "Timeout in receiving pre-download html file.") }
217+
null
218+
} catch (e: Exception) {
219+
log { e(name, "Error in receiving pre-download html file, e = $e") }
220+
null
212221
}
213-
} catch (e: TimeoutCancellationException) {
214-
log { d(name, "Timeout in receiving pre-download html file.") }
215-
null
216-
} catch (e: Exception) {
217-
log { e(name, "Error in receiving pre-download html file, e = $e") }
218-
null
219222
}
223+
} else {
224+
null
220225
}
221226
}
222227
}
223228

224229
override fun onDestroy() {
225230
super.onDestroy()
226-
waitingChannel?.cancel()
227-
downloadTask?.cancel()
231+
waitingChannel?.let {
232+
it.cancel()
233+
waitingChannel = null
234+
}
235+
downloadTask?.let {
236+
it.cancel()
237+
downloadTask = null
238+
}
228239

229-
localResp?.fileStream?.close()
240+
val fileStream = localResp?.fileStream
241+
fileStream?.let {
242+
if (it !is PreReadInputStream || !it.isClosed()) {
243+
launchCoroutine {
244+
runOnIo {
245+
try {
246+
@Suppress("BlockingMethodInNonBlockingContext")
247+
it.close()
248+
} catch (e: Throwable) {
249+
log { e(name, e) }
250+
}
251+
}
252+
}
253+
}
254+
}
230255
localResp?.filename?.let { fileRepo?.deleteFile(it) }
231256
}
232257
}

0 commit comments

Comments
 (0)