Skip to content

Commit 1758dbc

Browse files
authored
Fix 1453 (#1454)
* add test and fix for cache clearing bug * update test docstring and releasenotes
1 parent 80840f0 commit 1758dbc

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/releasehistory.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Please note that all releases prior to a version 1.0.0 are considered pre-releas
1515

1616
### Bug fixes
1717

18+
* #1454 Fixes a bug that causes cache operations to error under some circumstances.
1819
* #1435 Fixes a bug in which `Colletion.set_force_field_parameters` did not update force field parameters upon export to simulation engines.
1920

2021
## 0.5.1 - 2025-02-25

openff/interchange/_tests/test_issues.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,28 @@ def test_issue_1234_openmm(sage, valence_handler):
297297

298298
# energies should be different, since parameters are (wildly!) different
299299
assert abs(original_energy - new_energy) > 0.1
300+
301+
302+
def test_clear_caches_with_dead_weakref_proxy():
303+
"""Reproduce a bug where _clear_caches raises ReferenceError when dead
304+
weakref proxies exist in gc.get_objects(). See
305+
https://github.com/openforcefield/openff-interchange/issues/1453
306+
"""
307+
import gc
308+
import weakref
309+
310+
class _Dummy:
311+
pass
312+
313+
# Create a dead weakref proxy that remains tracked by gc
314+
obj = _Dummy()
315+
proxy = weakref.proxy(obj)
316+
container = [proxy] # noqa: F841
317+
del obj
318+
gc.collect()
319+
320+
molecule = MoleculeWithConformer.from_smiles("C")
321+
force_field = ForceField("openff-2.2.0.offxml")
322+
323+
# This raised ReferenceError before the fix
324+
Interchange.from_smirnoff(force_field=force_field, topology=[molecule])

openff/interchange/components/interchange.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,5 +1115,8 @@ def _clear_caches():
11151115
import gc
11161116

11171117
for obj in gc.get_objects():
1118-
if isinstance(obj, functools._lru_cache_wrapper) and obj.__module__.startswith("openff.interchange"):
1119-
obj.cache_clear()
1118+
try:
1119+
if isinstance(obj, functools._lru_cache_wrapper) and obj.__module__.startswith("openff.interchange"):
1120+
obj.cache_clear()
1121+
except ReferenceError:
1122+
continue

0 commit comments

Comments
 (0)