You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] FastModuleType tp_setattro crashes on attribute deletion (Py_INCREF on NULL) #121923
comp:coreissues related to core part of tensorflowstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:bugBug
In tensorflow/python/util/fast_module_type.cc, the tp_setattro implementation unconditionally calls Py_INCREF(value) without checking if value is NULL. Per CPython's tp_setattro protocol, value is NULL when the operation is an attribute deletion (del module.attr). This causes a segfault.
FastModuleType.tp_setattro = [](PyObject* module, PyObject* name,
PyObject* value) -> int {
auto* fast_module = FastModuleObject::UncheckedCast(module);
PyObject* to_decref = nullptr;
Py_INCREF(value); // BUG: value is NULL when del module.attr!
Impact
Any del module.some_attribute on a TensorFlow module backed by FastModuleType (which includes tf.Module subclasses) will segfault the Python interpreter. This is trivially triggerable from user code.
Suggested Fix
FastModuleType.tp_setattro = [](PyObject* module, PyObject* name,
PyObject* value) -> int {
if (!value) {
// Attribute deletion — delegate to base typereturn PyModule_Type.tp_setattro(module, name, value);
}
// ... rest of existing code
Environment
TensorFlow version: current master
Affected: del tf_module.attribute on FastModule-backed objects
Hi @Venkat6871, I'd love to take this issue on and open a PR if it's not actively being coded.
While looking at the suggested fix, I noticed a deeper, latent architectural issue with FastModuleType.tp_setattro regarding cache coherency and exception safety:
Cache Divergence on Deletion:If we simply bypass the crash with if (!value), the deleted attribute is cleared from CPython's underlying __dict__, but it remains dirty inside fast_module->attr_map. On subsequent lookups, FastTpGetattro will hit the cache and incorrectly return the stale, deleted object instead of raising an AttributeError. We must explicitly evict the entry from attr_map during a deletion phase.
Exception Safety on Assignment Failure: Currently, tp_setattro updates attr_mapbefore calling PyObject_GenericSetAttr, and completely ignores its return code. If PyObject_GenericSetAttr fails (e.g., read-only attributes, descriptors, or OOM during dictionary adjustment), the internal cache and the underlying python object diverge. Even worse, returning 0 while a CPython error indicator is set can cause interpreter instability.
I have an exception-safe, transaction-style implementation ready that drops the lock appropriately before calling into CPython, safely handles cache eviction on del, and executes a rollback on assignment failure to preserve state synchronization.
Hi @Fuyugithub, Thank you for raising your concern here!
A fix for this issue has been proposed in Pull Request. Once it is merged, this issue will be resolved.
Hi @AshiteshSingh, Thank you for raising the PR to address this issue. We truly appreciate your initiative and the effort in contributing the fix.
Hi @Fuyugithub,
Thank you so much for filing this issue! We truly appreciate your support. At the moment, this request is not prioritized and mark it for community contributions. If you would like to contribute on a fix, please feel free to fork the repository and submit a pull request in accordance with the TensorFlow CONTRIBUTION GUIDELINES and link this issue in your PR to ensure the maintainers can easily review and collaborate.
To avoid duplicate work, please leave a comment here if you decide to take it on!
Hi @Fuyugithub,
The fix for this issue from PR #122534 has been imported and integrated into the TensorFlow master branch.
The fix updates FastModuleType.tp_setattro to correctly handle attribute deletion when value is nullptr, preventing the segmentation fault caused by Py_INCREF(NULL). It also keeps the attr_map cache consistent and includes tests covering attribute deletion and cache consistency. The change has been integrated into master, resolving the reported issue.
Thank you for reporting this issue and for helping improve TensorFlow!
comp:coreissues related to core part of tensorflowstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:bugBug
Activity
abdulwahabahmedkhanyusufzai commentedon Jul 2, 2026
Hi @Venkat6871, I'd love to take this issue on and open a PR if it's not actively being coded.
While looking at the suggested fix, I noticed a deeper, latent architectural issue with
FastModuleType.tp_setattroregarding cache coherency and exception safety:if (!value), the deleted attribute is cleared from CPython's underlying__dict__, but it remains dirty insidefast_module->attr_map. On subsequent lookups,FastTpGetattrowill hit the cache and incorrectly return the stale, deleted object instead of raising anAttributeError. We must explicitly evict the entry fromattr_mapduring a deletion phase.tp_setattroupdatesattr_mapbefore callingPyObject_GenericSetAttr, and completely ignores its return code. IfPyObject_GenericSetAttrfails (e.g., read-only attributes, descriptors, or OOM during dictionary adjustment), the internal cache and the underlying python object diverge. Even worse, returning0while a CPython error indicator is set can cause interpreter instability.I have an exception-safe, transaction-style implementation ready that drops the lock appropriately before calling into CPython, safely handles cache eviction on
del, and executes a rollback on assignment failure to preserve state synchronization.May I go ahead and submit the PR for this?
Kayyuri commentedon Jul 5, 2026
Hi @Fuyugithub, Thank you for raising your concern here!
A fix for this issue has been proposed in Pull Request. Once it is merged, this issue will be resolved.
Hi @AshiteshSingh, Thank you for raising the PR to address this issue. We truly appreciate your initiative and the effort in contributing the fix.
Kayyuri commentedon Sep 23, 2026
Hi @Fuyugithub,
Thank you so much for filing this issue! We truly appreciate your support. At the moment, this request is not prioritized and mark it for community contributions. If you would like to contribute on a fix, please feel free to fork the repository and submit a pull request in accordance with the TensorFlow CONTRIBUTION GUIDELINES and link this issue in your PR to ensure the maintainers can easily review and collaborate.
To avoid duplicate work, please leave a comment here if you decide to take it on!
Venkat6871 commentedon Oct 5, 2026
Hi @Fuyugithub,
The fix for this issue from PR #122534 has been imported and integrated into the TensorFlow
masterbranch.The fix updates
FastModuleType.tp_setattroto correctly handle attribute deletion whenvalueisnullptr, preventing the segmentation fault caused byPy_INCREF(NULL). It also keeps theattr_mapcache consistent and includes tests covering attribute deletion and cache consistency. The change has been integrated intomaster, resolving the reported issue.Thank you for reporting this issue and for helping improve TensorFlow!
github-actions commentedon Oct 13, 2026
This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.
github-actions commentedon Oct 21, 2026
This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.
google-ml-butler commentedon Oct 21, 2026
Are you satisfied with the resolution of your issue?
Yes
No