Anonymous Username - over 100 users

AI-generated summary

The discussion revolves around a bug in the username generation system for anonymous users in Discourse. Jono_Brain2 reports that usernames switch from the format anonymous100 to a random hexadecimal string (9ab9441ad3316f5d2d35cd4807317cc) once the number of anonymous users exceeds 100. This occurs due to a hardcoded limit of 100 attempts in the find_available_username_based_on method, which is considered short-sighted. codinghorror and Stephen note this is a known issue, referencing a prior report. lisbethw1130 confirms it remains unfixed, requiring manual name changes. sam acknowledges the complexity and shares a fix via a GitHub commit (a8fbb19e7c728f5f33d2b784530635791ea959e3), which Jono_Brain2 appreciates as a better solution than his proposed workaround.

We have really weird issue, up until user 100 the username was in this format

anonymousxxx
e.g. anonymous10, anonymous55, anonymous100

However as soon as we reached over number 100 anonymous user accounts, they are now in this format:

9ab9441ad3316f5d2d35cd4807317cc
01c3b651778b7871a7640511daa8eb8

Think i can see why looking at this code, but it seems a bit short sighted to only allow 100 random users?

def self.find_available_username_based_on(name, allow_username = nil)
    name = fix_username(name)
    i = 1
    attempt = name
    **until attempt == allow_username || User.username_available?(attempt) || i > 100**
      suffix = i.to_s
      max_length = User.username_length.end - suffix.length - 1
      attempt = "#{name[0..max_length]}#{suffix}"
      i += 1
    end
    until attempt == allow_username || User.username_available?(attempt) || i > 200
      attempt = SecureRandom.hex[1..SiteSetting.max_username_length]
      i += 1
    end
    attempt
  end

Is this an actual bug @sam?

Surprisingly not the first time this has been reported:

Think that last report went misunderstood, too.

yeah, it hasn’t been fixed, so we have to change the name every 99 times :sleepy:

I suggest we fix this @tgxworld

This is a tricky problem we need a different algorithm here.

Fixed per:

https://github.com/discourse/discourse/commit/a8fbb19e7c728f5f33d2b784530635791ea959e3

Thanks guys.

I was going to look at doing a change so when the username is ‘anonymous’ it works slightly different and checks for the last one created. But @sam 's solution seems a lot better.