Skip to content

Make cursor finalizer shutdown cleanup idempotent and lower log level when cursor is already closed #182

@hiroyukinakatsuka

Description

@hiroyukinakatsuka

Is your feature request related to a problem? Please describe.

At interpreter shutdown, an ERROR log is emitted from the cursor finalizer even when the cursor and connection were explicitly closed during normal execution. This is noisy and can be misleading in production diagnostics.

Observed log (at process exit):

mssql\_python::log\@142: Error during cursor cleanup in **del**: Cursor is already closed.

Environment / Context

  • OS: Windows 10
  • Python: 3.12.6
  • mssql-python: 0.9.0
  • Pooling: disabled
  • Logging: setup_logging("stdout", logging.DEBUG)

Minimal context (simplified code):

import logging, sys
import mssql_python as DBDRV

DBDRV.setup_logging("stdout", logging.DEBUG)
DBDRV.pooling(enabled=False)

conn = DBDRV.connect("<DSN>")
cur = conn.cursor()
cur.execute("SELECT 1;")
cur.fetchall()

conn.close()
sys.exit(0)  # At shutdown, ERROR log above is printed

Describe the solution you'd like

  • Make cursor cleanup idempotent in the finalizer path: if the cursor is already closed, treat it as a no-op.
  • Downgrade this condition from ERROR to INFO or DEBUG, or suppress it entirely when it occurs during interpreter shutdown.

Describe alternatives you've considered

  • Explicitly closing all cursors/connections, clearing references, forcing gc.collect() → functional but still prints the ERROR at shutdown.
  • Lowering global log level to suppress the message → also hides genuinely actionable errors.
  • Custom atexit cleanup → reduces likelihood but does not address the finalizer’s behavior/severity.

Additional context

This request aims to reduce shutdown-time log noise without changing runtime semantics. It should be backward compatible: double-closing a closed cursor becomes a no-op, and logging severity better reflects the benign nature of the condition.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions