Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Possible panic fix#112

Merged
FZambia merged 2 commits intocentrifugal:panic_fixfrom
C-Pro:disconnect-panic
Jun 22, 2025
Merged

Possible panic fix#112
FZambia merged 2 commits intocentrifugal:panic_fixfrom
C-Pro:disconnect-panic

Conversation

@C-Pro
Copy link
Contributor

@C-Pro C-Pro commented Jun 20, 2025

I have encountered a panic in production, I believe it happened when some kind of network error happened concurrently with normal client.Close() on a client that had some callbacks in the queue.

My theory is that client.Close set c.cbQueue = nil at the same time as client.handleDisconnect running in a goroutine attempted to push to the queue. This is how test fails before my "fix":

$ go test -v -count=1 -run ^TestConcurrentCloseDisconnect$ .
=== RUN   TestConcurrentCloseDisconnect
--- FAIL: TestConcurrentCloseDisconnect (2.20s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102950e00]

goroutine 5 [running]:
testing.tRunner.func1.2({0x102a8ea40, 0x102dc9c30})
        /opt/homebrew/Cellar/go/1.24.2/libexec/src/testing/testing.go:1734 +0x1ac
testing.tRunner.func1()
        /opt/homebrew/Cellar/go/1.24.2/libexec/src/testing/testing.go:1737 +0x334
panic({0x102a8ea40?, 0x102dc9c30?})
        /opt/homebrew/Cellar/go/1.24.2/libexec/src/runtime/panic.go:792 +0x124
github.com/centrifugal/centrifuge-go.(*cbQueue).pushOrClose(0x140022ce5a8?, 0x102630bb4?, 0x8?)
        /Users/user/work/experiments/centrifuge-go/queue.go:73 +0x20
github.com/centrifugal/centrifuge-go.(*cbQueue).push(...)
        /Users/user/work/experiments/centrifuge-go/queue.go:58
github.com/centrifugal/centrifuge-go.(*Client).runHandlerAsync(...)
        /Users/user/work/experiments/centrifuge-go/client.go:783
github.com/centrifugal/centrifuge-go.(*Subscription).moveToSubscribing(0x140022ce5a0, 0x1, {0x1029a2325, 0x10})
        /Users/user/work/experiments/centrifuge-go/subscription.go:452 +0x1e0
github.com/centrifugal/centrifuge-go.(*Client).moveToDisconnected(0x1400221ed88, 0x1, {0x1029a2325, 0x10})
        /Users/user/work/experiments/centrifuge-go/client.go:465 +0x4d0
github.com/centrifugal/centrifuge-go.(*Client).handleDisconnect(0x140022ce5a0?, 0x10299fcb0?)
        /Users/user/work/experiments/centrifuge-go/client.go:734 +0x80
github.com/centrifugal/centrifuge-go.TestConcurrentCloseDisconnect(0x14000003500)
        /Users/user/work/experiments/centrifuge-go/client_test.go:358 +0x68
testing.tRunner(0x14000003500, 0x102b0d710)
        /opt/homebrew/Cellar/go/1.24.2/libexec/src/testing/testing.go:1792 +0xe4
created by testing.(*T).Run in goroutine 1
        /opt/homebrew/Cellar/go/1.24.2/libexec/src/testing/testing.go:1851 +0x374
FAIL    github.com/centrifugal/centrifuge-go    3.200s
FAIL

I am not sure that my "fix" is a proper one though, as callbacks won't be run in case Close has happened before.
I suspect there is a better solution.

@FZambia FZambia changed the base branch from master to panic_fix June 22, 2025 09:45
@FZambia FZambia merged commit 90b2bc2 into centrifugal:panic_fix Jun 22, 2025
3 checks passed
@FZambia
Copy link
Member

FZambia commented Jun 22, 2025

@C-Pro hi, many thanks!

I think it's a reasonable fix, and it should cover other possible races as well. Should be OK behaviour-wise since client is unusable after close. As you noticed, made a separate PR with a couple of adjustments.

@C-Pro
Copy link
Contributor Author

C-Pro commented Jun 22, 2025

My concern of not executing callbacks is that users may rely on it to be guaranteed. E.g. incrementing metric on subscribe and decrementing on unsubscribe will lead to something resembling "leaking" subscriptions if on unsubscribe callback is sometimes lost.

@FZambia
Copy link
Member

FZambia commented Jun 22, 2025

Generally callbacks still should be called, whether by disconnect or by close logic. They are submitted to the queue before making the queue nil upon close.

FZambia added a commit that referenced this pull request Jun 22, 2025
@FZambia
Copy link
Member

FZambia commented Jun 22, 2025

Released in https://github.com/centrifugal/centrifuge-go/releases/tag/v0.10.9

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

Comments