Skip to content
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

bpo-46857: Fix refleak in OSError INIT_ALIAS() #31594

Merged
merged 2 commits into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions Lib/test/test_embed.py
Expand Up @@ -1657,15 +1657,11 @@ def test_no_memleak(self):
self.fail(f"unexpected output: {out!a}")
refs = int(match.group(1))
blocks = int(match.group(2))
self.assertEqual(refs, 0, out)
if not MS_WINDOWS:
# bpo-46417: Tolerate negative reference count which can occur because
# of bugs in C extensions. It is only wrong if it's greater than 0.
self.assertLessEqual(refs, 0, out)
self.assertEqual(blocks, 0, out)
else:
# bpo-46857: on Windows, Python still leaks 1 reference and 1
# memory block at exit.
self.assertLessEqual(refs, 1, out)
# bpo-46857: on Windows, Python still leaks 1 memory block at exit
self.assertIn(blocks, (0, 1), out)


Expand Down
10 changes: 4 additions & 6 deletions Objects/exceptions.c
Expand Up @@ -15,10 +15,10 @@


/* Compatibility aliases */
PyObject *PyExc_EnvironmentError = NULL;
PyObject *PyExc_IOError = NULL;
PyObject *PyExc_EnvironmentError = NULL; // borrowed ref
PyObject *PyExc_IOError = NULL; // borrowed ref
#ifdef MS_WINDOWS
PyObject *PyExc_WindowsError = NULL;
PyObject *PyExc_WindowsError = NULL; // borrowed ref
#endif


Expand Down Expand Up @@ -3647,10 +3647,8 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)

#define INIT_ALIAS(NAME, TYPE) \
do { \
Py_INCREF(PyExc_ ## TYPE); \
Py_XDECREF(PyExc_ ## NAME); \
PyExc_ ## NAME = PyExc_ ## TYPE; \
if (PyDict_SetItemString(mod_dict, # NAME, PyExc_ ## NAME)) { \
if (PyDict_SetItemString(mod_dict, # NAME, PyExc_ ## TYPE)) { \
return -1; \
} \
} while (0)
Expand Down
7 changes: 1 addition & 6 deletions Objects/object.c
Expand Up @@ -61,12 +61,7 @@ Py_ssize_t _Py_RefTotal;
Py_ssize_t
_Py_GetRefTotal(void)
{
PyObject *o;
Py_ssize_t total = _Py_RefTotal;
o = _PySet_Dummy;
if (o != NULL)
total -= Py_REFCNT(o);
return total;
return _Py_RefTotal;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is no longer needed, more the better, but I haven't been testing this change

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, this is the change that makes total refs 0!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, this is the change that makes total refs 0!

Without this change, _Py_GetRefTotal() returns -2 when the Python process is started, before calling Py_Initialiaze() (ex: if you put a breakpoint on the main() function).


void
Expand Down