Skip to content

fix: must restore after the stack is captured#5669

Merged
fengmk2 merged 1 commit intonextfrom
fix-getCalleeFromStack
Oct 30, 2025
Merged

fix: must restore after the stack is captured#5669
fengmk2 merged 1 commit intonextfrom
fix-getCalleeFromStack

Conversation

@fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Oct 30, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved error stack trace capture timing to ensure more reliable error diagnostics and debugging information.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @fengmk2, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a timing-related bug in the stack trace capture utility. By adjusting the sequence of operations, it guarantees that the custom stack trace configuration remains active throughout the entire stack capture process, thereby preventing potential issues with the accuracy or completeness of the captured stack information.

Highlights

  • Stack Trace Restoration Timing: The restoration of Error.prepareStackTrace and Error.stackTraceLimit has been reordered to occur after the stack has been fully captured and processed. This change ensures that the custom stack trace preparation is not prematurely reverted, which could lead to incorrect or incomplete stack information.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 30, 2025

Walkthrough

The code now restores Error.prepareStackTrace and Error.stackTraceLimit after constructing the stack array, rather than before the loop. This ensures stack frame capture completes before restoration, with an explanatory inline comment added.

Changes

Cohort / File(s) Summary
Stack trace restoration timing
tegg/core/common-util/src/StackUtil.ts
Deferred restoration of Error configuration (prepareStackTrace and stackTraceLimit) to occur after stack frames are populated, with inline documentation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

  • Single file modification with straightforward code reordering
  • No new logic or dependencies introduced
  • Timing change is localized and isolated

Poem

🐰 Stack frames now capture true and clear,
Before we wave the config disappear!
Restoration waits, a patient dance,
Each trace recorded, given its chance! 📚✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "fix: must restore after the stack is captured" is specific and directly related to the main change in the changeset. The code modification moves the restoration of Error.prepareStackTrace and Error.stackTraceLimit from before the loop to after the stack frames are captured and populated into obj.stack. The title accurately captures this timing change with clear, concise language that would help a teammate understand the primary purpose of the fix when scanning git history.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-getCalleeFromStack

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages
Copy link

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: d8aaf05
Status: ✅  Deploy successful!
Preview URL: https://c75badb9.egg-cci.pages.dev
Branch Preview URL: https://fix-getcalleefromstack.egg-cci.pages.dev

View logs

@codecov
Copy link

codecov bot commented Oct 30, 2025

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.43%. Comparing base (f446181) to head (d8aaf05).
⚠️ Report is 1 commits behind head on next.

Files with missing lines Patch % Lines
tegg/core/common-util/src/StackUtil.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             next    #5669   +/-   ##
=======================================
  Coverage   87.43%   87.43%           
=======================================
  Files         561      561           
  Lines       10931    10931           
  Branches     1238     1238           
=======================================
+ Hits         9557     9558    +1     
+ Misses       1290     1289    -1     
  Partials       84       84           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cloudflare-workers-and-pages
Copy link

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: d8aaf05
Status: ✅  Deploy successful!
Preview URL: https://247c973c.egg-v3.pages.dev
Branch Preview URL: https://fix-getcalleefromstack.egg-v3.pages.dev

View logs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tegg/core/common-util/src/StackUtil.ts (1)

32-55: Consider wrapping restoration in a try-finally block.

If an exception occurs while iterating obj.stack (lines 42-50), the restoration code won't execute, leaving Error.prepareStackTrace and Error.stackTraceLimit in a modified state globally.

Consider this pattern:

     stacks = [];
     // capture the stack with raw Error.stackTraceLimit and Error.prepareStackTrace
     const rawStackTraceLimit = Error.stackTraceLimit;
     const rawPrepareStackTrace = Error.prepareStackTrace;
-    Error.prepareStackTrace = prepareObjectStackTrace;
-    Error.stackTraceLimit = frameCount;
-    const obj: { stack: NodeJS.CallSite[] } = {
-      stack: [],
-    };
-    Error.captureStackTrace(obj);
-    for (let callSite of obj.stack) {
-      stacks.push({
-        scriptName: callSite.getFileName() ?? '',
-        scriptId: callSite.getScriptHash() ?? '',
-        lineNumber: callSite.getLineNumber() ?? 1,
-        columnNumber: callSite.getColumnNumber() ?? 1,
-        functionName: callSite.getFunctionName() ?? '',
-      });
+    try {
+      Error.prepareStackTrace = prepareObjectStackTrace;
+      Error.stackTraceLimit = frameCount;
+      const obj: { stack: NodeJS.CallSite[] } = {
+        stack: [],
+      };
+      Error.captureStackTrace(obj);
+      for (let callSite of obj.stack) {
+        stacks.push({
+          scriptName: callSite.getFileName() ?? '',
+          scriptId: callSite.getScriptHash() ?? '',
+          lineNumber: callSite.getLineNumber() ?? 1,
+          columnNumber: callSite.getColumnNumber() ?? 1,
+          functionName: callSite.getFunctionName() ?? '',
+        });
+      }
+    } finally {
+      // restore the original Error.stackTraceLimit and Error.prepareStackTrace
+      // must restore after the stack is captured
+      Error.prepareStackTrace = rawPrepareStackTrace;
+      Error.stackTraceLimit = rawStackTraceLimit;
     }
-    // restore the original Error.stackTraceLimit and Error.prepareStackTrace
-    // must restore after the stack is captured
-    Error.prepareStackTrace = rawPrepareStackTrace;
-    Error.stackTraceLimit = rawStackTraceLimit;
   }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f446181 and d8aaf05.

📒 Files selected for processing (1)
  • tegg/core/common-util/src/StackUtil.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

**/*.ts: Prefer TypeScript and ESM: write sources and exports in .ts (ESM-first) rather than CommonJS
Use two-space indentation, trailing commas, and semicolons (Prettier/oxlint defaults)
Name files in lowercase with hyphens (e.g., loader-context.ts)
Name classes in PascalCase
Name functions and variables in camelCase
Re-export types thoughtfully to keep the public API stable

Files:

  • tegg/core/common-util/src/StackUtil.ts
tegg/**/src/**/*.{ts,tsx,js,mjs}

📄 CodeRabbit inference engine (tegg/CLAUDE.md)

Do not use CommonJS APIs (require, module.exports); author code as ESM

Files:

  • tegg/core/common-util/src/StackUtil.ts
tegg/**/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (tegg/CLAUDE.md)

Use .js extensions in ESM import specifiers in TypeScript source

Files:

  • tegg/core/common-util/src/StackUtil.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: Test (macos-latest, 24, 4/5)
  • GitHub Check: Test (ubuntu-latest, 24, 1/5)
  • GitHub Check: Test (ubuntu-latest, 22, 5/5)
  • GitHub Check: Test (windows-latest, 22, 3/5)
  • GitHub Check: Test (ubuntu-latest, 22, 3/5)
  • GitHub Check: Test (ubuntu-latest, 24, 5/5)
  • GitHub Check: Test (macos-latest, 22, 5/5)
  • GitHub Check: Test (macos-latest, 22, 1/5)
  • GitHub Check: Test (ubuntu-latest, 24, 4/5)
  • GitHub Check: Test (ubuntu-latest, 22, 4/5)
  • GitHub Check: Test (macos-latest, 22, 4/5)
  • GitHub Check: Test (windows-latest, 22, 4/5)
  • GitHub Check: Test bin (windows-latest, 22, 1/3)
  • GitHub Check: Test bin (ubuntu-latest, 22, 1/3)
  • GitHub Check: Test bin (windows-latest, 22, 2/3)
  • GitHub Check: Test bin (ubuntu-latest, 22, 0/3)
  • GitHub Check: Test bin (windows-latest, 22, 0/3)
  • GitHub Check: Test bin (ubuntu-latest, 22, 2/3)
  • GitHub Check: typecheck
🔇 Additional comments (1)
tegg/core/common-util/src/StackUtil.ts (1)

51-54: Fix correctly ensures stack capture completes before restoration.

The restoration timing is now correct. Error.prepareStackTrace is invoked lazily when obj.stack is first accessed in the loop (line 42), so restoration must occur after the loop completes. The explanatory comment makes the intent clear.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes a bug where Error.prepareStackTrace and Error.stackTraceLimit were restored too early, before the stack trace was actually accessed and processed. The change of moving the restoration logic after the stack processing loop is correct.

I've added one comment to suggest an improvement for robustness. Since the code modifies global Error properties, it's safer to wrap the logic in a try...finally block to ensure these properties are always restored, even if an error occurs during stack processing. This will prevent potential side effects in other parts of the application.

@fengmk2 fengmk2 merged commit 022af16 into next Oct 30, 2025
48 of 51 checks passed
@fengmk2 fengmk2 deleted the fix-getCalleeFromStack branch October 30, 2025 12:33
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