Skip to content

gh-145272: Fix data race when accessing func_code in PyFunctionObject#145534

Open
Krishna-web-hub wants to merge 1 commit intopython:mainfrom
Krishna-web-hub:New_fix_clean
Open

gh-145272: Fix data race when accessing func_code in PyFunctionObject#145534
Krishna-web-hub wants to merge 1 commit intopython:mainfrom
Krishna-web-hub:New_fix_clean

Conversation

@Krishna-web-hub
Copy link
Contributor

@Krishna-web-hub Krishna-web-hub commented Mar 5, 2026

  • Fix a data race when accessing PyFunctionObject.func_code in free-threaded builds.

  • This patch replaces the direct read with _Py_atomic_load_ptr() and updates the setter to use _Py_atomic_exchange_ptr() so that updates to func_code are performed atomically.

  • The race was reproduced using ThreadSanitizer and verified to be fixed after this change.

}

return Py_NewRef(op->func_code);
PyCodeObject *code = _Py_atomic_load_ptr(&op->func_code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use PyFunction_GET_CODE() and fix PyFunction_GET_CODE() to use an atomic operation?

Maybe also only use atomic operation if the Py_GIL_DISABLED macro is defined.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants