Skip to content

AttributeError: 'str' object has no attribute '__module__' in django.py fixups when using Flower #10042

@maycuatroi1

Description

@maycuatroi1

Checklist

  • I have verified that the issue exists against the main branch of Celery.
  • This has already been asked to the discussions forum first.
  • I have read the relevant section in the contribution guide on reporting bugs.
  • I have checked the issues list for similar or identical bug reports.
  • I have checked the pull requests list for existing proposed fixes.
  • I have checked the commit log to find out if the bug was already fixed in the main branch.
  • I have included all related issues and possible duplicate issues in this issue.

Mandatory Debugging Information

  • Celery version: 5.6.1
  • Flower version: 2.0.1
  • Python version: 3.12
  • OS: Linux (Docker container)
  • Broker: Redis
  • Result backend: Redis

Description

When starting Flower with a Django/Celery application, an AttributeError is raised in celery/fixups/django.py at line 220.

The error occurs because self.worker.pool_cls is a string ('prefork') instead of a class object, so accessing .__module__ fails.

Error Traceback

File "/usr/local/lib/python3.12/site-packages/celery/fixups/django.py", line 220, in _close_database
    is_prefork = "prefork" in self.worker.pool_cls.__module__
                              │    │      └ 'prefork'
                              │    └ <Worker: celery@75efc8aa2596 (INIT)>
                              └ <celery.fixups.django.DjangoWorkerFixup object at 0x7f77b5998f20>

AttributeError: 'str' object has no attribute '__module__'. Did you mean: '__mod__'?

Full Stack Trace

File "/usr/local/lib/python3.12/site-packages/celery/fixups/django.py", line 116, in on_worker_init
    self.worker_fixup.install()
File "/usr/local/lib/python3.12/site-packages/celery/fixups/django.py", line 161, in install
    self.close_database()
File "/usr/local/lib/python3.12/site-packages/celery/fixups/django.py", line 207, in close_database
    return self._close_database()
File "/usr/local/lib/python3.12/site-packages/celery/fixups/django.py", line 220, in _close_database
    is_prefork = "prefork" in self.worker.pool_cls.__module__
AttributeError: 'str' object has no attribute '__module__'

Steps to Reproduce

  1. Set up a Django project with Celery 5.6.1
  2. Install Flower 2.0.1
  3. Run Flower: celery -A your_app flower --port=5555
  4. Observe the AttributeError in logs

Expected Behavior

Flower should start without errors.

Actual Behavior

An AttributeError is raised because pool_cls is a string instead of a class object when the Django fixup code tries to check if the pool is prefork.

Root Cause Analysis

In celery/fixups/django.py line 220:

is_prefork = "prefork" in self.worker.pool_cls.__module__

The code assumes pool_cls is a class with a __module__ attribute, but at this point in the initialization, pool_cls is still a string (e.g., 'prefork').

Suggested Fix

Check if pool_cls is a string before accessing .__module__:

pool_cls = self.worker.pool_cls
if isinstance(pool_cls, str):
    is_prefork = "prefork" in pool_cls
else:
    is_prefork = "prefork" in pool_cls.__module__

Impact

  • Severity: Low (Flower still works despite the error)
  • Workaround: None needed - the error is logged but doesn't prevent Flower from functioning

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions