#34765: call_command reuses already loaded command causing stale class variable
usage
-------------------------------------+-------------------------------------
Reporter: MaziyarMK | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 4.2
(Management commands) |
Severity: Normal | Keywords: call_command
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi,
If a custom command has custom class variables that are used in handle(),
previously set values are re-used on the next call to the same command
because a new instance is not initialized.
because of:
{{{
if isinstance(app_name, BaseCommand):
# If the command is already loaded, use it directly.
command = app_name
else:
command = load_command_class(app_name, command_name)
}}}
https://github.com/django/django/blob/8edaf07a28a3e3848ab1edbdcd2fdc25e6bd6015/django/core/management/__init__.py#L272
I have seen this cause issues when the same command is called several
times in a test.
Proposed solution:
Either warn the user about defining custom class level variables, or don't
re-use the initialized command, instead kill the old object and create a
new one on each use. As an optimizaiton we can require this only on custom
commands, and let the base commands keep using the optimized-path with
cached command instance.
Bug introduced in:
https://github.com/django/django/commit/901c3708fb8a2e51bddd37358f8e536282a8c266
--
Ticket URL: <https://code.djangoproject.com/ticket/34765>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070189c1a516df-c7ccde25-fe62-45ac-a96b-cb888491bf75-000000%40eu-central-1.amazonses.com.