Skip to content

Conversation

@chrfwow
Copy link
Contributor

@chrfwow chrfwow commented Dec 2, 2025

This PR

The new LayeredEvaluationContext can result in a StackOverflowException when a before hook returns the passed hook context. This would result in the LayeredEvaluationContext having a reference to itself in the hook context layer. As soon as toMap, toObjectMap or similar is called, an exception will be thrown.

Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
@chrfwow chrfwow requested review from a team as code owners December 2, 2025 16:27
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @chrfwow, 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 critical StackOverflowException that could arise from specific usage patterns of LayeredEvaluationContext within before hooks. By introducing a safeguard against self-referential context layering, the change enhances the stability and reliability of the evaluation context handling, ensuring that applications can safely use hooks without encountering unexpected runtime errors related to recursion.

Highlights

  • Fix StackOverflow: Addressed a StackOverflowException that could occur in LayeredEvaluationContext when a before hook returned the current hook context, leading to a recursive self-reference.
  • Prevent Recursive Contexts: Implemented a reference equality check in HookSupport.java to ensure that a LayeredEvaluationContext does not add itself as a layer, thereby preventing infinite recursion.
  • New Test Case: Introduced a new unit test in HookSupportTest.java to specifically verify that returning the current context from a hook no longer triggers a StackOverflowException.
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.

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 addresses a potential StackOverflowError that can occur in LayeredEvaluationContext when a before hook returns its input context, creating a recursive reference. The fix in HookSupport.java correctly prevents this by adding a reference equality check. A new test case has been added to HookSupportTest.java to verify this fix. My review includes a suggestion to improve the assertion in the new test to make it more explicit and idiomatic.

Comment on lines 135 to 137
layeredEvaluationContext.asObjectMap();

assertThatNoException();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The use of assertThatNoException() here is not idiomatic. It creates an assertion object that is not used, and the test relies on an uncaught exception to fail. A more explicit and robust way to assert that no exception is thrown is to use isThrownBy() with a lambda or method reference. This clearly states which part of the code is expected not to throw an exception.

Suggested change
layeredEvaluationContext.asObjectMap();
assertThatNoException();
assertThatNoException().isThrownBy(layeredEvaluationContext::asObjectMap);

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.66%. Comparing base (f5f595b) to head (3754f75).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1760      +/-   ##
============================================
+ Coverage     92.46%   93.66%   +1.20%     
- Complexity      597      602       +5     
============================================
  Files            55       55              
  Lines          1406     1406              
  Branches        154      154              
============================================
+ Hits           1300     1317      +17     
+ Misses           64       50      -14     
+ Partials         42       39       -3     
Flag Coverage Δ
unittests 93.66% <100.00%> (+1.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
var returnedContext = returnedEvalContext.get();
if (!returnedContext.isEmpty()) {
// yes, we want to check for reference equality here, this prevents recursive layered contexts
if (returnedContext != hookContext.getCtx() && !returnedContext.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (returnedContext != hookContext.getCtx() && !returnedContext.isEmpty()) {
if (!returnedContext.isEmpty() && returnedContext != hookContext.getCtx()) {

[Q] would java optimize this anyway or could this be a slight improvement checking for empty first?

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 2, 2025

Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
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.

2 participants