-
Notifications
You must be signed in to change notification settings - Fork 66
Mutable attrs bypass _json_string cache, causing stale hashes and exports #2863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcorudolphflex
merged 1 commit into
develop
from
FXC-3374-mutable-attrs-bypass-json-string-cache-causing-stale-hashes-and-exports
Oct 8, 2025
Merged
Mutable attrs bypass _json_string cache, causing stale hashes and exports #2863
marcorudolphflex
merged 1 commit into
develop
from
FXC-3374-mutable-attrs-bypass-json-string-cache-causing-stale-hashes-and-exports
Oct 8, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 2 comments
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/base.py
|
7c96576
to
bb076d1
Compare
yaugenst-flex
requested changes
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @marcorudolphflex looks great! Just a minor nit on the changelog.
Oh and the commit should be tagged fix:
, not feat:
3d1da23
to
a0e50f4
Compare
yaugenst-flex
approved these changes
Oct 8, 2025
hashes and exports - _json_string is cached, but refreshed when attrs refresh - changed hashing method from sha256 to md5 Jira: FXC-3374
a0e50f4
to
13c9c41
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem:
_json_string is cached (tidy3d/components/base.py), but attrs stays mutable. If a model is hashed or serialized before mutating obj.attrs, the cached JSON is reused, so _hash_self() ignores the change and to_hdf5/JSON writes omit the new attrs.
Fixed:
_json_string is cached, but refreshed when attrs refresh
Greptile Overview
Updated On: 2025-10-02 09:22:33 UTC
Summary
This PR addresses a critical caching and hashing bug in `Tidy3dBaseModel` related to mutable `attrs` fields. The core issue was that the `_json_string` property was cached without considering changes to the mutable `attrs` dictionary, leading to stale cached values that could cause incorrect hashes and exports.The solution introduces a sophisticated dual-caching mechanism:
Separated hashing and JSON concerns: A new
_json_string_for_hashing
property excludesattrs
entirely to ensure hash stability, while the regular_json_string
property includesattrs
with proper cache invalidation.Smart cache invalidation: A new
cached_property_guarded
decorator monitors changes toattrs
through an_attrs_digest()
method that creates stable fingerprints of the attrs field using JSON encoding and SHA256 hashing.Consistent export behavior: The
to_hdf5()
method now uses the appropriate JSON string variant based on whether it's called for hashing purposes.This change preserves the intended mutability of
attrs
(allowingobj.attrs['foo'] = bar
) while ensuring that object hashes remain consistent for objects with the same core properties, regardless of metadata changes. The JSON serialization still properly reflects the current state including any attrs modifications through the guarded caching mechanism.Important Files Changed
Changed Files
tidy3d/components/base.py
tests/test_components/test_base.py
CHANGELOG.md
Confidence score: 4/5
CHANGELOG.md
categorization should be reviewed as this appears to be a bug fix rather than a feature changeSequence Diagram
Context used:
Rule from
dashboard
- Use changelog categories correctly: "Fixed" for bug fixes, "Changed" for modifications to existing f... (source)