issue12119 [trytond] Transaction cache not always cleaned

This commit is contained in:
Raimon Esteve 2023-03-08 13:08:29 +01:00
parent 15dadd16fa
commit 57648a428b
3 changed files with 61 additions and 13 deletions

60
issue12119.diff Normal file
View file

@ -0,0 +1,60 @@
diff --git a/tryton/trytond/trytond/transaction.py b/tryton/trytond/trytond/transaction.py
index 5605838962..acd8982f55 100644
--- a/tryton/trytond/trytond/transaction.py
+++ b/tryton/trytond/trytond/transaction.py
@@ -2,8 +2,9 @@
# this repository contains the full copyright notices and license terms.
import logging
import time
-from collections import defaultdict
+from collections import defaultdict, deque
from threading import local
+from weakref import WeakValueDictionary
from sql import Flavor
@@ -67,19 +68,11 @@ class Transaction(object):
started_at = None
def __new__(cls, new=False):
- from trytond.cache import LRUDict
- from trytond.pool import Pool
transactions = cls._local.transactions
if new or not transactions:
instance = super(Transaction, cls).__new__(cls)
- instance.cache = LRUDict(
- _cache_transaction,
- lambda: LRUDict(
- _cache_model,
- lambda name: LRUDict(
- record_cache_size(instance),
- Pool().get(name)._record),
- default_factory_with_key=True))
+ instance.cache = WeakValueDictionary()
+ instance._cache_deque = deque(maxlen=_cache_transaction)
instance._atexit = []
transactions.append(instance)
else:
@@ -98,10 +91,21 @@ class Transaction(object):
return self._local.tasks
def get_cache(self):
+ from trytond.cache import LRUDict
+ from trytond.pool import Pool
keys = tuple(((key, self.context[key])
for key in sorted(self.cache_keys)
if key in self.context))
- return self.cache[(self.user, keys)]
+ cache = self.cache.setdefault(
+ (self.user, keys), LRUDict(
+ _cache_model,
+ lambda name: LRUDict(
+ record_cache_size(self),
+ Pool().get(name)._record),
+ default_factory_with_key=True))
+ # Keep last used cache references to allow to pre-fill them
+ self._cache_deque.append(cache)
+ return cache
def start(self, database_name, user, readonly=False, context=None,
close=False, autocommit=False):

2
series
View file

@ -38,4 +38,4 @@ issue11868.diff # [account] Add on delete CASCADE to lines and keywords of move
issue11582.diff # [trytond] Add authentication services
issue11582-sao.diff # [sao] Add authentication services
tmp_chache_issue.diff # [account_invoice] TEMPORALLY: Fix the 'cache' problem.
issue12119.diff # [trytond] Transaction cache not always cleaned

View file

@ -1,12 +0,0 @@
diff --git a/tryton/modules/account_invoice/invoice.py b/tryton/modules/account_invoice/invoice.py
index 8e81c27be6..c83868c8e4 100644
--- a/tryton/modules/account_invoice/invoice.py
+++ b/tryton/modules/account_invoice/invoice.py
@@ -1591,6 +1591,7 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):
if moves:
Move.save(moves)
cls.save(invoices)
+ invoices = cls.browse(invoices)
Move.post([i.move for i in invoices if i.move.state != 'posted'])
reconciled = []
to_print = []