Skip to content

Commit

Permalink
[FIX] registry: use a weak dictionary for model_cache to avoid memo…
Browse files Browse the repository at this point in the history
…ry leaks

The class attribute `model_cache` refers to model classes, which refer to their
own registry.  This cache potentially keeps all past registries alive!
  • Loading branch information
rco-odoo committed May 3, 2017
1 parent 8f1cc2f commit 77e7799
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion odoo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,7 @@ def _setup_base(self, partial):
self._add_magic_fields()
cls._proper_fields = set(cls._fields)

cls.pool.model_cache[cls._model_cache_key] = cls
cls.pool.model_cache[cls._model_cache_key] = cls

# 2. add custom fields
self._add_manual_fields(partial)
Expand Down
10 changes: 4 additions & 6 deletions odoo/modules/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections import Mapping, defaultdict, deque
from contextlib import closing
from operator import attrgetter
from weakref import WeakValueDictionary
import logging
import os
import threading
Expand All @@ -30,6 +31,9 @@ class Registry(Mapping):
_lock = threading.RLock()
_saved_lock = None

# a cache for model classes, indexed by their base classes
model_cache = WeakValueDictionary()

@lazy_classproperty
def registries(cls):
""" A mapping from database names to registries. """
Expand Down Expand Up @@ -177,12 +181,6 @@ def __setitem__(self, model_name, model):
""" Add or replace a model in the registry."""
self.models[model_name] = model

@lazy_classproperty
def model_cache(cls):
""" A cache for model classes, indexed by their base classes. """
# we cache 256 classes per registry on average
return LRU(cls.registries.count * 256)

@lazy_property
def field_sequence(self):
""" Return a function mapping a field to an integer. The value of a
Expand Down

0 comments on commit 77e7799

Please sign in to comment.