From 3e2f612f43e40ce74df20efb60e27712f0988e64 Mon Sep 17 00:00:00 2001 From: NGnius Date: Tue, 8 Oct 2019 20:45:49 -0400 Subject: [PATCH] Fix db connection leak --- rxsmserver/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/rxsmserver/__init__.py b/rxsmserver/__init__.py index 65e1134..4fe5b7b 100644 --- a/rxsmserver/__init__.py +++ b/rxsmserver/__init__.py @@ -19,6 +19,12 @@ def get_or_create_connection(): database_connections[thread_id] = dblib.connect(database_path) return database_connections[thread_id] +def close_connection(): + thread_id = get_ident() + if thread_id in database_connections: + database_connections[thread_id].close() + del(database_connections[thread_id]) + @app.route('/release', methods=['POST']) def create_or_modify_release(): try: @@ -47,6 +53,7 @@ def create_or_modify_release(): cursor.execute('UPDATE releases SET url=?, created_date=? WHERE version=? and platform=?', (url, int(time()), version, platform)) operation = 'update' db_connection.commit() + close_connection() return jsonify({'status':200, 'reason':'Version %s %sd successfully' % (version, operation)}), 200 @@ -78,9 +85,11 @@ def check_for_update(): current_version = req_json['version'] platform = req_json['platform'] except: + close_connection() return jsonify({'status':400, 'reason':'Invalid request; missing parameter or invalid JSON'}), 400 cursor.execute('SELECT version, url FROM releases WHERE platform=? ORDER BY created_date DESC', (platform,)) result = cursor.fetchone() + close_connection() if result is None: return jsonify({'status':404, 'reason':'Platform not found (%ss)' % (time()-start), 'url':'', 'out-of-date':False}), 404 elif result[0] != current_version: @@ -94,7 +103,7 @@ cursor = conn.cursor() cursor.execute(\ '''CREATE TABLE IF NOT EXISTS releases ( id INTEGER PRIMARY KEY, - version TEXT UNIQUE NOT NULL, + version TEXT NOT NULL, url TEXT NOT NULL, platform TEXT NOT NULL, created_date INTEGER NOT NULL