Skip to content

Fix unclosed file ResourceWarnings in FileRecips and ToDigest#36

Open
thegushi wants to merge 1 commit into
jaredmauch:mainfrom
thegushi:fix/unclosed-files
Open

Fix unclosed file ResourceWarnings in FileRecips and ToDigest#36
thegushi wants to merge 1 commit into
jaredmauch:mainfrom
thegushi:fix/unclosed-files

Conversation

@thegushi
Copy link
Copy Markdown
Collaborator

Two handlers open file-like resources that are never explicitly closed, producing ResourceWarning under Python 3.

  • FileRecips.py: open(members.txt) is read but never closed; wrap in with fp:.
  • ToDigest.py: Mailbox (a mailbox.mbox subclass) opens its own internal file descriptor. Two call sites never called close():
    • process(): Mailbox.AppendMessage() now wrapped in try/finally
    • send_i18n_digests(): body extracted to _send_i18n_digests() so the Mailbox can be closed in a finally block before any early return

try/finally is used instead of a with-statement because Mailbox does not support the context manager protocol on all Python 3 builds (confirmed failing on FreeBSD Python 3.11).

Files changed:

  • Mailman/Handlers/FileRecips.py
  • Mailman/Handlers/ToDigest.py

FileRecips.py: use 'with fp:' to ensure members.txt is closed after reading.

ToDigest.py: Mailbox (mailbox.mbox subclass) opens its own file descriptor
internally. Two instances were never closed:
- process(): wrap Mailbox.AppendMessage() in try/finally to call close()
- send_i18n_digests(): extract body into _send_i18n_digests() and close
  the Mailbox in a finally block to handle the early-return path

On FreeBSD Python 3.11, Mailbox does not support the context manager
protocol, so try/finally is used rather than a with-statement.
@thegushi thegushi mentioned this pull request May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant