From 02484f899a2b8201d0313531015273e8bdbf5ede Mon Sep 17 00:00:00 2001
From: Simon Lejeune <sle@openerp.com>
Date: Wed, 8 Jun 2016 10:53:54 +0200
Subject: [PATCH] [FIX] report: thread-safe barcode generation

A lock occurs when the user wants to print a report having multiple barcode while the server is
started in threaded-mode. The reason is that reportlab has to build a cache of the T1 fonts
before rendering a barcode (done in a C extension) and this part is not thread safe. We attempt
here to init the T1 fonts cache at the start-up of Odoo so that rendering of barcode in multiple
thread does not lock the server.

Another workaround was to set an explicit lock around the reportlab's function call, but it's
more dangerous as it may introduce undetectable lock between python and postgresql.

Thanks to @odony and @KangOl.

Fixes #12236
---
 addons/report/models/report.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/addons/report/models/report.py b/addons/report/models/report.py
index bdfb405a11cf..66ad1c9abb37 100644
--- a/addons/report/models/report.py
+++ b/addons/report/models/report.py
@@ -28,6 +28,17 @@ from pyPdf import PdfFileWriter, PdfFileReader
 from reportlab.graphics.barcode import createBarcodeDrawing
 
 
+# A lock occurs when the user wants to print a report having multiple barcode while the server is
+# started in threaded-mode. The reason is that reportlab has to build a cache of the T1 fonts
+# before rendering a barcode (done in a C extension) and this part is not thread safe. We attempt
+# here to init the T1 fonts cache at the start-up of Odoo so that rendering of barcode in multiple
+# thread does not lock the server.
+try:
+    createBarcodeDrawing('Code128', value='foo', format='png', width=100, height=100, humanReadable=1).asString('png')
+except Exception:
+    pass
+
+
 #--------------------------------------------------------------------------
 # Helpers
 #--------------------------------------------------------------------------
-- 
GitLab