Skip to main content

SKY-L008 · Logic and resilience

Missing resource cleanup

Medium

The catalog gives no severity for this rule; the detector always reports MEDIUM.

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

jobs/store.py · PythonReported on line 7
"""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 count

What Skylos leaves alone

jobs/store.py · Python
"""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 conn

Why 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

  1. Runs with --quality on Python files. Looks at the statements of each function and of the module, including those inside if, for, while and try blocks, but not inside a with block.
  2. Recognizes open(), Path(...).open(), sqlite3.connect(), psycopg2.connect(), pymysql.connect(), cx_Oracle.connect(), socket.socket(), requests.Session(), urllib3.PoolManager(), http.client.HTTPConnection() and HTTPSConnection(), and the tempfile NamedTemporaryFile(), TemporaryFile() and SpooledTemporaryFile() calls, written with those module names.
  3. Reports such a call when its result is assigned (conn = ..., self.conn = ...) or when the call is a statement on its own.
  4. Skips the assignment when the very next statement is conn.close() or with conn: (the second only for handles whose with closes them, so not for database connections), when a finally closes 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 with enter_context, callback, addCleanup, closing(), atexit.register or weakref.finalize.
  5. 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).

jobs/store.py · Python · the fix, against the flagged file
"""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 as json.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.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.