Locking error on ir_sequence_strict

Hello,

sometimes, we get the following error:

Mon Jul 22 09:50:04 2019] CRITICAL:trytond.worker:task “22380” failed
File “/usr/local/lib/python3.5/dist-packages/trytond/modules/account_invoice/invoice.py”, line 1364, in post
cls.set_number(invoices)
File “/usr/local/lib/python3.5/dist-packages/trytond/modules/account_invoice_history/invoice.py”, line 31, in set_number
super(Invoice, cls).set_number(invoices)
File “/usr/local/lib/python3.5/dist-packages/trytond/modules/account_invoice/invoice.py”, line 995, in set_number
invoice.number = invoice.get_next_number()
File “/usr/local/lib/python3.5/dist-packages/trytond/modules/account_invoice/invoice.py”, line 1036, in get_next_number
return Sequence.get_id(sequence.id)
File “/usr/local/lib/python3.5/dist-packages/trytond/ir/sequence.py”, line 376, in get_id
return super(SequenceStrict, cls).get_id(clause, _lock=True)
File “/usr/local/lib/python3.5/dist-packages/trytond/ir/sequence.py”, line 355, in get_id
cursor.execute(*query)
File “/usr/local/lib/python3.5/dist-packages/trytond/backend/postgresql/database.py”, line 65, in execute
cursor.execute(self, sql, args)
psycopg2.OperationalError: could not obtain lock on row in relation “ir_sequence_strict”

Actual, I have no clue how to solve this.

Thank you and best regards,
Hendrik

The error means that there is another transaction (so another using) posting an invoice with the same invoice sequence. This is because the invoice numbering is unique and it should always return consecutive numbers.

Normally waiting the other user to finish posting the transations should fix the issue.

Thank you for the fast answer. My problem is, that I cannot wait for another user, because I do not know, when a user starts the post-action. ( Because of the actual implementation, where we post the invoice immediatly after sending the shipment ).

I looked inside the implementation of the get_id method and I asked myself, if it could be a solution like it is in SAP for example: There, a lock will initiated and if a current lock exists, the system waits some seconds and after this, it retry the lock - until it has been successful or a counter of failed attemps has been reached.

Could this solve my problem?

Then you should proably post the invoice using the task queue and use a single worker for each invoice.

So the invoices will be posted in background by a single process.

The worker is already doing that: http://hg.tryton.org/trytond/file/default/trytond/worker.py#l103
You may need to increase the number of retries if you have a lot of contention.