fix(discord): avoid unnecessary members intent on startup + fix token lock leak - #5302
Merged
Conversation
Only request the privileged members intent when DISCORD_ALLOWED_USERS includes non-numeric entries that need username resolution. Also release the Discord token lock when startup fails so retries and restarts are not blocked by a stale lock.\n\nAdds regression tests for conditional intents and startup lock cleanup.
binhnt92
added a commit
to binhnt92/hermes-agent
that referenced
this pull request
Apr 6, 2026
When Slack's connect() acquires a scoped lock on the app token and then fails (bad token, Socket Mode error), the lock is never released. The next gateway start sees "Slack app token already in use" and refuses to connect until the process dies. Same issue in Signal: if the health check fails after acquiring the phone lock, the lock is held permanently. Discord got this fix in PR NousResearch#5302. Slack and Signal were missed. Add lock release to both exception/failure paths. Extract Signal's inline release logic into a reusable _release_phone_lock() helper. Also close the leaked httpx client on Signal health check failure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Salvaged from PR #4690 by @abhey-gupta.
Two fixes to Discord adapter startup:
1. Conditional members intent
intents.members = Truewas always set, but the privileged Server Members intent is only needed when the allowed users list contains usernames that need resolution to numeric IDs. If the intent isn't enabled in the Discord Developer Portal, the bot can fail to come online entirely.Fix: Move allowed_users parsing before intents setup, then set
intents.membersonly when non-numeric entries exist in the allowlist.2. Token lock leak on connection failure
If
connect()hits aTimeoutErroror other exception after acquiring the token lock, the lock was never released — blocking future connection attempts until gateway restart.Fix: Add
release_scoped_lock()calls in both theTimeoutErrorand generalExceptionhandlers.Follow-up
ButtonStyle.grey(main addedExecApprovalViewafter the PR branched)Test plan
python -m pytest tests/gateway/test_discord_connect.py— 4/4 passedAttribution
Cherry-picked from @abhey-gupta (#4690), conflict resolved against current main.