Skip to content

Conversation

@Rajat-Ahuja1997
Copy link
Contributor

Summary

Fixes a regression introduced in v7.4.2 that causes LookupError when using cashews with FastAPI's
lifespan pattern or other cross-context scenarios.

Problem

Version 7.4.2 (commit a47f8ce) changed ControlMixin.__init__ from:

self.__disable: ContextVar[set[Command]] = ContextVar(str(id(self)), default=set())
to:
self.__disable: ContextVar[set[Command]] = ContextVar(str(id(self)))
self.__disable.set(set())

This breaks cross-context usage because:
- .set() only affects the current async context
- When cache operations run in different contexts (e.g., HTTP requests after setup in lifespan),
.get() raises LookupError because there's no value and no default

This is the same issue as #382, which was fixed for key_context.py in PR #383 (commit e22d2a7), but
the fix was not applied to ControlMixin.

Solution

Restore the default=set() parameter to the ContextVar constructor. This ensures .get() returns a
default value in all async contexts, not just where .set() was called.

Changes

- Restored default=set() parameter in cashews/backends/interface.py:183
- Removed redundant self.__disable.set(set()) call
- Added test_control_mixin_cross_context to verify the fix and prevent future regressions

Testing

- All existing tests pass (54/67 in test_disable_control.py, 13 fail due to missing optional
diskcache dependency)
- New test test_control_mixin_cross_context specifically tests cross-context behavior
- Verified test fails without the fix (LookupError) and passes with the fix

Related

- Fixes the same root cause as #382 / PR #383
- Affects users using FastAPI lifespan pattern or similar cross-context scenarios

This fixes a regression introduced in version 7.4.2 (commit a47f8ce) where
ControlMixin's __disable ContextVar was changed from having a default value
to using an explicit .set() call. This broke cross-context usage patterns
like FastAPI's lifespan, where cache.setup() is called in one async context
and cache operations happen in different contexts (HTTP requests).

The issue is identical to Krukov#382, which was fixed for key_context.py in PR Krukov#383
(commit e22d2a7), but the same fix was not applied to ControlMixin.

Changes:
- Restore default=set() parameter to ControlMixin.__disable ContextVar
- Remove redundant self.__disable.set(set()) call
- Add test_control_mixin_cross_context to verify cross-context behavior

This ensures that .get() returns a default value in all async contexts,
not just the one where .set() was called, preventing LookupError.
@kthy
Copy link

kthy commented Dec 1, 2025

@Krukov any chance this fix could be merged and we could get a point release? 🙏

@Krukov Krukov merged commit 0ce9d69 into Krukov:master Dec 6, 2025
0 of 6 checks passed
@Krukov
Copy link
Owner

Krukov commented Dec 6, 2025

Sure, Sorry for the weak support, I have a difficult period in my life.

Thanks for MR !

@kthy
Copy link

kthy commented Dec 8, 2025

No worries, hope you'll come out okay on the other side. Wishing you strength and luck!

@Rajat-Ahuja1997
Copy link
Contributor Author

No worries, hope you'll come out okay on the other side. Wishing you strength and luck!

Agreed, hope things get better for you soon!

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.

3 participants