-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix Monitor command blocking on ReadTimeout #3716
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
base: master
Are you sure you want to change the base?
Changes from all commits
3aebb85
c27d7f8
790dfa8
a022f65
fa3c807
6a48d3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7370,6 +7370,7 @@ type MonitorCmd struct { | |
| baseCmd | ||
| ch chan string | ||
| status MonitorStatus | ||
| closed bool | ||
| mu sync.Mutex | ||
| } | ||
|
|
||
|
|
@@ -7382,6 +7383,7 @@ func newMonitorCmd(ctx context.Context, ch chan string) *MonitorCmd { | |
| }, | ||
| ch: ch, | ||
| status: monitorStatusIdle, | ||
| closed: false, | ||
| mu: sync.Mutex{}, | ||
| } | ||
| } | ||
|
|
@@ -7411,23 +7413,58 @@ func (cmd *MonitorCmd) readReply(rd *proto.Reader) error { | |
|
|
||
| func (cmd *MonitorCmd) readMonitor(rd *proto.Reader, cancel context.CancelFunc) error { | ||
| for { | ||
| // Check if context is done first | ||
| select { | ||
| case <-cmd.ctx.Done(): | ||
| cmd.closeChannel() | ||
| cancel() | ||
| return cmd.ctx.Err() | ||
| default: | ||
| } | ||
|
|
||
| cmd.mu.Lock() | ||
| st := cmd.status | ||
| pk, _ := rd.Peek(1) | ||
| cmd.mu.Unlock() | ||
| if len(pk) != 0 && st == monitorStatusStart { | ||
| cmd.mu.Lock() | ||
| line, err := rd.ReadString() | ||
| cmd.mu.Unlock() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cmd.ch <- line | ||
| } | ||
|
|
||
| if st == monitorStatusStop { | ||
| cmd.closeChannel() | ||
| cancel() | ||
| break | ||
| } | ||
|
|
||
| if st == monitorStatusStart { | ||
| cmd.mu.Lock() | ||
| pk, peekErr := rd.Peek(1) | ||
| cmd.mu.Unlock() | ||
|
|
||
| if peekErr != nil { | ||
| // Check if it's a timeout error - if so, ignore and continue | ||
| if isTimeout, _ := isTimeoutError(peekErr); isTimeout { | ||
| continue | ||
| } | ||
| // For non-timeout errors, close channel and return | ||
| cmd.closeChannel() | ||
| cancel() | ||
| return peekErr | ||
| } | ||
|
|
||
| if len(pk) != 0 { | ||
| cmd.mu.Lock() | ||
| line, err := rd.ReadString() | ||
| cmd.mu.Unlock() | ||
| if err != nil { | ||
| // Check if it's a timeout error - if so, ignore and continue | ||
| if isTimeout, _ := isTimeoutError(err); isTimeout { | ||
| continue | ||
| } | ||
| // For non-timeout errors, close channel and return | ||
| cmd.closeChannel() | ||
| cancel() | ||
| return err | ||
| } | ||
| cmd.ch <- line | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tight CPU-spinning loop when monitor status is idleLow Severity The original code called |
||
| } | ||
| return nil | ||
| } | ||
|
|
@@ -7444,6 +7481,16 @@ func (cmd *MonitorCmd) Stop() { | |
| cmd.status = monitorStatusStop | ||
| } | ||
|
|
||
| // closeChannel safely closes the channel if it hasn't been closed yet. | ||
| func (cmd *MonitorCmd) closeChannel() { | ||
| cmd.mu.Lock() | ||
| defer cmd.mu.Unlock() | ||
| if !cmd.closed { | ||
| close(cmd.ch) | ||
| cmd.closed = true | ||
| } | ||
| } | ||
|
|
||
| type VectorScoreSliceCmd struct { | ||
| baseCmd | ||
|
|
||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.