Skip to content

[FIXED] Nil account panic in processLeafNodeConnect#7991

Open
umut-polat wants to merge 1 commit intonats-io:mainfrom
umut-polat:fix/leafnode-nil-account
Open

[FIXED] Nil account panic in processLeafNodeConnect#7991
umut-polat wants to merge 1 commit intonats-io:mainfrom
umut-polat:fix/leafnode-nil-account

Conversation

@umut-polat
Copy link
Copy Markdown

What

Fixes a nil pointer panic in processLeafNodeConnect when c.acc is nil.

Stack trace from #7989

panic: runtime error: invalid memory address or nil pointer dereference

goroutine 100415 [running]:
github.com/nats-io/nats-server/v2/server.(*Account).registerLeafNodeCluster(0x0, ...)
        server/accounts.go:993 +0x2d
github.com/nats-io/nats-server/v2/server.(*client).processLeafNodeConnect(...)
        server/leafnode.go:2159 +0x595

The logs show Authentication Timeout followed by connection closed right before the panic. The account was never set on the client because the connection was closed during auth.

Fix

Add a nil guard for acc after reading c.acc, consistent with the same pattern used in other code paths within the same file (lines 2259, 2758, 2890).

Resolves #7989

@umut-polat umut-polat requested a review from a team as a code owner March 28, 2026 11:24
Copy link
Copy Markdown
Member

@MauriceVanVeen MauriceVanVeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but would it be possible to add a test for this?

@umut-polat umut-polat force-pushed the fix/leafnode-nil-account branch from 590acb6 to 59d6d38 Compare March 28, 2026 11:40
@umut-polat
Copy link
Copy Markdown
Author

added a test that races a CONNECT with a very short auth timeout (1ms) to exercise the nil account guard. the exact race from #7989 is hard to reproduce deterministically since it depends on goroutine scheduling, but the test covers the defensive path.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 59d6d381de

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// timeout while still being processed), bail out to avoid a panic.
if acc == nil {
c.closeConnection(MissingAccount)
return ErrMissingAccount
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Return nil after closing nil-account leaf connections

In processLeafNodeConnect, the new nil-account branch closes the connection and then returns ErrMissingAccount. The parser read loop treats non-whitelisted errors as protocol errors (client.go read loop around parse()), so this path logs an error and attempts a ProtocolViolation close even when this is just the auth-timeout race you are handling. That creates noisy/misclassified errors compared with adjacent leaf handlers that close and return nil for equivalent pre-CONNECT races.

Useful? React with 👍 / 👎.

@neilalexander
Copy link
Copy Markdown
Member

When a leafnode connection is closed during authentication (e.g. auth
timeout), the account may not have been set on the client yet. If
processLeafNodeConnect continues to run after the connection was
closed, it dereferences c.acc in registerLeafNodeCluster, causing a
nil pointer panic.

Add a nil check for the account before proceeding, consistent with
other code paths in the same file (initLeafNodeSmapAndSendSubs,
checkJetStreamExportReconcile) that already guard against this.

Add a test that races a CONNECT with an expired auth timeout to
exercise the guard.

Resolves nats-io#7989

Signed-off-by: umut-polat <[email protected]>
@umut-polat umut-polat force-pushed the fix/leafnode-nil-account branch from 59d6d38 to 8353a69 Compare April 1, 2026 15:24
@umut-polat
Copy link
Copy Markdown
Author

done — added sign-off.

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.

panic: runtime error: invalid memory address or nil pointer dereference

3 participants