Tryton-Cron loosing DB or context after a while

Has anyone encountered some issues running tryton-cron lately from a fresh install? I just happened to use version 6.0 and I am getting this weird error:

Traceback (most recent call last):
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/modelstorage.py", line 1807, in save
    transaction.set_user(user), \
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/transaction.py", line 196, in set_user
    self.context = self.context.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/transaction.py", line 149, in stop
    self.rollback()
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/transaction.py", line 249, in rollback
    self.connection.rollback()
AttributeError: 'NoneType' object has no attribute 'rollback'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/ir/cron.py", line 164, in run
    cron.run_once()
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/descriptors.py", line 31, in newfunc
    return self.func(owner, [instance], *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/modelview.py", line 775, in wrapper
    return func(cls, records, *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/modules/company/ir.py", line 107, in run_once
    super(Cron, cls).run_once([cron])
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/descriptors.py", line 33, in newfunc
    return self.func(owner, *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/modelview.py", line 775, in wrapper
    return func(cls, records, *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/ir/cron.py", line 141, in run_once
    getattr(Model, method)()
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/modules/account_cr_invoice/hacienda.py", line 1073, in process_posted_messages
    cls.send_doc(hms, enabled_companies)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/modelview.py", line 775, in wrapper
    return func(cls, records, *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/modules/account_cr_invoice/hacienda.py", line 1130, in send_doc
    with api() as session:
  File "/home/richi/work/dev-tryton/pip/fecr/fecr/api.py", line 401, in __enter__
    self.api.try_get_token()
  File "/home/richi/work/dev-tryton/pip/fecr/fecr/api.py", line 144, in try_get_token
    self._set_token(self.fetch_token())
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/modules/account_cr_invoice/hacienda.py", line 750, in fetch_token
    return self.request_new_token()
  File "/home/richi/work/dev-tryton/pip/fecr/fecr/api.py", line 250, in request_new_token
    self.save_token(token)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/modules/account_cr_invoice/hacienda.py", line 746, in save_token
    self.token_source.save()
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/descriptors.py", line 31, in newfunc
    return self.func(owner, [instance], *args, **kwargs)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/model/modelstorage.py", line 1817, in save
    cls.write(*sum(
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/transaction.py", line 136, in __exit__
    self.stop(type is None)
  File "/home/richi/work/dev-tryton/tryton_src/trytond/trytond/transaction.py", line 151, in stop
    self.database.put_connection(
AttributeError: 'NoneType' object has no attribute 'put_connection'

I really don’t know where the problem is, anyone has seen this issue before? It appears as if something related to the threads or context being set to None and tryton-cron doesn’t know it? Please note that at the first 10 minutes of starting tryton-cron, it works well, it is after 10 or 15 minutes, that this error appears. Tryton-cron appears not to crash, but after the error, it is not able to perform any tasks the error continues infinitively.

It looks like the module account_cr_invoice is reusing a Tryton model instance that comes from a older transaction.

Yes, you are right! I reviewed my code and found out that I had stored some library access tokens (fecr/api ) inside a regular Tryton model (Invoice digest) in a temporal cache (class dict).

The access tokens extend only the ModelSQL base class as they don’t interact in the user UI, but are needed in the invoice digest for certain operations; only loaded from DB when needed. There is a token for each company, hence when the cron processes tasks for multiple companies, this cache helps to speedup things a bit. What will be the correct way to do this? Should I store the tokens inside the context so to only benefit the operations within a transaction or should I not worry about caching the tokens at all and assume that Tryton internally will cache this object?

Many thanks in advanced for any advise!

You can cache just the id. Instantiate a record with an id cost almost nothing in Tryton and the fields are read only once per transaction.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.