matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.82k stars 2.13k forks source link

Occasional UniqueViolation in device_lists_remote_cache_unique_id #16243

Open DMRobertson opened 1 year ago

DMRobertson commented 1 year ago

e.g.

From looking at the source, the only insert that isn't an upsert I can see is

https://github.com/matrix-org/synapse/blob/69048f7b4848ab6a4ae6cb233f8cbf36d73c0ba1/synapse/storage/databases/main/devices.py#L1911-L1919

which might emit this error if

Tracebacks in Sentry seems to corroborate my suspicions --- they have the quoted function in their stack.

DMRobertson commented 1 year ago

this (user, device) pair already exists in the DB.

Although you'd expect that to be ensured by https://github.com/matrix-org/synapse/blob/69048f7b4848ab6a4ae6cb233f8cbf36d73c0ba1/synapse/storage/databases/main/devices.py#L1907-L1909 if we're using REPEATABLE READ isolation

DMRobertson commented 1 year ago

Spotted in https://github.com/matrix-org/synapse/issues/16138#issuecomment-1705533637

DMRobertson commented 1 year ago

I also spotted SerializationFailures for this table:

erikjohnston commented 1 year ago

This is probably happening when we call that function twice at the same time, while having no existing entries in the DB. That way the DELETE is a no-op that doesn't lock any rows, so both transactions try to insert