Skip to content

generateUniqueUsername issues one query per candidate and can make 101 round trips for a single sign-in #790

Closed

Description

@chonilius

src/users/users.service.ts:149-161:

  private async generateUniqueUsername(base: string): Promise<string> {
    let candidate = base;
    let counter = 0;
    while (await this.userRepo.findOne({ where: { username: candidate } })) {
      counter += 1;
      candidate = `${base}-${counter}`;
      if (counter > 100) { candidate = `${base}-${Date.now()}-${counter}`; break; }
    }
    return candidate;
  }

One SELECT per iteration, up to 101 of them, on the OAuth sign-in path.

Why it matters: it is also racy in a way the loop cannot fix — two concurrent sign-ins for the same colliding login both settle on login-1, and the second insert violates the unique constraint, which nothing catches. So the loop pays up to 101 queries for a result that is still not guaranteed.

upsertFromGithub performs several other sequential existence checks for the same reason (:67-69, :88-90, :109-111, :118-120), so a single sign-in can be a dozen round trips before anything is written.

Fix: let the database decide — insert with the preferred username, catch the unique violation, and retry with a short random suffix. That is one query in the common case, correct under concurrency, and removes the arbitrary 100 cap. The Date.now() escape hatch can go with it.

Activity

  1. added
    performance
    on Nov 12, 2026
  2. added
    Stellar Wave
    on Nov 19, 2026
  3. Divine639700 commented on Nov 19, 2026

    @Divine639700

    @Divine639700 has applied to work on this issue as part of the Stellar Wave Program's 11th wave.

    Hi maintainer, I’d like to optimize generateUniqueUsername by replacing repeated database lookups with an insert-and-retry approach that handles unique constraint violations safely. I’ll also address the sequential existence checks in upsertFromGithub and add tests covering concurrent sign-ins, username collisions, and retry behaviour

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Divine639700 to this issue.

  4. jd-here-007 commented on Nov 19, 2026

    @jd-here-007
    Contributor

    @jd-here-007 has applied to work on this issue as part of the Stellar Wave Program's 11th wave.

    hi maintainer let me fix this please

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @jd-here-007 to this issue.

  5. drips-wave commented on Nov 19, 2026

    @drips-wave

    Congratulations, @jd-here-007! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on November 25, 2026.

    🧑‍💻 @jd-here-007: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  6. drips-wave commented on Nov 20, 2026

    @drips-wave

    This issue has been completed by @jd-here-007 as part of the Stellar Wave Program's 11th Wave 🥳

    😎 @jd-here-007: You earned 200 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar Waveperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions