Skip to content

fix: resolve TDZ race on getClient/getClientUser during ready event - #45

Open
SpackledJim wants to merge 1 commit into
cbackas:mainfrom
SpackledJim:fix/getclient-tdz-race
Open

SpackledJim wants to merge 1 commit into
cbackas:mainfrom
SpackledJim:fix/getclient-tdz-race

Conversation

@SpackledJim

Copy link
Copy Markdown

Bot crashes on ready: getClientUser accessed before initialization

Commit: 5a6e4c5 — fix: resolve TDZ race on getClient/getClientUser during ready event

Symptom

After login, the bot exits on the ready event:

error: Uncaught ReferenceError: Cannot access 'getClientUser' before initialization
    at setTVDBLoadingActivity (src/lib/discordActivities.ts:53:22)
    at Client.<anonymous> (src/app.ts:34:3)

Reproduced consistently on 4.1.2 and 4.2.0 (Deno 2.5.6, Docker).

Cause

app.ts exports its client accessors on the last lines of the module, after await discordClient.login(token). The ready handler can run while that await is pending, so discordActivities.ts calls the accessor before its binding is initialized. The circular import between the two modules is why this surfaces as a TDZ error.

Fix

  • app.ts: move export const getClient to directly after the client is constructed, before handlers are registered and login() is called. Remove the trailing getClientUser export.
  • discordActivities.ts: import getClient and derive the user locally:
function getClientUser() {
  const user = getClient().user
  if (user == null) throw new Error("Fatal: Client user is null")
  return user
}

discordActivities.ts imported a getClientUser helper from app.ts that
either didn't exist (as shipped in 4.1.2) or, in a later partial fix on
main, was exported after `await discordClient.login(token)` — so any
`ready`-event handler calling it before that final line executed threw
"Cannot access '...' before initialization" instead of setting the
bot's activity.

Move the `getClient` export up to immediately after the Client is
constructed, before event handlers are registered or login() is
called, so it's initialized before any event can fire. Derive the
client user in discordActivities.ts via a local getClientUser()
helper built on getClient().user instead of relying on a second export
from app.ts.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S9oMx1pYL6igWuytQyqSTa
@cbackas

cbackas commented Sep 29, 2026

Copy link
Copy Markdown
Owner

You encounter this every time you run the container? Not to say this bug isn't real but I've never seen this issue and have had this connection logic in 'prod' for years

@SpackledJim

Copy link
Copy Markdown
Author

I did, yeah.

This is timing-dependent (gateway/network latency), which likely explains why it doesn't reproduce for everyone every time, but reproduces consistently in a fast local Docker setup.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants