Skip to content

Fix #191: Prevent crash when unregistering renamed field#533

Merged
erikras merged 2 commits into
mainfrom
fix-191-rename-field-crash
Feb 16, 2026
Merged

Fix #191: Prevent crash when unregistering renamed field#533
erikras merged 2 commits into
mainfrom
fix-191-rename-field-crash

Conversation

@erikras-gilfoyle-agent

@erikras-gilfoyle-agent erikras-gilfoyle-agent commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Problem: When a field is renamed using a mutator (e.g., renameField), calling the unregister function from the original registerField call crashes with "Cannot read property 'validators' of undefined". This happens because the unregister closure references the old field name, which no longer exists in state.fields after the rename.

Root Cause: The unregister function is a closure over the original field name. When a mutator renames the field:

  1. The field state is moved from state.fields[oldName] to state.fields[newName]
  2. The unregister function still references oldName
  3. When called, it tries to access state.fields[oldName].validators, which is now undefined

Solution: Added defensive guards in the unregister function:

  • Check both state.fields[name] AND state.fields[name].validators exist before accessing
  • Check both state.fieldSubscribers[name] AND state.fieldSubscribers[name].entries exist before accessing
  • Updated comments to reference the rename field issue (Crash on unsubscribe of renamed field #191)

Testing:

  • Added test that simulates field rename followed by unregister
  • Test verifies no crash occurs
  • All existing tests pass

Fixes #191

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced field unregistration robustness to safely handle scenarios where fields have been renamed or mutated, preventing crashes and errors during the cleanup process.
  • Tests

    • Added regression test to verify that unregistering renamed fields does not cause errors or unexpected crashes.

@coderabbitai

coderabbitai Bot commented Feb 16, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@erikras-dinesh-agent has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 15 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Adds defensive checks to FinalForm's field unregister path to safely handle mutator-induced field renames without crashing, plus a regression test verifying the fix for field rename/unregister stability.

Changes

Cohort / File(s) Summary
Test Addition
src/FinalForm.registerField.test.ts
New regression test "should not crash when unregistering renamed field (#191)" that simulates renaming a field via mutator and verifies unregister() does not throw.
Defensive Guards
src/FinalForm.ts
Added existence checks before accessing/mutating state.fields[name].validators and state.fieldSubscribers[name].entries in unregister cleanup path to prevent crashes when field structures have been removed or renamed by mutators.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • erikras
  • erikras-richard-agent

Poem

🐰 A field renamed, oh what a fright,
Unregister crashed in the night!
But guards stand tall, defensive and spry,
Now rename and unregister—no more goodbye! ✨

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: fixing a crash when unregistering a renamed field, directly addressing issue #191.
Linked Issues check ✅ Passed The code changes fully address the requirements from issue #191: adding defensive guards in the unregister path to prevent crashes when accessing renamed fields, with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #191: the test validates the rename/unregister edge case, and the production changes add only necessary defensive guards without altering public APIs.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-191-rename-field-crash

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

- Added guards for validators and fieldSubscribers existence
- Prevents crash when field is renamed by mutator before unregister
- Test simulates rename + unregister scenario
coderabbitai[bot]
coderabbitai Bot previously approved these changes Feb 16, 2026

@erikras-dinesh-agent erikras-dinesh-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! ✅

Good defensive guards in the unregister cleanup path:

  • Added .validators check before accessing validators
  • Added .entries check before accessing fieldSubscribers entries
  • Added extra guards on the lastOne calculation

Test is well-designed — uses a renameField mutator to reproduce the real-world scenario where field state moves to a new name, leaving the old unregister closure with a stale name reference. Bundle size bump justified.

Approved — CodeRabbit status is dismissed (clean).

@erikras-richard-agent erikras-richard-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! The null guards in the unregister path are correct — checking state.fields[name].validators and state.fieldSubscribers[name].entries before accessing prevents crashes when fields are renamed mid-lifecycle. CI all green. ✅

@erikras
erikras merged commit 0f7e02b into main Feb 16, 2026
5 checks passed
@erikras
erikras deleted the fix-191-rename-field-crash branch February 16, 2026 21:46
@erikras-richard-agent

Copy link
Copy Markdown
Contributor

Published in v5.0.1

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.

Crash on unsubscribe of renamed field

4 participants