]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Fix "RuntimeError: In set_text: Could not load glyph (error code 0x14)"
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 2 Sep 2021 12:28:15 +0000 (14:28 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 2 Sep 2021 12:28:15 +0000 (14:28 +0200)
app.py

diff --git a/app.py b/app.py
index f9785b87d4fadb2ec753b9f14115e3d14060e0b7..0ad32981d0484820d13b826bd20ed367a9db57b8 100644 (file)
--- a/app.py
+++ b/app.py
@@ -14,6 +14,7 @@ from datetime import date, timedelta, datetime, timezone
 from json import loads
 from requests import post
 import jinja2
+import threading
 
 matplotlib.use('Agg')
 
@@ -22,6 +23,11 @@ app = Flask(__name__)
 CORS(app, supports_credentials=True)
 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
 
+# Workaround for
+# https://github.com/matplotlib/matplotlib/issues/13723. Despite the
+# issue is closed, the problem is still there. See
+# https://github.com/matplotlib/matplotlib/issues/13723#issuecomment-761302131.
+plot_lock = threading.Lock()
 
 # Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
 # updated to our needs
@@ -206,7 +212,9 @@ def coffee_graph_flavors():
     else:
         ax.set_title("This week taste")
 
-    fig.savefig(b, format="svg", bbox_inches="tight")
+    with plot_lock:
+        fig.savefig(b, format="svg", bbox_inches="tight")
+        plt.close(fig)
     b.seek(0)
     return send_file(b, mimetype="image/svg+xml")
 
@@ -267,9 +275,10 @@ def coffee_graph_history():
         ax.set_title("This week total")
 
     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
-    fig.savefig(b, format="svg", bbox_inches="tight")
+    with plot_lock:
+        fig.savefig(b, format="svg", bbox_inches="tight")
+        plt.close(fig)
     b.seek(0)
-    plt.close(fig)
     return send_file(b, mimetype="image/svg+xml")