Repository navigation
Conversation
Invoke parsed only duration and called time.Sleep unconditionally, so the required timeout cap was never applied and cancelling the MCP request did not stop the sleep. Reject a duration longer than the configured timeout, and select on ctx.Done() so cancellation aborts the sleep. Fixes googleapis#4581
There was a problem hiding this comment.
Code Review
This pull request updates the wait tool's Invoke method to enforce a configured timeout limit and support early cancellation by replacing time.Sleep with a timer and select block. It also adds corresponding unit tests. The feedback suggests optimizing performance and error detection by parsing and validating the configured timeout during the tool's initialization phase rather than on every invocation. Additionally, it recommends validating that the requested duration is strictly positive to prevent logical errors.
| timeout, err := time.ParseDuration(t.Cfg.Timeout) | ||
| if err != nil { | ||
| return nil, util.NewAgentError("invalid timeout configuration", err) | ||
| } |
There was a problem hiding this comment.
Parsing and validating the tool's configured timeout (t.Cfg.Timeout) on every invocation is inefficient and delays configuration error detection to runtime. Instead, this validation should be performed during the tool's initialization phase in Initialize (where an invalid timeout string can be rejected immediately at startup), and the parsed time.Duration can be stored in the Tool struct.
| if totalDuration > timeout { | ||
| return nil, util.NewAgentError( | ||
| fmt.Sprintf("duration %v exceeds the configured timeout %v", totalDuration, timeout), nil) | ||
| } |
There was a problem hiding this comment.
The duration parameter is parsed as a time.Duration, which can be negative or zero (e.g., "-5s" or "0s"). Waiting for a non-positive duration is a logical error and should be rejected with an error. Adding a validation check ensures that only positive durations are accepted.
| if totalDuration > timeout { | |
| return nil, util.NewAgentError( | |
| fmt.Sprintf("duration %v exceeds the configured timeout %v", totalDuration, timeout), nil) | |
| } | |
| if totalDuration <= 0 { | |
| return nil, util.NewAgentError("duration must be greater than zero", nil) | |
| } | |
| if totalDuration > timeout { | |
| return nil, util.NewAgentError( | |
| fmt.Sprintf("duration %v exceeds the configured timeout %v", totalDuration, timeout), nil) | |
| } |
…tions Address review feedback: parse cfg.Timeout once in Initialize (storing the time.Duration) instead of on every Invoke, and reject a non-positive duration parameter. Fixes googleapis#4581
Description
The
waittool parsed onlydurationand calledtime.Sleepunconditionally,so the required
timeoutcap was never applied and cancelling the MCP requestdid not stop the sleep.
Invokenow rejects adurationlonger than the configuredtimeout, andselects on
ctx.Done()so cancellation aborts the sleep.PR Checklist
TestWaitInvokeRespectsTimeout)🛠️ Fixes #4581