[GH-ISSUE #101] LAST_CLEANUP causes UnboundLocalError due to missing global declaration in cleanup() #74

Closed
opened 2026-02-25 23:39:47 +03:00 by kerem · 3 comments
Owner

Originally created by @Aejs on GitHub (Jul 4, 2025).
Original GitHub issue: https://github.com/HaschekSolutions/opentrashmail/issues/101

Description

The cleanup() function uses the global variable LAST_CLEANUP but does not declare it as global.
This leads to an UnboundLocalError when the function is called, because Python treats any assignment to a variable as a local variable unless it's explicitly declared as global.

Error message

UnboundLocalError: cannot access local variable 'LAST_CLEANUP' where it is not associated with a value

This happens at the line:

if DELETE_OLDER_THAN_DAYS == False or time.time() - LAST_CLEANUP < 86400:

Later in the same function, the value is reassigned:

LAST_CLEANUP = time.time()

This causes Python to treat LAST_CLEANUP as a local variable, but it's accessed before being set.


Suggested fix

Add a global LAST_CLEANUP declaration at the beginning of the cleanup() function:

def cleanup():
    global LAST_CLEANUP
    if DELETE_OLDER_THAN_DAYS == False or time.time() - LAST_CLEANUP < 86400:
        return
    logger.info("Cleaning up")
    LAST_CLEANUP = time.time()

Real-world impact

This bug causes inbound emails to be rejected with a 500 error during SMTP processing.
Here is a real example of a bounce message sent back to the sender:

<some.user@examplemail.tld>: host mail.examplehost.tld[xx.xx.xx.xx] said: 500
Error: (UnboundLocalError) cannot access local variable 'LAST_CLEANUP' 
where it is not associated with a value (in reply to end of DATA command)

This results in:

  • Failed email delivery
  • A poor reputation with sending mail servers
  • Trashmail server appearing unreliable

Recommendation

A minimal fix is to add the missing global LAST_CLEANUP statement.

Originally created by @Aejs on GitHub (Jul 4, 2025). Original GitHub issue: https://github.com/HaschekSolutions/opentrashmail/issues/101 ### Description The `cleanup()` function uses the global variable `LAST_CLEANUP` but does not declare it as `global`. This leads to an `UnboundLocalError` when the function is called, because Python treats any assignment to a variable as a local variable **unless** it's explicitly declared as global. ### Error message ``` UnboundLocalError: cannot access local variable 'LAST_CLEANUP' where it is not associated with a value ``` This happens at the line: ```python if DELETE_OLDER_THAN_DAYS == False or time.time() - LAST_CLEANUP < 86400: ``` Later in the same function, the value is reassigned: ```python LAST_CLEANUP = time.time() ``` This causes Python to treat `LAST_CLEANUP` as a local variable, but it's accessed **before** being set. --- ### Suggested fix Add a `global LAST_CLEANUP` declaration at the beginning of the `cleanup()` function: ```python def cleanup(): global LAST_CLEANUP if DELETE_OLDER_THAN_DAYS == False or time.time() - LAST_CLEANUP < 86400: return logger.info("Cleaning up") LAST_CLEANUP = time.time() ``` --- ### Real-world impact This bug causes inbound emails to be **rejected with a 500 error** during SMTP processing. Here is a real example of a bounce message sent back to the sender: ``` <some.user@examplemail.tld>: host mail.examplehost.tld[xx.xx.xx.xx] said: 500 Error: (UnboundLocalError) cannot access local variable 'LAST_CLEANUP' where it is not associated with a value (in reply to end of DATA command) ``` This results in: - Failed email delivery - A poor reputation with sending mail servers - Trashmail server appearing unreliable --- ### Recommendation A minimal fix is to add the missing `global LAST_CLEANUP` statement.
kerem closed this issue 2026-02-25 23:39:47 +03:00
Author
Owner

@proodle commented on GitHub (Aug 11, 2025):

This bug is giving me some trouble, too. When I send a test mail via AWS SES to my Open Trashmail account (v1.5.0), Amazon receives a 500 error and automatically puts the mail address on a blacklist.
After applying Aejs's fix, it works fine.
Can you please create a pull request for this fix? I think it is really important.

<!-- gh-comment-id:3175813459 --> @proodle commented on GitHub (Aug 11, 2025): This bug is giving me some trouble, too. When I send a test mail via AWS SES to my Open Trashmail account (v1.5.0), Amazon receives a 500 error and automatically puts the mail address on a blacklist. After applying Aejs's fix, it works fine. Can you please create a pull request for this fix? I think it is really important.
Author
Owner

@Aejs commented on GitHub (Aug 11, 2025):

@proodle just created an pull request.

<!-- gh-comment-id:3176382101 --> @Aejs commented on GitHub (Aug 11, 2025): @proodle just created an pull request.
Author
Owner

@Aejs commented on GitHub (Aug 13, 2025):

Pull Request is merged. Close

<!-- gh-comment-id:3182529118 --> @Aejs commented on GitHub (Aug 13, 2025): Pull Request is merged. Close
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentrashmail#74
No description provided.