-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: run MONITOR on a dedicated connection and propagate read errors #3901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ankit-songara
wants to merge
6
commits into
redis:master
Choose a base branch
from
ankit-songara:fix-monitor-readtimeout-hang
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fbdeae2
fix: close monitor channel on read error so listeners don't block for…
Ankitsongara2003 c6860db
gofmt: indent Monitor doc comment list
Ankitsongara2003 53941c2
fix: run MONITOR on a dedicated connection and propagate read errors
Ankitsongara2003 a54004d
fix: address review findings on monitor connection teardown
Ankitsongara2003 dbdc2e3
Merge branch 'master' into fix-monitor-readtimeout-hang
ankit-songara 4176bec
Merge branch 'master' into fix-monitor-readtimeout-hang
ankit-songara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package redis_test | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "net" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/redis/go-redis/v9" | ||
| ) | ||
|
|
||
| // fakeMonitorServer implements just enough of the Redis protocol for the | ||
| // MONITOR command: it acknowledges the handshake, replies +OK to MONITOR, | ||
| // streams one monitor line and then closes the connection, simulating the | ||
| // server (or the network) dropping a monitor connection. | ||
| func fakeMonitorServer(ln net.Listener) { | ||
| for { | ||
| conn, err := ln.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| go func(c net.Conn) { | ||
| defer c.Close() | ||
| rd := bufio.NewReader(c) | ||
| for { | ||
| line, err := rd.ReadString('\n') | ||
| if err != nil { | ||
| return | ||
| } | ||
| if line[0] == '*' || line[0] == '$' { | ||
| continue // RESP framing, only react to command verbs | ||
| } | ||
| switch { | ||
| case strings.HasPrefix(strings.ToLower(line), "hello"): | ||
| c.Write([]byte("-ERR unknown command 'hello'\r\n")) | ||
| case strings.HasPrefix(strings.ToLower(line), "monitor"): | ||
| c.Write([]byte("+OK\r\n")) | ||
| c.Write([]byte("+1700000000.000000 [0 127.0.0.1:1] \"set\" \"foo\" \"bar\"\r\n")) | ||
| return // deferred Close drops the connection mid-monitor | ||
| default: | ||
| c.Write([]byte("+OK\r\n")) | ||
| } | ||
| } | ||
| }(conn) | ||
| } | ||
| } | ||
|
|
||
| // See https://github.com/redis/go-redis/issues/3079: when the connection | ||
| // backing MONITOR dies, the monitor channel must be closed so a listener is | ||
| // not blocked forever, and the error must be available via cmd.Err(). | ||
| func TestMonitorConnErrorClosesChannel(t *testing.T) { | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer ln.Close() | ||
| go fakeMonitorServer(ln) | ||
|
|
||
| client := redis.NewClient(&redis.Options{ | ||
| Addr: ln.Addr().String(), | ||
| }) | ||
| defer client.Close() | ||
|
|
||
| ch := make(chan string, 100) | ||
| cmd := client.Monitor(context.Background(), ch) | ||
| if cmd.Err() != nil { | ||
| t.Fatal(cmd.Err()) | ||
| } | ||
| cmd.Start() | ||
| defer cmd.Stop() | ||
|
|
||
| deadline := time.After(10 * time.Second) | ||
| for { | ||
| select { | ||
| case _, ok := <-ch: | ||
| if !ok { | ||
| if cmd.Err() == nil { | ||
| t.Fatal("channel closed but cmd.Err() is nil") | ||
| } | ||
| return // unblocked with an error, as expected | ||
| } | ||
| case <-deadline: | ||
| t.Fatal("monitor channel was not closed after the connection died; listener would block forever") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A clean Stop must not report an error and must not require any server | ||
| // traffic to take effect: closing the dedicated connection unblocks the | ||
| // reader goroutine even when the server is idle. | ||
| func TestMonitorStopWithoutTraffic(t *testing.T) { | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer ln.Close() | ||
| go func() { | ||
| for { | ||
| conn, err := ln.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| go func(c net.Conn) { | ||
| rd := bufio.NewReader(c) | ||
| for { | ||
| line, err := rd.ReadString('\n') | ||
| if err != nil { | ||
| c.Close() | ||
| return | ||
| } | ||
| if line[0] == '*' || line[0] == '$' { | ||
| continue | ||
| } | ||
| switch { | ||
| case strings.HasPrefix(strings.ToLower(line), "hello"): | ||
| c.Write([]byte("-ERR unknown command 'hello'\r\n")) | ||
| case strings.HasPrefix(strings.ToLower(line), "monitor"): | ||
| c.Write([]byte("+OK\r\n")) | ||
| select {} // keep the connection open, send nothing | ||
| default: | ||
| c.Write([]byte("+OK\r\n")) | ||
| } | ||
| } | ||
| }(conn) | ||
| } | ||
| }() | ||
|
|
||
| client := redis.NewClient(&redis.Options{ | ||
| Addr: ln.Addr().String(), | ||
| }) | ||
| defer client.Close() | ||
|
|
||
| ch := make(chan string, 100) | ||
| cmd := client.Monitor(context.Background(), ch) | ||
| cmd.Start() | ||
| time.Sleep(100 * time.Millisecond) // let the reader block in Peek | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| cmd.Stop() | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("Stop() blocked; reader goroutine was not unblocked") | ||
| } | ||
| if cmd.Err() != nil { | ||
| t.Fatalf("clean Stop must not set an error, got: %v", cmd.Err()) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.