Skip to content

Commit e3cafeb

Browse files
bpo-41568: Fix refleaks in zoneinfo subclasses (pythonGH-21907)
* Fix refleak in C module __init_subclass__ This was leaking a reference to the weak cache dictionary for every ZoneInfo subclass created. * Fix refleak in ZoneInfo subclass's clear_cache The previous version of the code accidentally cleared the global ZONEINFO_STRONG_CACHE variable (and inducing `ZoneInfo` to create a new strong cache) on calls to a subclass's `clear_cache()`. This would not affect guaranteed behavior, but it's still not the right thing to do (and it caused reference leaks). (cherry picked from commit c3dd7e4) Co-authored-by: Paul Ganssle <paul@ganssle.io>
1 parent d6bdf6d commit e3cafeb

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Modules/_zoneinfo.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)
412412
}
413413

414414
clear_strong_cache(type);
415-
ZONEINFO_STRONG_CACHE = NULL;
416415
}
417416
else {
418417
PyObject *item = NULL;
@@ -2471,6 +2470,7 @@ clear_strong_cache(const PyTypeObject *const type)
24712470
}
24722471

24732472
strong_cache_free(ZONEINFO_STRONG_CACHE);
2473+
ZONEINFO_STRONG_CACHE = NULL;
24742474
}
24752475

24762476
static PyObject *
@@ -2525,6 +2525,7 @@ zoneinfo_init_subclass(PyTypeObject *cls, PyObject *args, PyObject **kwargs)
25252525
}
25262526

25272527
PyObject_SetAttrString((PyObject *)cls, "_weak_cache", weak_cache);
2528+
Py_DECREF(weak_cache);
25282529
Py_RETURN_NONE;
25292530
}
25302531

@@ -2616,8 +2617,7 @@ module_free()
26162617
Py_CLEAR(ZONEINFO_WEAK_CACHE);
26172618
}
26182619

2619-
strong_cache_free(ZONEINFO_STRONG_CACHE);
2620-
ZONEINFO_STRONG_CACHE = NULL;
2620+
clear_strong_cache(&PyZoneInfo_ZoneInfoType);
26212621
}
26222622

26232623
static int

0 commit comments

Comments
 (0)