From 3fdd78df2eb4fc4d6918195dab3f5fec617e4acf Mon Sep 17 00:00:00 2001 From: forest Date: Mon, 4 Jan 2021 15:18:23 -0600 Subject: [PATCH] fix segfault in draw_plot_png_bytes --- capsulflask/metrics.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/capsulflask/metrics.py b/capsulflask/metrics.py index fc33b61..9d345b6 100644 --- a/capsulflask/metrics.py +++ b/capsulflask/metrics.py @@ -3,8 +3,9 @@ import matplotlib.pyplot as pyplot import matplotlib.dates as mdates from functools import reduce import requests -#import json +import json from datetime import datetime +from threading import Lock from io import BytesIO from flask import Blueprint from flask import current_app @@ -15,6 +16,7 @@ from werkzeug.exceptions import abort from capsulflask.db import get_model from capsulflask.auth import account_required +mutex = Lock() bp = Blueprint("metrics", __name__, url_prefix="/metrics") durations = dict( @@ -116,6 +118,7 @@ def get_plot_bytes(metric, capsulid, duration, size): return (502, None) series = prometheus_response.json()["data"]["result"] + if len(series) == 0: now_timestamp = datetime.timestamp(datetime.now()) series = [ @@ -129,13 +132,19 @@ def get_plot_bytes(metric, capsulid, duration, size): series[0]["values"] )) - plot_bytes = draw_plot_png_bytes(time_series_data, scale=scales[metric], size_x=sizes[size][0], size_y=sizes[size][1]) + mutex.acquire() + try: + plot_bytes = draw_plot_png_bytes(time_series_data, scale=scales[metric], size_x=sizes[size][0], size_y=sizes[size][1]) + finally: + mutex.release() return (200, plot_bytes) def draw_plot_png_bytes(data, scale, size_x=3, size_y=1): + #current_app.logger.info(json.dumps(data, indent=4, default=str)) + pyplot.style.use("seaborn-dark") fig, my_plot = pyplot.subplots(figsize=(size_x, size_y))