ensure that cursors are closed in QueryProcessor.iterate()

By using try..finally here, we ensure the cursor cleanup is performed
even if the generator is not exhausted before being garbage collected.

Fixes https://pagure.io/koji/issue/2435
This commit is contained in:
Mike McLean 2020-08-12 11:30:56 -04:00
parent 3932ab89b6
commit 71a891f428

View file

@ -9158,28 +9158,30 @@ SELECT %(col_str)s
c = context.cnx.cursor()
c.execute(query, values)
c.close()
query = "FETCH %i FROM %s" % (chunksize, cname)
while True:
if as_list:
if self.transform is None:
buf = _fetchMulti(query, {})
try:
query = "FETCH %i FROM %s" % (chunksize, cname)
while True:
if as_list:
if self.transform is None:
buf = _fetchMulti(query, {})
else:
# if we're transforming, generate the dicts so the transform can modify
buf = _multiRow(query, self.values, fields)
buf = [self.transform(row) for row in buf]
# and then convert back to lists
buf = [[row[f] for f in fields] for row in buf]
else:
# if we're transforming, generate the dicts so the transform can modify
buf = _multiRow(query, self.values, fields)
buf = [self.transform(row) for row in buf]
# and then convert back to lists
buf = [[row[f] for f in fields] for row in buf]
else:
buf = _multiRow(query, {}, fields)
if self.transform is not None:
buf = [self.transform(row) for row in buf]
if not buf:
break
for row in buf:
yield row
c = context.cnx.cursor()
c.execute("CLOSE %s" % cname)
c.close()
buf = _multiRow(query, {}, fields)
if self.transform is not None:
buf = [self.transform(row) for row in buf]
if not buf:
break
for row in buf:
yield row
finally:
c = context.cnx.cursor()
c.execute("CLOSE %s" % cname)
c.close()
def executeOne(self, strict=False):
results = self.execute()