Opened 6 years ago

Closed 6 years ago

#29467 closed Bug (fixed)

override_settings context manager can leave undesired settings state

Reported by: Sławek Ehlert Owned by: Sławek Ehlert
Component: Testing framework Version: 2.0
Severity: Normal Keywords: settings override_settings
Cc: Mariusz Felisiak, Carlton Gibson Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When connecting to the setting_changed signal a receiver function that throws an error, one can break the override_settings decorator/context manager and leave the current settings in the modified state. override_settings context manager should be more defensive inside the "enter" part when the setting_changed signal is sent.

Example:

## settings
SETTING_A = 'A'
SETTING_B = 'B'

## usage
from django.test import override_settings
from django.conf import settings
from django.core import signals


def setting_changed_receiver(*args, **kwargs):
    if kwargs['setting'] == 'SETTING_B':
        raise Exception("I'm here for the lulz")


signals.setting_changed.connect(setting_changed_receiver)

try:
    with override_settings(SETTING_A='X', SETTING_B='X'):
        pass
except:
    pass

print(settings.SETTING_A)
print(settings.SETTING_B)

prints out:

X
X

instead of the expected:

A
B

This may have some repercussions while running tests, since one failing test can influence a different, unrelated one.

Change History (7)

comment:1 by Sławek Ehlert, 6 years ago

You can see what kind of troubles it may cause in https://code.djangoproject.com/ticket/29449#comment:2

comment:2 by Claude Paroz, 6 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Sławek Ehlert, 6 years ago

Owner: changed from nobody to Sławek Ehlert
Status: newassigned

comment:4 by Sławek Ehlert, 6 years ago

I've opened https://github.com/django/django/pull/10068 to address this issue.

comment:5 by Mariusz Felisiak, 6 years ago

Cc: Mariusz Felisiak added

comment:6 by Carlton Gibson, 6 years ago

Cc: Carlton Gibson added
Has patch: set

comment:7 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In c6238bf0:

Fixed #29467 -- Made override_settings handle errors in setting_changed signal receivers.

Note: See TracTickets for help on using tickets.
Back to Top