Skip to content

Commit

Permalink
Reset WrapperCont.result when accessed to prevent double resumption w…
Browse files Browse the repository at this point in the history
…hen using no interceptor

Fixes #10
  • Loading branch information
kyay10 committed Jan 23, 2025
1 parent bfb3155 commit 58129ed
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions library/src/commonMain/kotlin/cloning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,25 @@ private tailrec fun <Start, First, End> SplitSeq<Start, First, End>.resumeWithIm
val exception = res.exceptionOrNull()
if (exception is SeekingStackException) exception.use(tail)
else tail.resumeWithImpl(res, false)
} else {
wrapper.result = hasBeenIntercepted
}
}
} else {
// TODO deduplicate
val wrapper = wrapperCont
val rest = rest!! as SplitSeq<Any?, *, *>
wrapper.seq = rest
wrapper.realContext = rest.context
if (isIntercepted) {
wrapper.result = hasBeenIntercepted
wrapper.endWaitingForValue()
frames.head().cont.intercepted().resumeWith(result)
} else {
wrapper.result = waitingForValue
wrapper.beginWaitingForValue()
frames.head().cont.resumeWith(result)
val res = wrapper.result
if (res != waitingForValue && res != hasBeenIntercepted) {
val exception = res.exceptionOrNull()
if (exception is SeekingStackException) exception.use(rest)
else rest.resumeWithImpl(res, false)
} else {
wrapper.result = hasBeenIntercepted
}
}
}
Expand Down Expand Up @@ -215,6 +212,17 @@ internal class WrapperCont<T>(seq: SplitSeq<T, *, *>, isWaitingForValue: Boolean
CoroutineContext {
var seq: SplitSeq<T, *, *>? = seq
var result: Result<T> = if (isWaitingForValue) waitingForValue else hasBeenIntercepted
get() = field.also { endWaitingForValue() }
private set

fun beginWaitingForValue() {
result = waitingForValue
}

fun endWaitingForValue() {
result = hasBeenIntercepted
}

var realContext = seq.context.unwrap()
set(value) {
field = value.unwrap()
Expand Down

0 comments on commit 58129ed

Please sign in to comment.