fix(coroutine): preserve base-frame tail-call yields - #40
Merged
Conversation
A Go function reached by a base-frame tail call was collapsed and then popped during yield, leaving an empty stack. Resume consequently reported successful completion with the argument window instead of a resumable yield. Keep a sole Go frame with a default resume continuation, classify completion from yieldState, and fail explicitly if any future path produces a yield without a frame. Cover direct, method, callable, stateless, user-yield, ResumeInto, root-Go, multi-result, and nested coroutine.resume shapes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the critical sole-frame yield loss reported by @butschster in:
https://gist.github.com/butschster/6a07d9628e2f46f7e9f14d3c62344f0c
Reproduction
On released
v1.5.18, a coroutine entry function ending in a yielding Go tail call:incorrectly returned
ResumeOKon the first resume, often exposing the argument window as successful results. Independent red-first cases also reproduced value loss throughcoroutine.wrap, at default/128/384 registry starts, after forced registry growth, and across repeated thread reuse.The tail call collapsed the Lua base frame, then
switchToParentThreadpopped the yielding Go frame. The empty stack madeResumereport successful completion even thoughyieldStatesaid the thread yielded.The stability audit also reproduced a separate lifecycle failure: a pre-canceled context made
Resume/ResumeIntoreturn an error while leaving the child installed asCurrentThread, linked to its parent, and incorrectly resumable.Fix
yieldStatebefore treating an empty stack as successful completion.Hardened coverage
return -1,L.Yield, and Luacoroutine.yield;*LFunctionand statelessLGoFunc;__call,coroutine.resume, andcoroutine.wrappaths;ResumeandResumeInto;Verification
v1.5.18;checkptr=2full suite;go vet ./...andgit diff --check;v1.5.18.Controlled benchmark results show no statistically significant timing regression:
BenchmarkTailCall: p=0.116;BenchmarkCoroutineYieldResume: p=0.775.Allocation counts are identical:
BenchmarkTailCall: 120 B/op, 3 allocs/op;BenchmarkCoroutineYieldResume: 10.30 KiB/op, 5 allocs/op.