Skip to content

[BUG] FastModuleType tp_setattro crashes on attribute deletion (Py_INCREF on NULL) #121923

Closed

Description

@Fuyugithub

Bug Description

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.

Location

tensorflow/python/util/fast_module_type.cc:382-384

Code

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 type
        return 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

Activity

  1. added
    comp:core
    on Jun 26, 2026
  2. abdulwahabahmedkhanyusufzai commented on Jul 2, 2026

    @abdulwahabahmedkhanyusufzai
    Contributor

    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:

    1. 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.
    2. Exception Safety on Assignment Failure: Currently, tp_setattro updates attr_map before 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.

    May I go ahead and submit the PR for this?

  3. Kayyuri commented on Jul 5, 2026

    @Kayyuri

    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.

  4. removed
    awaiting PR merge
    on Sep 14, 2026
  5. Kayyuri commented on Sep 23, 2026

    @Kayyuri

    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!

  6. added
    stat:contribution welcome
    on Sep 23, 2026
  7. removed
    stat:contribution welcome
    on Oct 5, 2026
  8. Venkat6871 commented on Oct 5, 2026

    @Venkat6871
    Contributor

    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!

  9. added
    stat:awaiting response
    on Oct 5, 2026
  10. github-actions commented on Oct 13, 2026

    @github-actions

    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.

  11. added
    stale
    on Oct 13, 2026
  12. github-actions commented on Oct 21, 2026

    @github-actions

    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.

  13. google-ml-butler commented on Oct 21, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp:corestalestat:awaiting responsetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions