Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix guest registration #6420

Merged
merged 3 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6420.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix broken guest registration when there are existing blocks of numeric user IDs.
18 changes: 6 additions & 12 deletions synapse/storage/data_stores/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import re

from six import iterkeys
from six.moves import range

from twisted.internet import defer
from twisted.internet.defer import Deferred
Expand Down Expand Up @@ -482,12 +481,8 @@ def find_next_generated_user_id_localpart(self):
"""
Gets the localpart of the next generated user ID.

Generated user IDs are integers, and we aim for them to be as small as
we can. Unfortunately, it's possible some of them are already taken by
existing users, and there may be gaps in the already taken range. This
function returns the start of the first allocatable gap. This is to
avoid the case of ID 1000 being pre-allocated and starting at 1001 while
0-999 are available.
Generated user IDs are integers, so we find the largest integer user ID
already taken and return that plus one.
"""

def _find_next_generated_user_id(txn):
Expand All @@ -497,15 +492,14 @@ def _find_next_generated_user_id(txn):

regex = re.compile(r"^@(\d+):")

found = set()
max_found = 0

for (user_id,) in txn:
match = regex.search(user_id)
if match:
found.add(int(match.group(1)))
for i in range(len(found) + 1):
if i not in found:
return i
max_found = max(int(match.group(1)), max_found)

return max_found + 1

return (
(
Expand Down