mirror of
https://github.com/sqlmapproject/sqlmap.git
synced 2026-11-19 22:50:54 +00:00
Close leaked sqlite3 connections (ResourceWarning on Python 3.13+) (#6181)
Python 3.13+ reports sqlite3 connections that are garbage collected without being closed. The unit tests print a run of "ResourceWarning: unclosed database" lines on Linux/CPython 3.14: - SQLAlchemy.close() only returned its connection to the engine's pool, so the underlying DBAPI connection (e.g. sqlite3 for '-d sqlite://...') stayed open. Dispose the engine on close. - vulnTest() used "with sqlite3.connect(...)", which commits but does not close the connection. Use contextlib.closing(). - test_multibit and test_sqlalchemy never closed what they opened. Co-authored-by: PiyushSatyarthi <244159432+PiyushSatyarthi@users.noreply.github.com>
This commit is contained in:
parent
eca9ff2e13
commit
cb5216564a
4 changed files with 31 additions and 6 deletions
|
|
@ -5,6 +5,7 @@ Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
|
|||
See the file 'LICENSE' for copying permission
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import doctest
|
||||
import json
|
||||
import logging
|
||||
|
|
@ -206,9 +207,11 @@ def vulnTest(tests=None, label="vuln"):
|
|||
os.close(handle)
|
||||
cleanups.append(database)
|
||||
|
||||
with sqlite3.connect(database) as conn:
|
||||
# Note: sqlite3's own context manager only commits (it does not close the connection)
|
||||
with contextlib.closing(sqlite3.connect(database)) as conn:
|
||||
c = conn.cursor()
|
||||
c.executescript(vulnserver.SCHEMA)
|
||||
conn.commit()
|
||||
|
||||
handle, request = tempfile.mkstemp(suffix=".req")
|
||||
os.close(handle)
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ class SQLAlchemy(GenericConnector):
|
|||
|
||||
self.dialect = dialect
|
||||
self.address = conf.direct
|
||||
self.engine = None
|
||||
|
||||
if conf.dbmsUser:
|
||||
self.address = self.address.replace("'%s':" % conf.dbmsUser, "%s:" % _urllib.parse.quote(conf.dbmsUser))
|
||||
|
|
@ -84,6 +85,7 @@ class SQLAlchemy(GenericConnector):
|
|||
else:
|
||||
engine = _sqlalchemy.create_engine(self.address, connect_args={})
|
||||
|
||||
self.engine = engine
|
||||
self.connector = engine.connect()
|
||||
except (TypeError, ValueError) as ex:
|
||||
if "_get_server_version_info" in traceback.format_exc():
|
||||
|
|
@ -110,6 +112,19 @@ class SQLAlchemy(GenericConnector):
|
|||
else:
|
||||
raise SqlmapMissingDependence("SQLAlchemy not available (e.g. 'pip%s install SQLAlchemy')" % ('3' if six.PY3 else ""))
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
GenericConnector.close(self)
|
||||
finally:
|
||||
# Note: closing the connection only returns it to the engine's pool (the underlying DBAPI
|
||||
# connection, e.g. sqlite3, stays open) - disposing the engine is what actually releases it
|
||||
if self.engine is not None:
|
||||
try:
|
||||
self.engine.dispose()
|
||||
except Exception as ex:
|
||||
logger.debug(getSafeExString(ex))
|
||||
self.engine = None
|
||||
|
||||
def fetchall(self):
|
||||
try:
|
||||
retVal = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue