Skip to content

Conversation

@SebastianWiz
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/3056792565/88583?viewId=8172236

Summary

The gform_field_value_uid filter was being called recursively when dynamically populating unique IDs in GP Nested Forms, resulting in memory exhaustion.

Here's Samuel's loom showing the issue: https://www.loom.com/share/123f6ad1e9ab4e3ab55ae7d1328cb835

Fix: Added recursion protection to prevent infinite filter calls

@coderabbitai
Copy link

coderabbitai bot commented Sep 4, 2025

Walkthrough

Adds a per-field, per-request re-entrancy guard to the gform_field_value_uid filter in gpuid-dynamic-population.php using a static processing map keyed by form and field IDs; short-circuits if already processing, otherwise marks, generates UID, unmarks, and returns the value.

Changes

Cohort / File(s) Summary
Re-entrancy guard for UID dynamic population
gp-unique-id/gpuid-dynamic-population.php
Introduced a static $processing array keyed by formId-fieldId to prevent re-entrant execution in gform_field_value_uid. If the key is already processing, the filter returns the existing value; otherwise it sets the flag, runs UID generation, then unsets the flag. No changes to UID generation logic or public API.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant WP as WordPress/GF
  participant Filter as gform_field_value_uid
  participant UID as UID Generator

  WP->>Filter: Request dynamic value (formId, fieldId)
  alt processing[key] exists
    Note over Filter: Re-entrancy detected
    Filter-->>WP: Return existing/short-circuited value
  else
    Filter->>Filter: Set processing[key] = true
    Filter->>UID: Generate UID (unchanged logic)
    UID-->>Filter: UID value
    Filter->>Filter: Unset processing[key]
    Filter-->>WP: Return UID
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled
  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 507d4e1 and 6878357.

📒 Files selected for processing (1)
  • gp-unique-id/gpuid-dynamic-population.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • gp-unique-id/gpuid-dynamic-population.php
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch seb/fix/88583-nested-forms-uid-memory-exhaustion

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link

github-actions bot commented Sep 4, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 6878357

Copy link

@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 (3)
gp-unique-id/gpuid-dynamic-population.php (3)

14-20: Make the guard exception-safe and the key robust (object vs array).

  • Wrap the guarded section in try/finally so the flag is always cleared even if an error occurs.
  • Derive the form/field IDs defensively to handle both GF_Field objects and arrays.

Apply this diff:

 static $processing = array();
-$key = $field->formId . '_' . $field->id;
+$form_id  = isset( $field->formId ) ? $field->formId : ( isset( $field['formId'] ) ? $field['formId'] : 0 );
+$field_id = isset( $field->id )     ? $field->id     : ( isset( $field['id'] )     ? $field['id']     : 0 );
+$key      = $form_id . '_' . $field_id;
 if ( isset( $processing[ $key ] ) ) {
   return $value;
 }
 $processing[ $key ] = true;
 
+try {
   // Update what type of unique ID you would like to generate. Accepts 'alphanumeric', 'numeric', or 'sequential'.
   $type_of_id = 'alphanumeric';
@@
   if ( function_exists( 'gp_unique_id' ) ) {
@@
     $value = $gw_uid;
   }
-}
-
-unset( $processing[ $key ] );
-return $value;
+  return $value;
+} finally {
+  unset( $processing[ $key ] );
+}

Also applies to: 38-38


29-36: Consider per-field caching instead of a single page-level cache.

static $gw_uid caches one UID for the entire request. If multiple fields (or forms) use the uid parameter on the same page, they’ll all get the same value. If that’s not intentional, cache by form/field key.

Proposed change (builds on the earlier $key/$form_id variables):

-  static $gw_uid                                  = false;
+  static $uid_cache                               = array();
@@
-  if ( ! $gw_uid ) {
-    $gw_uid = gp_unique_id()->get_unique( $field->formId, $field );
-  }
-  $value = $gw_uid;
+  if ( ! isset( $uid_cache[ $key ] ) ) {
+    $uid_cache[ $key ] = gp_unique_id()->get_unique( $form_id, $field );
+  }
+  $value = $uid_cache[ $key ];

Please confirm whether the current “one UID per page load” behavior is required for your use case (especially with Nested Forms). If not, the above avoids cross-field reuse.


4-4: Fix typos in comments and URL.

  • “documetnation” → “documentation”
  • “Paramater” → “Parameter”
  • Update the docs URL path spelling.

Apply this diff:

- * https://gravitywiz.com/documetnation/gravity-forms-unique-id/
+ * https://gravitywiz.com/documentation/gravity-forms-unique-id/
@@
- * 4. Set "uid" as the value for the Paramater Name setting (screenshot: https://gwiz.io/2HhtBTa).
+ * 4. Set "uid" as the value for the Parameter Name setting (screenshot: https://gwiz.io/2HhtBTa).

Also applies to: 10-10

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled
  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ca433bf and 507d4e1.

📒 Files selected for processing (1)
  • gp-unique-id/gpuid-dynamic-population.php (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
gp-unique-id/gpuid-dynamic-population.php (1)
gravity-forms/gw-calculated-shipping.php (1)
  • field (91-117)
🔇 Additional comments (1)
gp-unique-id/gpuid-dynamic-population.php (1)

14-20: Good re-entrancy guard — fixes the infinite recursion.

The static processing map with a per-form/field key correctly short-circuits recursive calls and prevents the memory exhaustion. Nice, focused fix.

@SebastianWiz SebastianWiz changed the title Fixed an issue where dynamically populating Unique ID in Nested Forms caused memory exhaustion. gpuid-dynamic-population.php: Fixed an issue where dynamically populating Unique ID in Nested Forms caused memory exhaustion. Sep 4, 2025
…lating Unique ID in Nested Forms caused memory exhaustion.
@SebastianWiz SebastianWiz force-pushed the seb/fix/88583-nested-forms-uid-memory-exhaustion branch from 507d4e1 to 6878357 Compare September 4, 2025 05:23
@spivurno
Copy link
Contributor

spivurno commented Sep 4, 2025

@SebastianWiz Could you tell me more about how the recursion was occuring?

@SebastianWiz
Copy link
Contributor Author

@spivurno During debugging I could see that the static variable $gw_uid that tracks if a UID was already generated, is empty when submitting the parent form. My guess is because the parent form submissions happens in a new context. It would then try to generate a new UID, causing gp_unique_id()->get_unique to be called recursively in an infinite loop

@spivurno
Copy link
Contributor

spivurno commented Sep 4, 2025

Ah, got it. Thanks for the extra details!

@SebastianWiz SebastianWiz merged commit e5ae702 into master Sep 5, 2025
4 checks passed
@SebastianWiz SebastianWiz deleted the seb/fix/88583-nested-forms-uid-memory-exhaustion branch September 5, 2025 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants