Skip to content

Commit

Permalink
gw: fix sqlite3.Connection leak
Browse files Browse the repository at this point in the history
Connection context manager behavior
operates on a transaction.
sqlite3.Connection.__exit__() does
not .close() .
  • Loading branch information
mdavidsaver committed Jan 31, 2024
1 parent a03400e commit 70b030d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/p4p/gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import re
import sqlite3
from contextlib import closing

from functools import wraps, reduce

Expand Down Expand Up @@ -171,7 +172,8 @@ def __init__(self, statsdb=None):
self.statsdb = self.__tempdb.name
_log.debug("Using temporary stats db: %s", self.statsdb)

with sqlite3.connect(self.statsdb) as C:
# open .db and manage transaction
with closing(sqlite3.connect(self.statsdb)) as C, C:
C.executescript("""
DROP TABLE IF EXISTS us;
DROP TABLE IF EXISTS usbyname;
Expand Down Expand Up @@ -311,7 +313,7 @@ def update_stats(self, norm):
T0 = time.time()
self.refsPV.post(listRefs())

with sqlite3.connect(self.statsdb) as C:
with closing(sqlite3.connect(self.statsdb)) as C, C:
C.executescript('''
DELETE FROM us;
DELETE FROM ds;
Expand Down

0 comments on commit 70b030d

Please sign in to comment.