print_designer icon indicating copy to clipboard operation
print_designer copied to clipboard

Bug Report: flush_realtime_log raises AttributeError: _realtime_log during background execution

Open alaahariri opened this issue 5 months ago • 1 comments

Summary: Calling frappe.email.doctype.email_queue.email_queue.send_now in a non-request context (e.g., via API or scheduler) results in an AttributeError from frappe.realtime.flush_realtime_log() due to an uninitialized frappe.local._realtime_log.


Versions:

{
  "frappe": "15.71.0",
  "erpnext": "15.65.2"
}

Steps to Reproduce:

  1. Go to Email Queue
  2. Click Send Now for a queued email with attachment (e.g., Sales Invoice)
  3. Observe the error in logs or API response

Traceback:

Traceback (most recent call last):
  File "apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 184, in send
    ctx.update_recipient_status_to_sent(recipient)
  File "apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 291, in update_recipient_status_to_sent
    recipient.update_db(status="Sent", commit=True)
  File "apps/frappe/frappe/email/doctype/email_queue_recipient/email_queue_recipient.py", line 35, in update_db
    frappe.db.commit()
  File "apps/frappe/frappe/database/database.py", line 1178, in commit
    self.after_commit.run()
  File "apps/frappe/frappe/utils/__init__.py", line 1167, in run
    _func()
  File "apps/frappe/frappe/realtime.py", line 87, in flush_realtime_log
    for args in frappe.local._realtime_log:
AttributeError: _realtime_log

Root Cause: frappe.local._realtime_log is only initialized during web requests. When calling code from a background process, this attribute is missing, but flush_realtime_log() assumes it always exists.


Proposed Fix: Add a simple guard in flush_realtime_log():

def flush_realtime_log():
    if not hasattr(frappe.local, "_realtime_log"):
        return
    for args in frappe.local._realtime_log:
        emit_via_redis(*args)

alaahariri avatar Jul 12 '25 14:07 alaahariri

Already handled here ... seems we are facing the same issues ... https://github.com/frappe/frappe/pull/33236

git-avc avatar Jul 12 '25 15:07 git-avc