details:   https://code.tryton.org/tryton/commit/f1840545f4eb
branch:    default
user:      Cédric Krier <[email protected]>
date:      Wed Aug 12 17:57:01 2026 +0200
description:
        Register cache instance after being fully initialized

        Otherwise an instance can be accessed by another thread via the 
registry when
        it is still being initialized. And it may crash on missing attribute.

        Closes #15007
diffstat:

 trytond/trytond/cache.py |  12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diffs (29 lines):

diff -r 2b4682bac6b5 -r f1840545f4eb trytond/trytond/cache.py
--- a/trytond/trytond/cache.py  Wed Aug 19 00:14:52 2026 +0200
+++ b/trytond/trytond/cache.py  Wed Aug 12 17:57:01 2026 +0200
@@ -83,7 +83,15 @@
         self.portable_id = str(uuid4())
 
 
-class BaseCache(object):
+class _CacheMeta(type):
+    def __call__(cls, *args, **kwargs):
+        inst = super().__call__(*args, **kwargs)
+        assert inst._name not in BaseCache._instances, inst._name
+        BaseCache._instances[inst._name] = inst
+        return inst
+
+
+class BaseCache(metaclass=_CacheMeta):
     _instances = {}
     context_ignored_keys = {
         'client', '_request', '_check_access', '_skip_warnings',
@@ -112,8 +120,6 @@
             self.duration = dt.timedelta(**duration)
         else:
             self.duration = None
-        assert self._name not in self._instances, self._name
-        self._instances[self._name] = self
 
     @classmethod
     def stats(cls):

Reply via email to