Skip to content

Lua stack traces: a raising callback, package init or payload leaves no frames behind - #1554

Merged
Frotty merged 2 commits into
masterfrom
lua-stacktrace-depth-after-errors
Oct 24, 2026
Merged

Frotty merged 2 commits into
masterfrom
lua-stacktrace-depth-after-errors

Conversation

@Frotty

@Frotty Frotty commented Oct 24, 2026

Copy link
Copy Markdown
Member

With -stacktraces, a Lua callback, package init or hot reload payload which raises no longer leaves its frames on the stack-trace stack, so traces taken later show only their own frames.

The problem

StackTraceInjector2 makes every instrumented function push its call position on entry and pop it on a normal return. A Lua callback runs through an xpcall adapter (LuaTranslator.callbackAdapterFor), a package init through an xpcall with __wurst_init_error, and a hot reload payload through pcall(install). When the code raises, the unwind skips the pops: wurst_stack_depth stayed raised for the rest of the game, every later trace showed the failed frames below its own, and each further error added more. Reload hooks already put the depth back (#1551); nothing else did. Found by the review of #1551.

The change

  • A callback adapter saves the depth in a local before xpcall and writes it back after: one local read and one global write per call, only with -stacktraces. The error handler still sees the failing frames, as it runs before the unwind. A callback run synchronously inside another instrumented function (ForGroup, an extern pcall) returns to that function's depth, the depth on entering the adapter.
  • A package init writes the depth back when its xpcall fails, from one global (__wurst_initStackDepth): inits run one after another, and the init call has no local of its own (the 200-locals accounting).
  • __wurst_hotreload_apply saves and restores the depth around pcall(install), as the hook runner does around each hook (LuaTranslator.stackTraceDepth, shared by all three).
  • Without -stacktraces the emitted adapters, inits and runtime are byte-identical (callbackAdapterWithoutStacktracesKeepsNoDepth).
  • Jass is unchanged: it cannot catch, so a crashed Jass thread leaves the depth raised as well.

Both an ordinary error and the deliberate thread abort (I2S(1 div 0)) are covered: their handlers differ in what they print, but xpcall returns false for both and the depth is put back for both.

Evidence

Tests which fail before the change: raisingCallbackLeavesNoStackFramesBehind (an ordinary error and the abort inside a callback run through pcall in an instrumented function; the traces showed "when calling boom" and "via function reference" frames), raisingPackageInitLeavesNoStackFramesBehind (a raising and an aborting init, with its own driver, since the harness fails a run with a failed init), and failedInstallLeavesNoFramesBehind (a payload adds a package whose init raises; the trace after showed "when calling explode").

Unit tests, Windows: LuaBackendAuditTests (300), LuaTranslationTests (152), LuaHotReloadTests (121), DeterministicChecks (38), HotReloadPipelineTests (6). Linux is covered by PR CI; macOS is unverified. Not measured in the game: the change is to emitted Lua which the test runtime runs as the game would, and a trace's content is a diagnostic, not game state.

… behind

With -stacktraces, xpcall unwinds past the pops of a callback or package init that raises, so wurst_stack_depth stayed raised for the rest of the game and every later trace showed the failed frames. The callback adapter now saves the depth before xpcall and puts it back after; a package init puts it back when its xpcall fails. The error handlers still see the failing frames, as they run before the unwind. Builds without -stacktraces emit the adapters as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T14:32:12.848894Z 217c2da PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Frotty
Frotty merged commit 765c93e into master Oct 24, 2026
2 checks passed
@Frotty
Frotty deleted the lua-stacktrace-depth-after-errors branch November 25, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant