Fix nil dereference in awaitResponse when the connection closes - #21
Merged
dank merged 1 commit intoSep 17, 2026
Merged
Conversation
Close() closes the response channel of every pending request. A receive on a closed channel yields nil, and awaitResponse read response.Error from it without a check, so any request that was still waiting when the connection closed panicked the calling goroutine. Close() runs on an explicit call, on a read error (for example when PsyNet drops the socket on DuplicateLogin) and on a pong timeout. awaitResponse now returns ctx.Err() if the context ended, or the new ErrConnectionClosed otherwise. Add two tests that panic without the fix: Close() with a pending request, and the server dropping the connection.
Owner
|
Nice, thanks! |
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.
Fixes #20.
Problem
Close()closes the response channel of every pending request. A receive on a closed channel yieldsnil, andawaitResponsereadresponse.Errorfrom it with no check. Any request that was still waiting when the connection closed (explicitClose(), a read error such as PsyNet'sDuplicateLogindrop, or the pong timeout) panicked the calling goroutine.Change
awaitResponsechecks the receive. If the channel was closed with no response, it returnsctx.Err()when the context has ended, and the newErrConnectionClosedotherwise. Thectx.Err()branch covers the cleanup goroutine insendRequestAsync, which closes the same channel onctx.Done().ErrConnectionClosedis exported so that callers can useerrors.Isto tell "reconnect and retry" apart from a PsyNet error.Tests
Both new tests panic on
masterand pass with the fix:TestPsyNetRPC_CloseWithPendingRequest: waits until the request is inpendingReqs, callsrpc.Close(), expectsErrConnectionClosed.TestPsyNetRPC_ServerDropsConnectionWithPendingRequest: the mock server closes the connection when the request arrives, soClose()runs fromreadMessages. ExpectsErrConnectionClosedandIsConnected() == false. This needed one small option onMockWSServer(SetCloseOnRequest).Locally (Go 1.26.8, Windows):
gofmt -l .is empty,go vet ./...is clean, the two new tests pass with-count=20, and the full suite passes with-count=3. I could not run-raceon this machine (no C compiler for cgo), so that part is left to CI.