synapse icon indicating copy to clipboard operation
synapse copied to clipboard

New logins fail to `/sync` due to reported database corruption.

Open leprasmurf opened this issue 4 years ago • 12 comments

Description

Users are unable to log into my self-hosted instance using either the android apps (element-android or fluffychat) or the self-hosted element-web. The clients provide minimal information or feedback of the on-going error; however, logs show stack traces on SyncRestServlet.

I was running 1.59.1, but rolled back to 1.58.1 to troubleshoot and noticed the difference in the error messages.

v1.58.1

2022-05-22 16:27:49,197 - ERROR - synapse.http.server - 100 - GET-1720 - Failed handle request via 'SyncRestServlet': <XForwardedForRequest at 0x7f3895d77b50 method='GET' uri='/_matrix/client/r0/sync?filter=0&ti
meout=0&_cacheBuster=1653236868783' clientproto='HTTP/1.1' site='8008'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1656, in _inlineCallbacks                                                                                                              result = current_context.run(
  File "/usr/local/lib/python3.9/site-packages/twisted/python/failure.py", line 514, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python3.9/site-packages/synapse/util/caches/response_cache.py", line 246, in cb
    return await callback(*args, **kwargs)                                                                                                                                                                           File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 355, in _wait_for_sync_for_user
    result: SyncResult = await self.current_sync_for_user(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 406, in current_sync_for_user
    sync_result = await self.generate_sync_result(                                                                                                                                                                   File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1113, in generate_sync_result
    res = await self._generate_sync_entry_for_rooms(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1624, in _generate_sync_entry_for_rooms
    await concurrently_execute(handle_room_entries, room_entries, 10)
  File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 235, in concurrently_execute                                                                                                       await yieldable_gather_results(
  File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 276, in yieldable_gather_results
    raise dfe.subFailure.value from None                                                                                                                                                                             File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1656, in _inlineCallbacks
    result = current_context.run(                                                                                                                                                                                    File "/usr/local/lib/python3.9/site-packages/twisted/python/failure.py", line 514, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)                                                                                                                                                                   File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 227, in _concurrently_execute_inner
    await maybe_awaitable(func(value))                                                                                                                                                                               File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1613, in handle_room_entries
    await self._generate_room_entry(                                                                                                                                                                                 File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 2059, in _generate_room_entry
    batch = await self._load_filtered_recents(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 592, in _load_filtered_recents
    loaded_recents = await filter_events_for_client(                                                                                                                                                                 File "/usr/local/lib/python3.9/site-packages/synapse/visibility.py", line 85, in filter_events_for_client
    event_id_to_state = await storage.state.get_state_for_events(                                                                                                                                                    File "/usr/local/lib/python3.9/site-packages/synapse/storage/state.py", line 676, in get_state_for_events
    event_to_groups = await self._get_state_group_for_events(event_ids)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/state.py", line 808, in _get_state_group_for_events
    return await self.stores.main._get_state_group_for_events(event_ids)                                                                                                                                             File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1660, in _inlineCallbacks
    result = current_context.run(gen.send, result)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/state.py", line 332, in _get_state_group_for_events
    raise RuntimeError("No state group for unknown or outlier event %s" % e)                                                                                                                                       RuntimeError: No state group for unknown or outlier event $D8cewEuqesl5P-OSyv0PKOswPk8lMCUjLtTMnw1AZas

1.59.1

2022-05-22 12:44:10,397 - ERROR - synapse.http.server - 120 - GET-1049 - Failed handle request via 'SyncRestServlet': <XForwardedForRequest at 0x7faa685d43d0 method='GET' uri='/_matrix/client/r0/sync?filter=0&ti
meout=30000' clientproto='HTTP/1.1' site='8008'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1656, in _inlineCallbacks
    result = current_context.run(
  File "/usr/local/lib/python3.9/site-packages/twisted/python/failure.py", line 514, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python3.9/site-packages/synapse/util/caches/response_cache.py", line 246, in cb
    return await callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 355, in _wait_for_sync_for_user
    result: SyncResult = await self.current_sync_for_user(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 406, in current_sync_for_user
    sync_result = await self.generate_sync_result(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1113, in generate_sync_result
    res = await self._generate_sync_entry_for_rooms(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1624, in _generate_sync_entry_for_rooms
    await concurrently_execute(handle_room_entries, room_entries, 10)
  File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 235, in concurrently_execute
    await yieldable_gather_results(
  File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 291, in yieldable_gather_results
    raise dfe.subFailure.value from None
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1656, in _inlineCallbacks
    result = current_context.run(
  File "/usr/local/lib/python3.9/site-packages/twisted/python/failure.py", line 514, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python3.9/site-packages/synapse/util/async_helpers.py", line 227, in _concurrently_execute_inner
    await maybe_awaitable(func(value))
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 1613, in handle_room_entries
    await self._generate_room_entry(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 2059, in _generate_room_entry
    batch = await self._load_filtered_recents(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/sync.py", line 570, in _load_filtered_recents
    events, end_key = await self.store.get_recent_events_for_room(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/stream.py", line 693, in get_recent_events_for_room
    events = await self.get_events_as_list(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 480, in get_events_as_list
    event_entry_map = await self._get_events_from_cache_or_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 690, in _get_events_from_cache_or_db
    missing_events: Dict[str, EventCacheEntry] = await delay_cancellation(
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1660, in _inlineCallbacks
    result = current_context.run(gen.send, result)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 676, in get_missing_events_from_db
    raise e
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 670, in get_missing_events_from_db
    missing_events = await self._get_events_from_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 1103, in _get_events_from_db
    raise RuntimeError(
RuntimeError: Database corruption: Event $7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4 in room ![room_id]:[my.server] from the database appears to have been modified (calculated event id $bJ92lmhXr_Mohpt2YNiNXK4ydp0tYSfqeQGZYTSWx60)

This issue appears to be distinct from #12507:

synapse=# select * from event_forward_extremities efe left join events e using (event_id) where e.event_id is null;
 event_id | room_id | topological_ordering | type | room_id | content | unrecognized_keys | processed | outlier | depth | origin_server_ts | received_ts | sender | contains_url | instance_name | stream_ordering
| state_key | rejection_reason
----------+---------+----------------------+------+---------+---------+-------------------+-----------+---------+-------+------------------+-------------+--------+--------------+---------------+-----------------
+-----------+------------------
(0 rows)

and #12549:

synapse=# select e.room_id, count(*)
  from events e left join rejections r using (event_id) left join event_to_state_groups esg using (event_id)
  where not e.outlier and esg.event_id is null and r.event_id is null
  group by e.room_id;
 room_id | count
---------+-------
(0 rows)
synapse=# select e.* from events e left join rejections r using (event_id) left join event_to_state_groups esg using (event_id) where not e.outlier and esg.event_id is null and r.event_id is null;
 topological_ordering | event_id | type | room_id | content | unrecognized_keys | processed | outlier | depth | origin_server_ts | received_ts | sender | contains_url | instance_name | stream_ordering | state_ke
y | rejection_reason
----------------------+----------+------+---------+---------+-------------------+-----------+---------+-------+------------------+-------------+--------+--------------+---------------+-----------------+---------
--+------------------
(0 rows)

I can get a list of failing events (1.59.1):

leprasmurf@docker01:/opt/matrix$ grep 'Database corruption: Event' backend/log/homeserver.log | grep -o '$.* in room !.* from the database' | sort | uniq -c
      7 $3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE in room ![room_id]:[my.server] from the database
     18 $7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4 in room ![room_id]:[my.server] from the database
      3 $CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU in room ![room_id]:[my.server] from the database
     10 $dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU in room ![room_id]:[my.server] from the database
      9 $f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U in room ![room_id]:[my.server] from the database
      4 $FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU in room ![room_id]:[my.server] from the database
     10 $idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s in room ![room_id]:[my.server] from the database
     31 $MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE in room ![room_id]:[my.server] from the database
     10 $Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w in room ![room_id]:[my.server] from the database
      6 $T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg in room ![room_id]:[my.server] from the database

And I can see these events in the event_to_state_groups table:

synapse=# SELECT * FROM event_to_state_groups WHERE event_id IN ('$3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE', '$7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4', '$CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU', '$dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU', '$f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U', '$FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU', '$idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s', '$MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE', '$Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w', '$T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg');
                   event_id                   | state_group
----------------------------------------------+-------------
 $dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU |           7
 $CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU |          18
 $Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w |          30
 $T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg |          62
 $3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE |          72
 $idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s |         103
 $f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U |         147
 $FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU |         176
 $7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4 |         197
 $MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE |         204
(10 rows)

as well as the events table:

synapse=# SELECT event_id,outlier FROM events WHERE event_id IN ('$3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE', '$7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4', '$CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU', '$dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU', '$f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U', '$FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU', '$idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s', '$MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE', '$Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w', '$T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg');
                   event_id                   | outlier
----------------------------------------------+---------
 $dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU | f
 $CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU | f
 $Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w | f
 $T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg | f
 $3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE | f
 $idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s | f
 $f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U | f
 $MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE | f
 $7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4 | f
 $FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU | f
(10 rows)

Trying to view the "State Events" in Synapse Admin on one of the afflicted rooms yields "Server communication error"

Trying to PUT to an afflicted room fails

2022-05-22 18:16:36,934 - ERROR - synapse.http.server - 120 - PUT-5646 - Failed handle request via 'RoomSendEventRestServlet': <XForwardedForRequest at 0x7f1f37f5d760 method='PUT' uri='/_matrix/client/r0/rooms/![room_id]%3A[my.server]/send/m.room.message/m1653243364881.51' clientproto='HTTP/1.1' site='8008'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1656, in _inlineCallbacks
    result = current_context.run(
  File "/usr/local/lib/python3.9/site-packages/twisted/python/failure.py", line 514, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/usr/local/lib/python3.9/site-packages/synapse/rest/client/room.py", line 273, in on_POST
    ) = await self.event_creation_handler.create_and_send_nonmember_event(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/message.py", line 886, in create_and_send_nonmember_event
    ev = await self.handle_new_client_event(
  File "/usr/local/lib/python3.9/site-packages/synapse/util/metrics.py", line 113, in measured_func
    r = await func(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/message.py", line 1191, in handle_new_client_event
    await self._event_auth_handler.check_auth_rules_from_context(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/event_auth.py", line 57, in check_auth_rules_from_context
    auth_events_by_id = await self._store.get_events(auth_event_ids)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 433, in get_events
    events = await self.get_events_as_list(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 480, in get_events_as_list
    event_entry_map = await self._get_events_from_cache_or_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 690, in _get_events_from_cache_or_db
    missing_events: Dict[str, EventCacheEntry] = await delay_cancellation(
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1660, in _inlineCallbacks
    result = current_context.run(gen.send, result)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 676, in get_missing_events_from_db
    raise e
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 670, in get_missing_events_from_db
    missing_events = await self._get_events_from_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 1103, in _get_events_from_db
    raise RuntimeError(
RuntimeError: Database corruption: Event $MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s in room ![room_id]:[my.server] from the database appears to have been modified (calculated event id $GvkDX05onksbLfcEm7xvmDk50DjL9CrZ6tCvLoHYX70)
leprasmurf@docker01:/opt/matrix$ grep PUT-5646 backend/log/homeserver.log
2022-05-22 18:16:36,934 - ERROR - synapse.http.server - 120 - PUT-5646 - Failed handle request via 'RoomSendEventRestServlet': <XForwardedForRequest at 0x7f1f37f5d760 method='PUT' uri='/_matrix/client/r0/rooms/![room_id]%3A[my.server]/send/m.room.message/m1653243364881.51' clientproto='HTTP/1.1' site='8008'>
2022-05-22 18:16:36,936 - INFO - synapse.access.http.8008 - 427 - PUT-5646 - 1.2.3.4 - 8008 - {@leprasmurf:[my.server]} Processed request: 0.011sec/0.000sec (0.002sec, 0.001sec) (0.001sec/0.002sec/4) 55B 500 "PUT /_matrix/client/r0/rooms/![room_id]%3A[my.server]/send/m.room.message/m1653243364881.51 HTTP/1.1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Element/1.10.12 Chrome/98.0.4758.141 Electron/17.4.0 Safari/537.36" [2 dbevts]

Steps to reproduce

Enable retention in the synapse config?

Ref:

  • https://github.com/matrix-org/synapse/issues/12507#issuecomment-1109634948
  • https://github.com/matrix-org/synapse/issues/12549#issuecomment-1111523042

Retention is now disabled, for what it's worth.

Version information

Homeserver: Self-hosted Version: 1.58.1 / 1.59.1 Install method: docker Platform: debian

leprasmurf avatar May 22 '22 18:05 leprasmurf

Old retention settings:

retention:
  enabled: true
  allowed_lifetime_min: 1d
  allowed_lifetime_max: 5y
  purge_jobs:
    - interval: 7d
    - longest_max_lifetime: 3d
      interval: 12h
    - shortest_max_lifetime: 3d
      interval: 1d

I was able to delete and recreate the rooms that were reporting corruption. Likely, the history of these rooms is lost but the ability to log into new sessions is taking priority.

read -s access_token
room_id='![room_id]:[my.server]'
new_name='[room_name]'

# Remove room
curl -s -H "Authorization: Bearer ${access_token}" -X DELETE -d '{}' 'https://[my.server]/_synapse/admin/v2/rooms/'${room_id}

# Purge room
curl -s -H "Authorization: Bearer ${access_token}" -X DELETE -d '{"purge": true, "force_purge": true}' 'https://[my.server]/_synapse/admin/v2/rooms/'${room_id}

# Remove room and recreate
curl -s -H "Authorization: Bearer ${access_token}" -X DELETE -d '{"new_room_user_id": "@leprasmurf:[my.server]", "room_name": "'"${new_name}"'"}' 'https://[my.server]/_synapse/admin/v2/rooms/'${room_id}

# Purge room and recreate
curl -s -H "Authorization: Bearer ${access_token}" -X DELETE -d '{"new_room_user_id": "@leprasmurf:[my.server]", "room_name": "'"${new_name}"'", "purge": true, "force_purge": true}' 'https://[my.server]/_synapse/admin/v2/rooms/'${room_id}

curl -s -H "Authorization: Bearer ${access_token}" -X GET 'https://[my.server]/_synapse/admin/v2/rooms/'${room_id}'/delete_status' | jq .

I have a few duplicate rooms now, because the failed failed first attempts to delete a room would also create a new one. Upon failure, I would retry with force_purge enabled and a second room would be created but at least the corrupt room would be purged.

I had a session that was attempting to log in (element-web, last time I checked it was around 30 consecutive failures this round) and once the final room with corruption was purged the session finally connected and continued the login process. It took purging about 20 rooms.

The purged / corrupted rooms refused to let go of users, and as such they failed to join the newly recreated rooms too, so I have to go back and manually force some joins (https://matrix-org.github.io/synapse/develop/admin_api/room_membership.html).

Here is the sanitized log output from one such delete attempt

2022-05-23 12:28:13,095 - INFO - synapse.handlers.room - 1623 - DELETE-2604 - Kicking '@[user]:[my.server]' from '![room_id]:[my.server]'...
2022-05-23 12:28:13,105 - ERROR - synapse.handlers.room - 1663 - DELETE-2604 - Failed to leave old room and join new room for '@[user]:[my.server]'
Traceback (most recent call last):                                                                                                                                                                                   File "/usr/local/lib/python3.9/site-packages/synapse/handlers/room.py", line 1630, in shutdown_room
    _, stream_id = await self.room_member_handler.update_membership(                                                                                                                                                 File "/usr/local/lib/python3.9/site-packages/synapse/handlers/room_member.py", line 520, in update_membership
    result = await self.update_membership_locked(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/room_member.py", line 938, in update_membership_locked
    return await self._local_membership_update(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/room_member.py", line 389, in _local_membership_update
    result_event = await self.event_creation_handler.handle_new_client_event(
  File "/usr/local/lib/python3.9/site-packages/synapse/util/metrics.py", line 113, in measured_func
    r = await func(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/message.py", line 1191, in handle_new_client_event
    await self._event_auth_handler.check_auth_rules_from_context(
  File "/usr/local/lib/python3.9/site-packages/synapse/handlers/event_auth.py", line 57, in check_auth_rules_from_context
    auth_events_by_id = await self._store.get_events(auth_event_ids)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 433, in get_events
    events = await self.get_events_as_list(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 480, in get_events_as_list
    event_entry_map = await self._get_events_from_cache_or_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 690, in _get_events_from_cache_or_db
    missing_events: Dict[str, EventCacheEntry] = await delay_cancellation(
  File "/usr/local/lib/python3.9/site-packages/twisted/internet/defer.py", line 1660, in _inlineCallbacks
    result = current_context.run(gen.send, result)
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 676, in get_missing_events_from_db
    raise e
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 670, in get_missing_events_from_db
    missing_events = await self._get_events_from_db(
  File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/events_worker.py", line 1103, in _get_events_from_db
    raise RuntimeError(RuntimeError: Database corruption: Event $IBh5Bag7y7FEWa-AT_X_Yn-4SjfznmALbpv8oYPU-Cw in room ![room_id]:[my.server] from the database appears to have been modified (calculated event id $SismOgze8gUlgtn0dY8y44k5Z_7ZQzHIb2Ez_RNpFUs)
2022-05-23 12:28:13,107 - INFO - synapse.http.server - 104 - DELETE-2604 - <XForwardedForRequest at 0x7f30dc483b50 method='DELETE' uri='/_synapse/admin/v1/rooms/![room_id]:[my.server]' clientproto='HTTP/1.1' site='8008'> SynapseError: 400 - Users are still joined to this room
2022-05-23 12:28:13,108 - INFO - synapse.access.http.8008 - 427 - DELETE-2604 - 1.2.3.4 - 8008 - {@leprasmurf:[my.server]} Processed request: 0.099sec/0.001sec (0.023sec, 0.003sec) (0.007sec/0.031sec/26) 69B 400 "DELETE /_synapse/admin/v1/rooms/![room_id]:[my.server] HTTP/1.1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36" [18 dbevts]
2022-05-23 12:28:13,198 - INFO - synapse.access.http.8008 - 427 - GET-2605 - 1.2.3.4 - 8008 - {@leprasmurf:[my.server]} Processed request: 0.007sec/0.001sec (0.001sec, 0.001sec) (0.002sec/0.003sec/3) 520B 200 "GET /_synapse/admin/v1/rooms/![room_id]:[my.server] HTTP/1.1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36" [0 dbevts]

leprasmurf avatar May 23 '22 13:05 leprasmurf

v1.58.1 File "/usr/local/lib/python3.9/site-packages/synapse/storage/databases/main/state.py", line 332, in _get_state_group_for_events raise RuntimeError("No state group for unknown or outlier event %s" % e)

I think you're missing the final line from that error, with the event ID?

It took purging about 20 rooms.

Were any remote homeservers joined to the affected rooms or were they local only?

squahtx avatar May 26 '22 09:05 squahtx

If you have a backup copy of the database before the purge, or happen to still have the broken events in the event_json table, I'd be interested to see if the broken events have anything in common.

squahtx avatar May 26 '22 09:05 squahtx

I think you're missing the final line from that error, with the event ID?

You are correct, but the event id would routinely change with so many channels having issues.

Were any remote homeservers joined to the affected rooms or were they local only?

They were all local-only. The rooms I'd connected to over federation were not having issues and none of the rooms I had issues with had any other homeserver user.

If you have a backup copy of the database before the purge, or happen to still have the broken events in the event_json table, I'd be interested to see if the broken events have anything in common.

I do have backups, I took one before doing anything destructive. I'm not certain what you're asking me to do, I don't think I can get the event IDs again unless I restore the bad backup and the the system fail again.

leprasmurf avatar May 26 '22 13:05 leprasmurf

I was able to find (and sanitize) some log data. It looks to be about 10 minutes of log entries from 1.59.1 calling the events "Data corruption":

homeserver.2022-05-23.sanitized.log

leprasmurf avatar May 26 '22 21:05 leprasmurf

Right so, the error in question is: RuntimeError: Database corruption: Event $MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s in room ![room_id]:[my.server] from the database appears to have been modified (calculated event id $GvkDX05onksbLfcEm7xvmDk50DjL9CrZ6tCvLoHYX70) Event IDs are hashes of event data, so that means that the event's data in the event_json table has been changed, or there's been a difference in how the event hash is being computed.

There's only one place where we update existing rows in the event_json table and it's in synapse/storage/databases/main/censor_events.py when a redaction_retention_period is set in the config. However this looks to be fine, since that replaces the event data with the output of prune_event_dict and hashes are computed using the output of prune_event_dict too.

So maybe prune_event_dict's behaviour has changed over time, or there's actual database corruption. It's hard to tell without seeing the raw data of the events in question.

squahtx avatar May 30 '22 13:05 squahtx

I was able to find (and sanitize) some log data. It looks to be about 10 minutes of log entries from 1.59.1 calling the events "Data corruption":

homeserver.2022-05-23.sanitized.log

Thanks for the log. Unfortunately it doesn't shed much light on what went wrong.

So maybe prune_event_dict's behaviour has changed over time, or there's actual database corruption. It's hard to tell without seeing the raw data of the events in question.

I'd like to figure out which of these explanations is correct. Are you able to run the following SQL on the database backup and post the output:

SELECT event_id, json FROM event_json WHERE event_id IN ('$3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE', '$7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4', '$CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU', '$FL6zcRAKSsZYu76qwZcRDqXUDcKFqyuQ7LKNqd3h9H4', '$FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU', '$MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s', '$MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE', '$Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w', '$PX-Ygs_gUG8yA23teSsq4tdSPZop6_OTMznkVN3wLmE', '$Qy6rQopBgJx0VDTzUazDPUvsuJkPje7F_WA9SvO5TXc', '$T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg', '$ZVylvH0T47xLYmkQU6qEwdEw6es83x4wA73Vt1xcbpI', '$dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU', '$f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U', '$idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s', '$itbvWg60S8FEN7_P6Nv6HOdDTVjHhZRQUYGQ6YAFu7E', '$j76xYorjEQurZnWgsjLJSiRF_PKIFjOmiDHd-4sSHAs', '$jvezn5SXP6JfFNkGF0SFGO6cudZk9Xlta5AaKAjrLu4', '$wGN3W2Iyc13cGTfhbYzc8JNBh18EM3y9HXfrpggcpeM');

It'll contain raw event data from the broken rooms. If you're not comfortable sharing that publicly, I can be reached over DM at @squah:matrix.org

squahtx avatar May 30 '22 13:05 squahtx

@squahtx: fyi if it's been modified by the redaction-censoring process, you'd expect to see an entry in redactions for it with have_censored=True

richvdh avatar May 30 '22 13:05 richvdh

I may have my bias (wishful thinking) but I really don't think this was true database corruption. It hit the first set of rooms I setup when rebuilding the server, and only those oldest rooms. Once the rooms were deleted and recreated, no further corruption was reported.

The requested query was executed on the corrupted data. I don't think any of this too sensitive to post, as the rooms have since been deleted and recreated. Feel free to correct me if I'm wrong and I can modify the below.

synapse=# SELECT event_id, json FROM event_json WHERE event_id IN ('$3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE', '$7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4', '$CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU','$FL6zcRAKSsZYu76qwZcRDqXUDcKFqyuQ7LKNqd3h9H4', '$FLprA0mMyy_7ji6iSN3ReMXCsbFN7POqhNqTyHEcqaU', '$MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s', '$MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE', '$Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w', '$PX-Ygs_gUG8yA23teSsq4tdSPZop6_OTMznkVN3wLmE', '$Qy6rQopBgJx0VDTzUazDPUvsuJkPje7F_WA9SvO5TXc', '$T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg', '$ZVylvH0T47xLYmkQU6qEwdEw6es83x4wA73Vt1xcbpI', '$dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU', '$f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U', '$idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s', '$itbvWg60S8FEN7_P6Nv6HOdDTVjHhZRQUYGQ6YAFu7E', '$j76xYorjEQurZnWgsjLJSiRF_PKIFjOmiDHd-4sSHAs', '$jvezn5SXP6JfFNkGF0SFGO6cudZk9Xlta5AaKAjrLu4', '$wGN3W2Iyc13cGTfhbYzc8JNBh18EM3y9HXfrpggcpeM');

 event_id                                     | json
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 $dTBgXLa5YVbsi6P2G__XPj-Kw9uQQ1LpvJeqMqxkdmU | {"auth_events":["$0Vw9AxlXveMOZvOxSdKe_v3pa39vmWeVYMi9cCkPQpo","$hctSYukeLA1PsOms1ill1b0ak0qas2-eBai_1V75aPo","$5vJbUdGbf0R6LJjBpJpykLbEna7G1IIWADJ6yb89_PU"],"prev_events":["$09JvJjQRaThf3ADaUOfYdFItupOaflgJ7DNjL3f0FVs"],"type":"m.room.history_visibility","room_id":"!cjonAPcBdEWYeFTmRK:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":6,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646686618852,"hashes":{"sha256":"wmOEGVg1dZNOTn6fviLRHa/rW29ruvVvh1F7LYMxjDA"},"signatures":{"my.server":{"ed25519:a_bifX":"UlYAQoKfPaZzJVtmA0fvBLrZ7TEsZDKMPQLcuv99q9ENJunFZwLu/XHN8CA5wm1E7DvlYeSYICAPJ9QuKTEuCQ"}},"unsigned":{"age_ts":1646686618852}}
 $CKnrnAD9gMNnEtFWTa18g1J7wnqK3JB7Hd3VMuL5dlU | {"auth_events":["$2MGXyuqyuJi_Rf7fzFq9VYC2EnCGR4RKYuAhalUhDpQ","$mpz0NRq0vilEsVhOETA-aLA4tJMo1jvKhpHt-fexulg","$d0d1CY2ga7ZyaFt8BA-CFB1KVv2LqjU4jAqOS6tF32A"],"prev_events":["$jRP4fYm--831nr9kDMv-OclLXRM5VtLF4_joGrDqvPg"],"type":"m.room.history_visibility","room_id":"!BEjRnJmBCVgpDjTaXG:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646686789556,"hashes":{"sha256":"yz7slX8kOItn2zx7PCicMgKgCTiGONp4fswqDytK5AM"},"signatures":{"my.server":{"ed25519:a_bifX":"nli+RvR365jm07VfousEhfzyUCTyeqKni6ia5ZSI9ORLm63bv113TaK0Q5EOldKPvSiYSzejt9AQFbRDAm7eAw"}},"unsigned":{"age_ts":1646686789556}}
 $Qy6rQopBgJx0VDTzUazDPUvsuJkPje7F_WA9SvO5TXc | {"auth_events":["$K9mF8e3sW3sFSeKWyfp_CgGXaqZdxe4Jsim41ixCmYE","$VhqTIDY362MA2jwu5pSm5mtyhmjI2SUdNNwOWs-P6F4","$wD0d_DanIkqtFRY28XQP4u196MgVchMsUmKL7YzBvhk"],"prev_events":["$k9gNZBc_P5HdGfZbTwrqivdhtbIqZFptkHrtIXy52Ng"],"type":"m.room.history_visibility","room_id":"!XMnFvhZONjjYDZXlKA:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687134983,"hashes":{"sha256":"uvFR/l/D69TCg5eYe8BSAouERqWVEyAiXDpVy5UNwz8"},"signatures":{"my.server":{"ed25519:a_bifX":"dBkvN4j4Qn56dkOv0hvD8MJeI93WiIOn7yOaJoFpVNTdM6XDhgmTun3QABK1PnpvcmbaWpVjJeYHTuKJ2PSiAw"}},"unsigned":{"age_ts":1646687134983}}
 $Ng4MFGHnocIiaCPLLMUiWU7aOtx6jUxeOcBpSZSUd8w | {"auth_events":["$4w9oJ-3lXos3Buuuh1wBywErcc93bmWQ_NsqMPhz6vo","$ygVY9WnE9SnEY146x41ODcXhuZJZD9BBQRIzyCmv_sE","$2lz7YO0nug6cbb1exyef0ItVp0JvGLIN4hLC_odQSow"],"prev_events":["$zCrw5S6i2i-gq-ePzjy4A-jwo7KtwjITrI3NMcU35ao"],"type":"m.room.history_visibility","room_id":"!JQqukTgugcLqqgQeTF:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646686920833,"hashes":{"sha256":"+b45HZHrVFduPSqB7TP6QAyDJ8jv+InusSdhWc6QEQM"},"signatures":{"my.server":{"ed25519:a_bifX":"9yyepGuBL6oikmi0rYnult9eWj/ikC+cCa/RG7KFqHm2NK9jWgUj2YoJT6MmwXxYxK5cm0btUrp/BSsltZRvAg"}},"unsigned":{"age_ts":1646686920833}}
 $T9gl7nts667jsaWZ1FGs1D7nCDHNr4aPz2IVKQnVNrg | {"auth_events":["$WuXsqxkSi9u5rkN6JbC8k0tcgeDQXQiavOlP78hwL-k","$sDdJOS_SgsZ7977Jlq5mq_AuESEqwHMZxGM0H-14pMM","$phQDvnMpx52tA9vtgCOK73lB0WTPULje5SarCeqaE4g"],"prev_events":["$vNji4Z8UJiZ_txPIKK9GpUHJlKKttaX_ejsqixe04GA"],"type":"m.room.history_visibility","room_id":"!OjElfKzfOfKGZlkMhY:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":6,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687264746,"hashes":{"sha256":"1uSjuUCGjk1wR50mCr9GZpJ1bIbDKr+Z8wa9AbCwRxE"},"signatures":{"my.server":{"ed25519:a_bifX":"FGzV2kITSVhYl+pujjg0zQmmeGfs+rF1/F+xL0yqyCxGujgC4R5S2Je5GSgdEYg4IQcrmuPxoUsU29DXO06KBA"}},"unsigned":{"age_ts":1646687264746}}
 $MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s | {"auth_events":[],"prev_events":[],"type":"m.room.create","room_id":"!dRWSKyIQudsuxFruPU:my.server","sender":"@leprasmurf:my.server","content":{"room_version":"6","creator":"@leprasmurf:my.server"},"depth":1,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687336032,"hashes":{"sha256":"PY8bozlR64n/NlBEW6dPUkMsYOz+EPCrkFiC5oYtOVs"},"signatures":{"my.server":{"ed25519:a_bifX":"q0NhbZbvhh2QqAX1oBv/S5Ngy2kZEVbcgXsPR72okdG99cEwXa/VHUWxVI9jNX1IlC1A9ic0M0zPdxYS8Kt3CA"}},"unsigned":{"age_ts":1646687336032}}
 $3JWy44e_EZqWWFscWC63dpWeCsUqDhn6D-9bL341oaE | {"auth_events":["$7MLIt8g8LywRJZJJH6l_hAzUbqLRgktGy3MUB7_fTw4","$MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s","$4R62EbyId3Ym69uF8gV7pG5-4zbWpZqZm2pn0TNq4GA"],"prev_events":["$ajboBIMdqsJr1kLRYsGGJmy7ZkhTikwJyRhBleKXpx8"],"type":"m.room.history_visibility","room_id":"!dRWSKyIQudsuxFruPU:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687336475,"hashes":{"sha256":"w5fz2m7fY/zmDkHNyOb/Sok+5MRBT2/+zv90BaVQh0A"},"signatures":{"my.server":{"ed25519:a_bifX":"ZqLfEXighT97w5W3wamPYLDuOQLwqqWWzlItU9kUWyIPLPV0aaa3Kl5OlFtgn++JEWWoTONQdwW4Q/sA2NVdAg"}},"unsigned":{"age_ts":1646687336475}}
 $jvezn5SXP6JfFNkGF0SFGO6cudZk9Xlta5AaKAjrLu4 | {"auth_events":[],"prev_events":[],"type":"m.room.create","room_id":"!YSlBfGMviAAmSCEmBs:my.server","sender":"@leprasmurf:my.server","content":{"room_version":"6","creator":"@leprasmurf:my.server"},"depth":1,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687398035,"hashes":{"sha256":"soiOYdy4hqyUvbBDHvLu7yhA2cUaovvk4se7bVDSiYs"},"signatures":{"my.server":{"ed25519:a_bifX":"wHx5f+TDyp9RN08B1A7eIVAibuVHUJN6EGeUOz3XwIIapvH5Ca9/Q5I9xRJCjpqeeg3CVaCR91b8Svxeq3MdDQ"}},"unsigned":{"age_ts":1646687398035}}
 $idqY-o-EI5Bl-I_8R_lxsQVLu9EeneY8zrvZMLEb52s | {"auth_events":["$smC6GioSNCAT0XcPD8dzwTfmpQ6bRHspviRUXbnZDLc","$RtCmOxuk-MM9DKh5tF7mQpYdB5e7t1ftrLn-wuPjL9I","$6uw8rJuxtsZSWVuOB9hoX26VvW5jH-k8HfqPQWKXCNg"],"prev_events":["$D2BUTc3DaZAFk0nhGKrk3zmpdlqOzDah1bqCmbj6CnY"],"type":"m.room.history_visibility","room_id":"!gmClLijobjbHekPJPB:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687546177,"hashes":{"sha256":"mIN4NMw3vVcI/dmYglmm3OM3jNBrDBPF96NzJUL9MGw"},"signatures":{"my.server":{"ed25519:a_bifX":"Xd6s2vXXdUlgAcdkm1NBOorvOzGrdMpTPDwdx6RswhJKhCcpd3zyeAG7NFZ45dpGd1JMUZtghOJdupLNhc7ADQ"}},"unsigned":{"age_ts":1646687546177}}
 $FL6zcRAKSsZYu76qwZcRDqXUDcKFqyuQ7LKNqd3h9H4 | {"auth_events":[],"prev_events":[],"type":"m.room.create","room_id":"!mCRmpXGAlVwqRVkcuE:my.server","sender":"@leprasmurf:my.server","content":{"room_version":"6","creator":"@leprasmurf:my.server"},"depth":1,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687648781,"hashes":{"sha256":"lxSTt0QRDgQTkSsjMh1xKtkPFLbhEbXeLT9bGpLoeiE"},"signatures":{"my.server":{"ed25519:a_bifX":"2VPdJbvo82JAOZ63whDK5p1XKSyOyFR3mq/oLzd1ODBlqCJ7p78bsjlt0xmyWpyELCP1G1FEjbAoP/4rOCbSDg"}},"unsigned":{"age_ts":1646687648781}}
 $7q5tj1jq6nl2nb7OzmrvAghynhaPnYqxjdxuCL57gG4 | {"auth_events":["$PX-Ygs_gUG8yA23teSsq4tdSPZop6_OTMznkVN3wLmE","$aY6xQW1MlBm-bKLYdILCNmf4S41zZcjYRP6g3sLUPRs","$-OMXkN0TXRMmWPrA4mC3peRs8NBrSSicw-f31ZiNp-A"],"prev_events":["$N0l6rHsIWXXXI025DS_INDPcZMkysr59PQ3pvj7X4XM"],"type":"m.room.history_visibility","room_id":"!MGBZCfpOdcjbvgNnzs:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":6,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646690185098,"hashes":{"sha256":"PHC3bomopYKT+gHbRptqQe0R2QgCcpcqPzBSuu0gA1M"},"signatures":{"my.server":{"ed25519:a_bifX":"DrHYtKytbaF92AJ7VJABtvbZ/RH1Z4cgS9Ts1APQU6Sr6+cRZ+egLGHVdSxiW4GVCxsf+Evs3dkqtt33m9zADA"}},"unsigned":{"age_ts":1646690185098}}
 $ZVylvH0T47xLYmkQU6qEwdEw6es83x4wA73Vt1xcbpI | {"auth_events":["$IBh5Bag7y7FEWa-AT_X_Yn-4SjfznmALbpv8oYPU-Cw"],"prev_events":["$IBh5Bag7y7FEWa-AT_X_Yn-4SjfznmALbpv8oYPU-Cw"],"type":"m.room.member","room_id":"!YyBPtmdKrQhvLvLcpQ:my.server","sender":"@leprasmurf:my.server","content":{"membership":"join","displayname":"leprasmurf"},"depth":2,"prev_state":[],"state_key":"@leprasmurf:my.server","origin":"my.server","origin_server_ts":1646687873757,"hashes":{"sha256":"GbCw4WRRaNZHp+ATzkEbvu2F/JNNjHwq5N9IZtXfsIo"},"signatures":{"my.server":{"ed25519:a_bifX":"7iq7tgmks9seQOObl6TKcbyU0mC2bDXITPHCkG0f+NwRKznyDUqnzRzfHlOEXxkrHMipPYTEuafV8ty+EhozBA"}},"unsigned":{"age_ts":1646687873757}}
 $f8a7HJFM6OdsVoPN-mXywMQY3gkTHLDFbE5oCf5Il0U | {"auth_events":["$FzzWRn5Jr6yuaasPJjegI4eirfZEeulJz2a7r0U0yfA","$IBh5Bag7y7FEWa-AT_X_Yn-4SjfznmALbpv8oYPU-Cw","$ZVylvH0T47xLYmkQU6qEwdEw6es83x4wA73Vt1xcbpI"],"prev_events":["$3YVlYeRu27XHVhY8RxjGcKsLyHfPutj2wmNAjab_UZg"],"type":"m.room.guest_access","room_id":"!YyBPtmdKrQhvLvLcpQ:my.server","sender":"@leprasmurf:my.server","content":{"guest_access":"can_join"},"depth":6,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687874210,"hashes":{"sha256":"LhDEgCOg09EOsILYXWgnL7iVhL0obfSfa7zjQ+coo5s"},"signatures":{"my.server":{"ed25519:a_bifX":"MUEw4m1cG/GOm3tolmKiZwzeX6+l2REy6tgCm2Z5kk4EmRyqhOo99iLB6nYpPwMimvzBRC7Rnxdu49ZsClaXAQ"}},"unsigned":{"age_ts":1646687874210}}
 $j76xYorjEQurZnWgsjLJSiRF_PKIFjOmiDHd-4sSHAs | {"auth_events":["$GV9tuNFWaMdWCRWYxxe3FKMCdaeyfeBn6TI39w3HFao","$wGN3W2Iyc13cGTfhbYzc8JNBh18EM3y9HXfrpggcpeM","$5HSPaVl9wOdeHNlxMzDJ-FHVn1YH785Q7QPtp0bd1Cw"],"prev_events":["$C4oWJjk93VihlZ5niuV2NfBKBG5ZKZj86mgYHdiXjLw"],"type":"m.room.history_visibility","room_id":"!aSchXJzOhbWWvUMZnh:my.server","sender":"@leprasmurf:my.server","content":{"history_visibility":"shared"},"depth":5,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646688043458,"hashes":{"sha256":"gJCGqJtlHDXM1mxnNo7FJazQx1VMR3sYUjRvjNlVT6A"},"signatures":{"my.server":{"ed25519:a_bifX":"RWvUw36j/DKqqQQ99lXXX/bIc8Gjzj8PqPL0BBLy9lCCFHDtDk92NfeoDSZS70bJRxCaME4DvoiJZh+8mWfdAQ"}},"unsigned":{"age_ts":1646688043458}}
 $PX-Ygs_gUG8yA23teSsq4tdSPZop6_OTMznkVN3wLmE | {"auth_events":["$aY6xQW1MlBm-bKLYdILCNmf4S41zZcjYRP6g3sLUPRs","$-OMXkN0TXRMmWPrA4mC3peRs8NBrSSicw-f31ZiNp-A"],"prev_events":["$-OMXkN0TXRMmWPrA4mC3peRs8NBrSSicw-f31ZiNp-A"],"type":"m.room.power_levels","room_id":"!MGBZCfpOdcjbvgNnzs:my.server","sender":"@leprasmurf:my.server","content":{"users":{"@leprasmurf:my.server":100},"users_default":0,"events":{"m.room.name":50,"m.room.power_levels":100,"m.room.history_visibility":100,"m.room.canonical_alias":50,"m.room.avatar":50,"m.room.tombstone":100,"m.room.server_acl":100,"m.room.encryption":100},"events_default":0,"state_default":50,"ban":50,"kick":50,"redact":50,"invite":50,"historical":100},"depth":3,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646690184689,"hashes":{"sha256":"jC3X8J93+RQXMn5Z3t4w49TmQhb26dNzhsDeaSOap7E"},"signatures":{"my.server":{"ed25519:a_bifX":"/AONBV+XWozYy7B9PGnFC4Y4yH4Qrw4PKkR3jJY2sYumwApPdYrreJGXU96MkfN0fuvzrvUyRZCEYv/1fcxABQ"}},"unsigned":{"age_ts":1646690184689}}
 $wGN3W2Iyc13cGTfhbYzc8JNBh18EM3y9HXfrpggcpeM | {"auth_events":[],"prev_events":[],"type":"m.room.create","room_id":"!aSchXJzOhbWWvUMZnh:my.server","sender":"@leprasmurf:my.server","content": {"room_version":"6","creator":"@leprasmurf:my.server"},"depth":1,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646688043077,"hashes":{"sha256":"P63tTfxvnx1IzVfixvu6JAwxVjP0N8TInck0yaKq2ZQ"},"signatures":{"my.server":{"ed25519:a_bifX":"MCIDqfsv+76F2mvS50w90eB6r0iVJt/7MFWcn73BHsk/d7q30IFZu+D/H77Fl2UfTvzIOHERA01tRTJ5itdADg"}},"unsigned":{"age_ts":1646688043077}}
 $itbvWg60S8FEN7_P6Nv6HOdDTVjHhZRQUYGQ6YAFu7E | {"auth_events":["$PX-Ygs_gUG8yA23teSsq4tdSPZop6_OTMznkVN3wLmE","$aY6xQW1MlBm-bKLYdILCNmf4S41zZcjYRP6g3sLUPRs","$-OMXkN0TXRMmWPrA4mC3peRs8NBrSSicw-f31ZiNp-A"],"prev_events":["$6aUtopwmiEIAW0n9sPcfQb4GwStijdAfCJY83foEZpI"],"type":"m.room.topic","room_id":"!MGBZCfpOdcjbvgNnzs:my.server","sender":"@leprasmurf:my.server","content":{"topic":"World of Warcraft - private server"},"depth":8,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646690185289,"hashes":{"sha256":"H8+3kc9bYlb+h3WOHAwFjIVYgu9lhHkFsbDDPJhk3c4"},"signatures":{"my.server":{"ed25519:a_bifX":"oVOohsVBm5oIHOgBKyFbmOMt6cwC0Mhd1hqbC59Lb6zlnDFhnLdz7cmHq9fcw9xj4lTGVz5xWVZEA88f+Jp3DQ"}},"unsigned":{"age_ts":1646690185289}}
 $MfhPI_SQVQwAugiPHb7zj9Cg6xtOEjZmIxTTxAg6thE | {"auth_events":["$yLyf4zx5rUmOP4PKD8otDMEUufPbby01rJ8NkiNZ4pY","$WK4hwEqpQf56JhbaJ5CBd7frxqgnGhSXRGnEEoFaQDw","$Xhh5kXBX6oKWA9ouwMThemUs3LF-USoBQRPM7WqZyU4"],"prev_events":["$yLyf4zx5rUmOP4PKD8otDMEUufPbby01rJ8NkiNZ4pY"],"type":"m.room.join_rules","room_id":"!GYcqtcCjnzGdnbBUnr:my.server","sender":"@leprasmurf:my.server","content":{"join_rule":"invite"},"depth":4,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646690208601,"hashes":{"sha256":"XEg3sFPqTa+rVbgUuveh7IgHOELDmNRhAlB/RKW+ICQ"},"signatures":{"my.server":{"ed25519:a_bifX":"f8Mkf9oAziYMyPUlWlF9O5iF8VGcR/RnmTJj2WKMUa4cFS3cvRhetym3y+PVqRKNnuugDqGvSg7NOTLRZkRSAw"}},"unsigned":{"age_ts":1646690208601}}
(19 rows)

leprasmurf avatar May 31 '22 12:05 leprasmurf

Thanks for digging that out. Those are all state events and aren't redacted, which is interesting.

Event $MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s in room ![room_id]:[my.server] from the database appears to have been modified (calculated event id $GvkDX05onksbLfcEm7xvmDk50DjL9CrZ6tCvLoHYX70)

If I pick out $MWzu5qTxWhjEnsdpdYoH9OJfO4XungQeuli8zJ7Ij-s from that error and compute its event ID manually, I get $GvkDX05onksbLfcEm7xvmDk50DjL9CrZ6tCvLoHYX70 (unsurprising, since that's what the error says):

>>> import json
>>>
>>> from synapse.api.room_versions import RoomVersions
>>> from synapse.events import make_event_from_dict
>>>
>>> event_json = '{"auth_events":[],"prev_events":[],"type":"m.room.create","room_id":"!dRWSKyIQudsuxFruPU:my.server","sender":"@leprasmurf:my.server","content":{"room_version":"6","creator":"@leprasmurf:my.server"},"depth":1,"prev_state":[],"state_key":"","origin":"my.server","origin_server_ts":1646687336032,"hashes":{"sha256":"PY8bozlR64n/NlBEW6dPUkMsYOz+EPCrkFiC5oYtOVs"},"signatures":{"my.server":{"ed25519:a_bifX":"q0NhbZbvhh2QqAX1oBv/S5Ngy2kZEVbcgXsPR72okdG99cEwXa/VHUWxVI9jNX1IlC1A9ic0M0zPdxYS8Kt3CA"}},"unsigned":{"age_ts":1646687336032}}'
>>> event_json = event_json.replace("my.server", "<actual server name>")
>>> event_dict = json.loads(event_json)
>>>
>>> event = make_event_from_dict(event_dict, RoomVersions.V6)
>>> print(event.event_id)
$GvkDX05onksbLfcEm7xvmDk50DjL9CrZ6tCvLoHYX70

More interestingly, the hashes field of the event is also incorrect. It's "hashes":{"sha256":"PY8bozlR64n/NlBEW6dPUkMsYOz+EPCrkFiC5oYtOVs"},, but calculating the hash manually gives hw7pNzyBZ6lq1YTLvw+dsunKzylgFAT/MkOL2aqeDFQ.

>>> import hashlib
>>> from unpaddedbase64 import encode_base64
>>> from synapse.crypto.event_signing import compute_content_hash
>>>
>>> print(encode_base64(compute_content_hash(event.get_pdu_json(), hashlib.sha256)[1]))
hw7pNzyBZ6lq1YTLvw+dsunKzylgFAT/MkOL2aqeDFQ

Apart from that, the event JSON looks perfectly normal and reasonable.

The events are all from the same hour (20:56:58 - 21:56:48 UTC) on 7th March 2022, which isn't too long ago. prune_event_dict hasn't been changed that recently and wouldn't affect m.room.create events so I'm going to rule out that explanation.

All of the events in question have similarly incorrect hashes, so either:

  • they were all modified after the event was hashed and signed, which seems unlikely since their JSON looks normal
  • or encode_canonical_json or hashlib.sha256 were producing incorrect results for a while?

squahtx avatar May 31 '22 13:05 squahtx

It's really hard to know how to proceed here. We are at a loss to explain how these events could have ended up with incorrect event ids, or think about how we could repeat it. We've also not heard of anything like this corruption from anyone else, so it's hard to justify spending a lot of time investigating.

On the upside, I think now that the sanity checks are in place (https://github.com/matrix-org/synapse/pull/12620), any recurrence should be visible sooner.

richvdh avatar Jun 13 '22 09:06 richvdh

I'm going to close this for now, as we seem to have exausted avenues for explanation. @leprasmurf let us know if you see any recurrence.

richvdh avatar Aug 08 '22 12:08 richvdh