Skip to content

Conversation

bellini666
Copy link
Contributor

@bellini666 bellini666 commented Jul 7, 2025

85d2445#r161477713 changed the ContextVar to be a dict of ContextVars

While that does work in preventing the contexts to leak, removing a value with del local.value completely removes the ContextVar, literally unsetting it from all other contexts.

As we discussed here, using .copy() is simpler and should have no downsides compared to the current solution, especially since it is a shallow copy only

This PR reverts the mentioned change and instead uses a .copy() for both __setattr__ and __delattr__. The added tests there still pass for this solution, and I also added a new test that would have caught this issue.

Fixes #522
Fixes #485

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this is literally a reversion of 85d2445#r161477713, but I added .copy on lines 27 and 32

assert not hasattr(test_local, "value")

await asyncio.create_task(_test())
assert test_local.value == 123
Copy link
Contributor Author

@bellini666 bellini666 Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the reversion, and even without the .copy(), this would break, essentially reproducing the issue

Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @bellini666.

Essentially the suggestion we had in #477. (Always go for the simplest option 🫠)

I'll just wait to see if we get a response on the issue but... 👍 Thanks!

@carltongibson
Copy link
Member

OK, user confirming the fix on #485 (comment) — I'll roll a release shortly.

@carltongibson carltongibson merged commit fae2ab1 into django:main Jul 8, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asgiref 3.9.0 breaks django-hosts AttributeError: asgiref.local._CVar object has no attribute 'value'

2 participants