Skip to content

Commit

Permalink
fix: 修复destroy时stream close出现的线程错误崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
kylelyk12 committed Jul 21, 2023
1 parent 1b735db commit 3d0d4ed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion android/JDCache/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
# https://developer.android.com/topic/libraries/support-library/androidx-rn
groupId=com.jingdong.wireless.jdsdk
artifactId=jdhybrid-cache
version=1.0.0-beta-2
version=1.0.0-beta-3

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class PreReadInputStream(
this.unreadStreamFinish = false
}

fun isClosed(): Boolean {
return closed.get()
}

/**
* Call this when you would like to start pre-read the stream
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.jd.jdcache.service.DelegateManager
import com.jd.jdcache.service.base.*
import com.jd.jdcache.util.*
import com.jd.jdcache.util.CoroutineHelper.launchCoroutine
import com.jd.jdcache.util.CoroutineHelper.runOnIo
import com.jd.jdcache.util.JDCacheLog.d
import com.jd.jdcache.util.JDCacheLog.e
import com.jd.jdcache.util.UrlHelper.convertHeader
Expand Down Expand Up @@ -203,30 +204,54 @@ open class PreloadHtmlMatcher : JDCacheResourceMatcher() {

protected open fun geDownloadLocalResp() : JDCacheLocalResp?{
return waitingChannel?.let {
runBlocking {
try {
log { d(name, "Waiting for receiving pre-download html file.") }
//等待下载完成
withTimeout(2000L) {
waitingChannel?.receive()
if (!it.isClosedForReceive) {
runBlocking {
try {
log { d(name, "Waiting for receiving pre-download html file.") }
//等待下载完成
withTimeout(2000L) {
it.receive()
}
} catch (e: TimeoutCancellationException) {
log { d(name, "Timeout in receiving pre-download html file.") }
null
} catch (e: Exception) {
log { e(name, "Error in receiving pre-download html file, e = $e") }
null
}
} catch (e: TimeoutCancellationException) {
log { d(name, "Timeout in receiving pre-download html file.") }
null
} catch (e: Exception) {
log { e(name, "Error in receiving pre-download html file, e = $e") }
null
}
} else {
null
}
}
}

override fun onDestroy() {
super.onDestroy()
waitingChannel?.cancel()
downloadTask?.cancel()
waitingChannel?.let {
it.cancel()
waitingChannel = null
}
downloadTask?.let {
it.cancel()
downloadTask = null
}

localResp?.fileStream?.close()
val fileStream = localResp?.fileStream
fileStream?.let {
if (it !is PreReadInputStream || !it.isClosed()) {
launchCoroutine {
runOnIo {
try {
@Suppress("BlockingMethodInNonBlockingContext")
it.close()
} catch (e: Throwable) {
log { e(name, e) }
}
}
}
}
}
localResp?.filename?.let { fileRepo?.deleteFile(it) }
}
}

0 comments on commit 3d0d4ed

Please sign in to comment.