Fix KNOWN_ROOM_VERSIONS.__contains__ raising TypeError for non-string keys (#19649)

The Rust port of `KNOWN_ROOM_VERSIONS` (#19589) made `__contains__`
strict about key types, raising `TypeError` when called with `None`
instead of returning `False` like a Python dict would.
This broke `/sync` for rooms with a NULL `room_version` in the database.

```
  File "/home/synapse/src/synapse/handlers/sync.py", line 2628, in _get_room_changes_for_initial_sync
    if event.room_version_id not in KNOWN_ROOM_VERSIONS:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument 'key': 'NoneType' object cannot be cast as 'str'
```
This commit is contained in:
Quentin Gliech
2026-04-07 14:12:01 +02:00
committed by GitHub
parent 72711a3329
commit 09d83f3127
3 changed files with 59 additions and 3 deletions
+13 -3
View File
@@ -600,7 +600,12 @@ impl KnownRoomVersionsMapping {
Ok(())
}
fn __getitem__(&self, key: &str) -> PyResult<RoomVersion> {
fn __getitem__(&self, key: &Bound<'_, PyAny>) -> PyResult<RoomVersion> {
// We need to accept anything as the key, but we know that only strings
// are valid keys, so if it's not a string we just raise a KeyError.
let Ok(key) = key.extract::<&str>() else {
return Err(PyKeyError::new_err(key.to_string()));
};
let versions = self.versions.read().unwrap();
versions
.iter()
@@ -609,9 +614,14 @@ impl KnownRoomVersionsMapping {
.ok_or_else(|| PyKeyError::new_err(key.to_string()))
}
fn __contains__(&self, key: &str) -> PyResult<bool> {
fn __contains__(&self, key: &Bound<'_, PyAny>) -> bool {
// We need to accept anything as the key, but we know that only strings
// are valid keys, so if it's not a string we just return false.
let Ok(key) = key.extract::<&str>() else {
return false;
};
let versions = self.versions.read().unwrap();
Ok(versions.iter().any(|v| v.identifier == key))
versions.iter().any(|v| v.identifier == key)
}
fn keys(&self) -> PyResult<Vec<&'static str>> {