-
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
base: develop
Are you sure you want to change the base?
Mutable attrs bypass _json_string cache, causing stale hashes and exports #2863
Conversation
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
CHANGELOG.md
Outdated
|
||
### Changed | ||
- Improved performance of antenna metrics calculation by utilizing cached wave amplitude calculations instead of recomputing wave amplitudes for each port excitation in the `TerminalComponentModelerData`. | ||
- Fixed bug for `Tidy3dBaseModel`: Hashes are independent from `attrs`. Still `.json_string` does include them and is cached, but cache is updated if `attrs` are updated. |
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.
style: This entry describes a bug fix but is placed in the 'Changed' section instead of 'Fixed'. According to changelog conventions, bug fixes should go under 'Fixed'.
Context Used: Rule from dashboard
- Use changelog categories correctly: "Fixed" for bug fixes, "Changed" for modifications to existing f... (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: CHANGELOG.md
Line: 13:13
Comment:
**style:** This entry describes a bug fix but is placed in the 'Changed' section instead of 'Fixed'. According to changelog conventions, bug fixes should go under 'Fixed'.
**Context Used:** Rule from `dashboard` - Use changelog categories correctly: "Fixed" for bug fixes, "Changed" for modifications to existing f... ([source](https://app.greptile.com/review/custom-context?memory=17997812-233f-4a81-bee2-4d7666605061))
How can I resolve this? If you propose a fix, please make it concise.
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/base.py
|
…hashes and exports - _json_string is cached, but refreshed when attrs refresh Jira: FXC-3374
7c96576
to
bb076d1
Compare
…hashes and exports - change hashing to md5 Jira: FXC-3374
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)