Remove timeout logic from ReadRaw functions and add ReadRawWithContext (#18708)

Removing the timeout logic from raw-response functions and adding documentation comments. The following functions are affected:

- `ReadRaw`
- `ReadRawWithContext` (newly added)
- `ReadRawWithData`
- `ReadRawWithDataWithContext`

The previous logic of using `ctx, _ = c.c.withConfiguredTimeout(ctx)` could cause a potential [context leak](https://pkg.go.dev/context):

> Failing to call the CancelFunc leaks the child and its children until the parent is canceled or the timer fires. The go vet tool checks that CancelFuncs are used on all control-flow paths.

Cancelling the context would have caused more issues since the context would be cancelled before the request body is closed.

Resolves: #18658
This commit is contained in:
Anton Averchenkov
2023-01-17 15:41:59 -05:00
committed by GitHub
parent d205193daf
commit ef3e3eace2
5 changed files with 53 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
package command
import (
"context"
"fmt"
"io"
"os"
@@ -77,6 +78,10 @@ func (c *ReadCommand) Run(args []string) int {
return 2
}
// client.ReadRaw* methods require a manual timeout override
ctx, cancel := context.WithTimeout(context.Background(), client.ClientTimeout())
defer cancel()
// Pull our fake stdin if needed
stdin := (io.Reader)(os.Stdin)
if c.testStdin != nil {
@@ -92,7 +97,7 @@ func (c *ReadCommand) Run(args []string) int {
}
if Format(c.UI) != "raw" {
secret, err := client.Logical().ReadWithData(path, data)
secret, err := client.Logical().ReadWithDataWithContext(ctx, path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err))
return 2
@@ -109,7 +114,7 @@ func (c *ReadCommand) Run(args []string) int {
return OutputSecret(c.UI, secret)
}
resp, err := client.Logical().ReadRawWithData(path, data)
resp, err := client.Logical().ReadRawWithDataWithContext(ctx, path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading: %s: %s", path, err))
return 2