Repository navigation
driver/memcached: bound the binary handshake by DialTimeout (v1.6.0, Refs #1213) - #1217
Merged
Merged
Conversation
|
Caution Review failedThe pull request is closed.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
…ndshake at once and comes back as the caller's context error (Refs #1213)
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
FumingPower3925
marked this pull request as ready for review
November 1, 2026 21:12
This was referenced Nov 1, 2026
Merged
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.
driver/memcached: bound the binary handshake by DialTimeout. Refs #1213 (found by the review of #1209; same class as #1205).
The problem
DialTimeout(documented as "the TCP dial timeout") only coverednet.Dialer.DialContext. The binary-protocol handshake (handshake: a VERSION round trip that confirms the server speaks binary) ran on the caller's context, so a server that accepts TCP and never answers it held the dial, and any command that needed a new conn, until the caller's context ended: for ever undercontext.Background(). The text protocol has no handshake and is not affected.The change
memcachedConn.handshakeruns the VERSION round trip undercontext.WithTimeout(ctx, DialTimeout)(5 s when unset,defaultDialTimeout), exactly the shape of #1209 for redis. All three dial paths end in it (dialDirectMemcachedConn,dialMemcachedConnon unix,dialMemcachedConnNetConn), and the pool's dials use those, soClientcommands and the cluster's node clients are bounded alike. Nothing else indriver/memcachedruns a handshake (grep 'handshake(' driver/memcached: the three callers).Config.DialTimeout(WithDialTimeout,ClusterConfig.DialTimeout). Their docs now say it bounds the TCP dial and, separately, the binary handshake; a slow connect followed by a silent server takes up to about 2 xDialTimeout.celeris-memcached: handshake timed out after <d>: context deadline exceeded(on the direct path the socket's owni/o timeoutis appended; the socket deadline fires a hair before the context timer, handled as in driver/redis: bound the connection handshake by DialTimeout (v1.6.0, Refs #1205) #1209).Client-> pool ->dial-> the three dials above;TestClientBoundsTheBinaryHandshake1213goes throughNewClient(... WithProtocol(ProtocolBinary), WithDialTimeout(500ms))+Version(context.Background()).Commits:
8f5b3c3the tests (RED on main),5c5fc6athe fix.handshake_default_1213_test.go(the test of the unset-DialTimeoutdefault, which uses the newtestHookDefaultHandshakeTimeout, as redis does) is in the second commit because it does not compile on main.RED / GREEN
RED,
8f5b3c3alone (macOS arm64,go test ./driver/memcached -run 1213 -v):GREEN,
5c5fc6a: all pass; the three dials return after 500 ms (DialTimeout500 ms), the caller-deadline ones after 200 ms, the default-timeout one after 300 ms (the hook lowers the 5 s default). Linux 2 CPU-race -count=20: the bounded dials returned after 500-510 ms.Each test takes under 1 s (the longest, 0.5 s of bound, runs its three paths in parallel; the "answered" guard sleeps 0.4 s past a 0.3 s
DialTimeoutto show the conn is still good).Mutants (each applied alone to
5c5fc6a,go test -run 1213 -count=3)DialTimeoutis unsethctx.Err()first is a race; it fails the bounded test (direct) in some runs only (macOS: about 1 in 10 at-count=10, survives-count=3). Same as redis M5 in #1209.context.DeadlineExceededChecks
driver/memcachedpackage,-race -count=1, Linux dockergolang:1.27.3--cpus 2: ok.-race -run 1213 -count=20: Linux ok, macOS ok (44 s).GOOS=windows go vet ./driver/memcached: clean.golangci-lintv2.15.0GOOS=linux: nothing in the files of this PR (oneerrcheckfinding in the untouchedclose_after_teardown_859_linux_test.gois on main too).Behaviour notes
Release note
DialTimeout(default 5 s) now also bounds the binary-protocol handshake of every new memcached conn: a server that accepts the connection and does not answer the VERSION handshake used to hold the dial, and any command that needed a new conn, until the caller's context ended (for ever undercontext.Background()). A server that takes longer thanDialTimeoutto answer now fails the dial with an error matchingcontext.DeadlineExceeded, also when the caller's context has a longer deadline or none. Text-protocol clients are not affected.Review r1 follow-up (commit
f69b19c): the direct path, the one the pool usesThe pool always dials direct (
pool.go). There a caller's cancellation was ignored untilDialTimeout, and the caller then got a bareread tcp ...: i/o timeoutthat matched neithercontext.Cancelednorcontext.DeadlineExceeded. Same approach as #1216: for a direct connhandshakearmscontext.AfterFunc(hctx, ...)that sets a deadline of "now" on the socket, which wakes the blocked read; the error comes back wrapped with the caller's context error (context.Canceled, orcontext.DeadlineExceededfor a caller's deadline that fires on the socket a hair before the context reports it). A wake that raced with a handshake that had just succeeded is undone, so the conn is not left with a deadline in the past (a race-only guard; not reachable by a deterministic test).TestBinaryHandshakeKeepsTheCallersCancellation1213now includes the direct path (RED on5c5fc6a: "still blocked 2s after the cancel"), and...KeepsTheCallersShorterDeadline1213requireserrors.Is(err, context.DeadlineExceeded)on all three paths (it failed on the direct path with the bare i/o timeout).Mutants on
f69b19c: no wake (AfterFuncreplaced by a no-op) fails the cancellation (direct) and shorter-deadline (direct) tests; no wrapping of the caller's error fails the same two.Checks:
-race -run 1213 -count=20Linux docker ok, macOS-count=30ok (a first version raced withClosesettingc.tcp = nil: the wake uses a copy of the socket); wholedriver/memcached-raceLinux ok;GOOS=windows go vetclean; golangci-lint only the pre-existingerrcheckfinding.