Discord API · error code

Discord error 4000: Unknown error

RetryableGateway close codes (4000-4014)

Last verified against Discord Developer Docs - Opcodes and status codes

What Discord returns
We're not sure what went wrong. Try reconnecting?

What error 4000 means

The gateway closed your WebSocket without a specific reason. Discord's own description is deliberately vague, and in practice 4000 is the code you see when the connection dies for transport-level reasons: a Discord gateway node was restarted or rebalanced, your host's network dropped packets long enough to break the socket, a proxy or load balancer between you and Discord timed out an idle connection, or the server simply hit an internal error while handling your session.

It is marked reconnectable, and your library will almost always attempt to Resume automatically using the stored session_id, resume_gateway_url and last sequence number. If the resume succeeds you get a RESUMED event and replayed missed events; if it fails you get an INVALID_SESSION and a fresh IDENTIFY. A handful of 4000s per day on a long-running bot is normal. A storm of them points at your network or at an intermediary.

What it looks like

// WebSocket close frame (no JSON body)
code: 4000
reason: "Unknown error"

// what your library logs (discord.js style)
[WS => Shard 0] Connection closed with code 4000, attempting resume...

Why it happens

  • Discord gateway node restart or deployment
  • Network interruption on your host (container eviction, Wi-Fi, VPN reconnect)
  • Idle-timeout on a proxy or NAT in front of the bot that kills long-lived WebSockets
  • Heartbeat ACK never arrived because the event loop was blocked, and the library closed or Discord closed the socket
  • Occasional unexplained server-side hiccup

How to fix Discord error 4000

  1. 1Let the library resume; verify you see RESUMED in debug logs rather than a full re-IDENTIFY each time
  2. 2Check heartbeat health: if your process blocks the event loop (sync I/O, CPU-heavy work), heartbeats are late and disconnects follow
  3. 3If behind a proxy, raise or disable idle timeouts for the WebSocket, or connect directly
  4. 4Count 4000s per hour; if they are frequent, correlate with host CPU, memory and network metrics
  5. 5Make sure you reconnect to resume_gateway_url, not the original gateway URL, when resuming

How to stop it recurring

Keep the event loop responsive so heartbeats are always on time, run the bot on a host with stable egress, and enable your library's debug logging for shard lifecycle so you can tell resume from re-identify. Track disconnect counts as a metric rather than ignoring them.

Distinguish disconnect rate from disconnect existence: some 4000s are the cost of a long-lived socket, and a resume makes them invisible to users. The gateway timing rules that govern resumes and heartbeats are on the Discord limits page, and a bot that dies rather than resumes after 4000 should be checked against the offline checklist in Discord bot not responding.

Official reference: Discord Developer Docs - Opcodes and status codes. See all Discord error codes or the Discord limits and quotas.

Related codes

Error 4000 - quick answers

What does Discord error 4000 mean?

The gateway closed your WebSocket without a specific reason. Discord's own description is deliberately vague, and in practice 4000 is the code you see when the connection dies for transport-level reasons: a Discord gateway node was restarted or rebalanced, your host's network dropped packets long enough to break the socket, a proxy or load balancer between you and Discord timed out an idle connection, or the server s

How do I fix Discord error 4000?

1. Let the library resume; verify you see RESUMED in debug logs rather than a full re-IDENTIFY each time 2. Check heartbeat health: if your process blocks the event loop (sync I/O, CPU-heavy work), heartbeats are late and disconnects follow 3. If behind a proxy, raise or disable idle timeouts for the WebSocket, or connect directly 4. Count 4000s per hour; if they are frequent, correlate with host CPU, memory and network metrics 5. Make sure you reconnect to…

Can I retry after error 4000?

Yes - this is a retryable condition. Back off (start around one second and double on each attempt, with jitter) and watch for a retry-after hint from the platform before resending.

Stop debugging Discord by hand

Connect the channel through Conferbot: tokens, webhooks and retries are handled, failures show as readable status.