SKY-L008 · Logic and resilience
Missing resource cleanup
A file, socket, database connection or HTTP session is opened and kept in a variable without a with block or a finally that closes it. A later close() call only runs if nothing fails in between.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
When a query or a write raises, the close() line is skipped and the handle stays open. A busy worker that hits the same error again and again runs out of file descriptors or database connections, and SQLite keeps its lock on the file until the process ends.
What Skylos flags
"""Read job counts from the local queue database.""" import sqlite3 def count_pending(db_path): conn = sqlite3.connect(db_path) rows = conn.execute("SELECT COUNT(*) FROM jobs WHERE state = 'pending'") count = rows.fetchone()[0] conn.close() return countWhat Skylos leaves alone
"""Read job counts from the local queue database.""" import sqlite3 def open_jobs_db(db_path): conn = sqlite3.connect(db_path) conn.row_factory = sqlite3.Row return connWhy Skylos leaves it alone
The connection is returned, so the caller owns it and is the one who must close it. Skylos skips a handle that the function returns or yields, or hands to an owner such as ExitStack.enter_context() or an attribute the class closes.
How Skylos decides
- Runs with
--qualityon Python files. Looks at the statements of each function and of the module, including those insideif,for,whileandtryblocks, but not inside awithblock. - Recognizes
open(),Path(...).open(),sqlite3.connect(),psycopg2.connect(),pymysql.connect(),cx_Oracle.connect(),socket.socket(),requests.Session(),urllib3.PoolManager(),http.client.HTTPConnection()andHTTPSConnection(), and thetempfileNamedTemporaryFile(),TemporaryFile()andSpooledTemporaryFile()calls, written with those module names. - Reports such a call when its result is assigned (
conn = ...,self.conn = ...) or when the call is a statement on its own. - Skips the assignment when the very next statement is
conn.close()orwith conn:(the second only for handles whosewithcloses them, so not for database connections), when afinallycloses the name on every path, when the name is returned or yielded, and when the handle is stored on an attribute or in a container, wrapped in a returned object, or registered withenter_context,callback,addCleanup,closing(),atexit.registerorweakref.finalize. - Skips
self.attr = open(...)when a method of the class closes that attribute. Reports MEDIUM on the line of the assignment.
How to fix it
Close the connection in a with block. A sqlite3 connection used as with conn: only commits or rolls back; it does not close, so wrap it in contextlib.closing() (or close it in a finally).
"""Read job counts from the local queue database.""" import sqlite3Added: from contextlib import closing def count_pending(db_path):Removed: conn = sqlite3.connect(db_path)Removed: rows = conn.execute("SELECT COUNT(*) FROM jobs WHERE state = 'pending'")Removed: count = rows.fetchone()[0]Removed: conn.close()Removed: return countAdded: with closing(sqlite3.connect(db_path)) as conn:Added: rows = conn.execute("SELECT COUNT(*) FROM jobs WHERE state = 'pending'")Added: return rows.fetchone()[0]When it can be wrong
- A short script that opens one file and relies on the process exit to close it is reported.
- It misses
with sqlite3.connect(path) as conn:, which leaves the connection open, and a handle opened inside another call, such asjson.load(open(path)). - A
close()that comes later in the function, after other statements, does not count as cleanup, even when nothing between can fail.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Put the marker on the line that opens the resource.
Python conn = sqlite3.connect(db_path) # skylos: ignore[SKY-L008] closed by the pool[tool.skylos] ignore = ["SKY-L008"]turns the rule off for the project.