Android stack overflow using coroutine.

I am reading a very large file, which has a lot of while loops.

Since it is a timeout operation, I use a coroutine to perform this operation.

At first I thought it was a problem with the while loop, but the while loop will not keep pushing the stack without popping it.

Seeing the coroutine stack under the error log, I used Thread to execute this function, and found that no error was reported, and the location was caused by the resumeWith of coroutine.

Because the coroutine executes the suspend function in segments, the resumewith function is continuously called back according to the internal state machine.

11-19 11:19:14.451 28024 28352 E AndroidRuntime: Process: com.macoli.mockserver, PID: 28024
11-19 11:19:14.451 28024 28352 E AndroidRuntime: java.lang.StackOverflowError: stack size 1040KB
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at java.lang.String.indexOf(String.java:1711)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlin.text.StringsKt__StringsKt.indexOf(Strings.kt:1105)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlin.text.StringsKt__StringsKt.indexOf$default(Strings.kt:1101)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlin.text.StringsKt__StringsKt.contains(Strings.kt:1146)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlin.text.StringsKt__StringsKt.contains$default(Strings.kt:1144)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at com.macoli.mockserver.car.CarHandler.parseLog(CarHandler.kt:125)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at com.macoli.mockserver.car.CarHandler$startWebServer$1.invokeSuspend(CarHandler.kt:46)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
11-19 11:19:14.451 28024 28352 E AndroidRuntime:        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

Solution: upgrade the coroutine version

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")

reference:

https://github.com/Kotlin/kotlinx.coroutines/issues/2541

Guess you like

Origin blog.csdn.net/mldxs/article/details/127934720