Skip to content

Set type of user_id on is_server_admin to str #18786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2025

Conversation

anoadragon453
Copy link
Member

@anoadragon453 anoadragon453 commented Aug 6, 2025

And updates the few calling sites.

This allows the cache of this function to be invalidated. As we were calling _invalidate_cache_and_stream with a str instead of a UserID as this function expected, the cache was not actually being invalidated.

We can't just pass UserID to _invalidate_cache_and_stream, as that function puts the args in a DB column and does not deserialise types when pulling them out.

Once again fixes #18738. Broke in #18747.

Fix #18738
Fix #18783

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

@anoadragon453 anoadragon453 marked this pull request as ready for review August 6, 2025 14:03
@anoadragon453 anoadragon453 requested a review from a team as a code owner August 6, 2025 14:03
@AndrewFerr
Copy link
Member

Confirmed that this fixes #18783.

@@ -674,7 +674,7 @@ async def delete_account_validity_for_user(self, user_id: str) -> None:
)

@cached(max_entries=100000)
async def is_server_admin(self, user: UserID) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why didn't our custom mypy plugin catch this?

def get_method_signature_hook(
self, fullname: str
) -> Optional[Callable[[MethodSigContext], CallableType]]:
if fullname.startswith(
(
"synapse.util.caches.descriptors.CachedFunction.__call__",
"synapse.util.caches.descriptors._LruCachedFunction.__call__",
)
):
return cached_function_method_signature
if fullname in (
"synapse.util.caches.descriptors._CachedFunctionDescriptor.__call__",
"synapse.util.caches.descriptors._CachedListFunctionDescriptor.__call__",
):
return check_is_cacheable_wrapper

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe our plugin currently allows proper type-checking of callers of cached functions, but it doesn't help with _invalidate_cache_and_stream. The arguments in that case are completely different. _invalidate_cache_and_stream takes in the function name and a list of parameters (which are typed as Any). The cached decorated takes the parameters directly.

I think what we actually want to do here is to make use of ParamSpec to "forward" the types from an inner to an outer function. Then mypy could warn that we were using str instead of UserID.

But we'd ideally go one step further and specify that _invalidate_cache_and_stream can only take in primitive types (because they are dropped into the DB, and then pulled out again without deserialisation). Either that, or store the types in the DB as well/pickle them so we can store things like UserID in there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, so UserID is actually is_cacheable. And the only problem was mis-aligning str and UserID in the lookup to invalidate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and we can tell the cache is working by seeing #18783 😁

This allows the cache of this function to be invalidated. As we were calling `_invalidate_cache_and_stream` with a `str` instead of a `UserID` as this function expected, the cache was not actually being invalidated.
@anoadragon453 anoadragon453 force-pushed the anoa/fix_cache_storage_function branch from 2b73bb0 to 477bb83 Compare August 7, 2025 12:03
@anoadragon453 anoadragon453 changed the base branch from develop to release-v1.136 August 7, 2025 12:04
@anoadragon453
Copy link
Member Author

Base changed to release-v1.136 to include the fix in the RC.

@anoadragon453 anoadragon453 enabled auto-merge (squash) August 7, 2025 12:04
@anoadragon453 anoadragon453 merged commit 3a01e9d into release-v1.136 Aug 7, 2025
78 of 80 checks passed
@anoadragon453 anoadragon453 deleted the anoa/fix_cache_storage_function branch August 7, 2025 14:16
devonh pushed a commit that referenced this pull request Aug 11, 2025
devonh pushed a commit that referenced this pull request Aug 11, 2025
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 11, 2025
(There was no 1.135.1.)

pkgsrc:
  - Drop room creator power level patch, overtaken by upstream changes
    in this release.

# Synapse 1.135.2 (2025-08-11)

This is the Synapse portion of the
[Matrix coordinated security release](https://matrix.org/blog/2025/07/security-predisclosure/).
This release includes support for
[room version](https://spec.matrix.org/v1.15/rooms/)
12 which fixes a number of security vulnerabilities, including
[CVE-2025-49090](https://www.cve.org/CVERecord?id=CVE-2025-49090).

The default room version is not changed. Not all clients will support
room version 12 immediately, and not all users will be using the
latest version of their clients. Large, public rooms are advised to
wait a few weeks before upgrading to room version 12 to allow users
throughout the Matrix ecosystem to update their clients.

### Bugfixes

- Fix invalidation of storage cache that was broken in 1.135.0.
  ([\#18786](element-hq/synapse#18786))

### Internal Changes

- Add a parameter to `upgrade_rooms(..)` to allow auto join local users.
  ([\#82](element-hq/synapse#82))
- Speed up upgrading a room with large numbers of banned users.
  ([\#18574](element-hq/synapse#18574))
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.

Changing user admin status doesn't apply until server restart Performance of Sync API is degraded in v1.135.0rc1
4 participants