Skip to content

Commit

Permalink
[sqlserver] properly close cursor, avoid leaks
Browse files Browse the repository at this point in the history
We saw memory leak issues with that check. It could have been caused by
two different things:

* The fact that we are not closing cursors properly, and therefore not
  freeing properly the recordsets loaded in them
* We instantiated wrongly two different cursors in the main check loop
  letting one leak...
  • Loading branch information
LeoCavaille committed May 19, 2015
1 parent 950a7b6 commit d809e5b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion checks.d/sqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def get_sql_type(self, instance, counter_name):
except Exception, e:
self.log.warning("Could not get counter_name of base for metric: %s", e)

self.close_cursor(cursor)

return sql_type, base_name

def check(self, instance):
Expand All @@ -241,13 +243,25 @@ def check(self, instance):
custom_tags = instance.get('tags', [])
instance_key = self._conn_key(instance)
metrics_to_collect = self.instances_metrics[instance_key]
cursor = self.get_cursor(instance)
for metric in metrics_to_collect:
try:
metric.fetch_metric(cursor, custom_tags)
except Exception, e:
self.log.warning("Could not fetch metric %s: %s" % (metric.datadog_name, e))

self.close_cursor(cursor)

def close_cursor(self, cursor):
"""
We close the cursor explicitly b/c we had proven memory leaks
We handle any exception from closing, although according to the doc:
"in adodbapi, it is NOT an error to re-close a closed cursor"
"""
try:
cursor.close()
except Exception as e:
self.log.warning("Could not close adodbapi cursor\n{0}".format(e))


class SqlServerMetric(object):
'''General class for common methods, should never be instantiated directly
Expand Down

0 comments on commit d809e5b

Please sign in to comment.