mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #3886] perf: gen_random_string causes high CPU usage due to 75% byte rejection in tr #2627
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#2627
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Kookiejarz on GitHub (Mar 5, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/3886
Is your feature request related to a problem?
During installation, gen_random_string pipes /dev/urandom directly into
tr -dc 'a-zA-Z0-9'. Since only ~24% of random bytes pass the filter,
tr discards ~75% of all input and busy-loops at high CPU until the pipe closes.
This causes a noticeable CPU spike on every fresh install or credential regeneration, especially noticeable on low-spec VPS ( less than 1 vCPU / 512MB RAM), where tr can saturate the single core for several seconds during installation. An example is on Google Cloud Platform's e2-Micro Instance.
Describe the solution you'd like
Replace the current /dev/urandom | tr pipeline with openssl rand -base64,
which outputs A-Za-z0-9+/= only, reducing tr's rejection rate from ~75% to ~2%:
Describe alternatives you've considered
Here is a python alternative, cleanest but requires python pre-installed:
No CPU idle time issues, with guaranteed length precision. Recommended for use when scripts can rely on Python.
I didn't apply this method because this is a shell installation script and the goal is to keep it self-contained. Adding Python3 as a dependency would pull in a full runtime or a single use case, which is disproportionate for a lightweight install script.
So, OpenSSL is the pragmatic choice: one explicit new dependency across all distros, but a minimal and justified one — and far safer than relying on Python being present. :)
Checklist
@nebulosa2007 commented on GitHub (Mar 16, 2026):
without new dependencies
@Kookiejarz commented on GitHub (Mar 16, 2026):
Fair point on readability! The tradeoff I was considering: $RANDOM is a
15-bit PRNG seeded from PID+time, which could be a concern if these strings
ever end up in auth contexts. Since openssl is already a hard dep here
(via SSL cert management), I went with the cryptographic backing to be safe
without introducing anything new. :D