This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Improve signal handling for df
timeout
#16
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,72 @@ | ||
package filesystem | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func MockSlowGetFileSystemInfo() (interface{}, error) { | ||
/* Run a command that will definitely time out */ | ||
cmd := exec.Command("sleep", "5") | ||
|
||
outCh := make(chan []byte, 1) | ||
errCh := make(chan error, 1) | ||
|
||
var out interface{} | ||
var err error | ||
|
||
go func() { | ||
_out, _err := cmd.Output() | ||
if _err != nil { | ||
errCh <- fmt.Errorf("df failed to collect filesystem data: %s", _err) | ||
return | ||
} | ||
outCh <- _out | ||
}() | ||
|
||
WAIT: | ||
for { | ||
select { | ||
case res := <-outCh: | ||
if res != nil { | ||
out, err = parseDfOutput(string(res)) | ||
} else { | ||
out, err = nil, fmt.Errorf("df failed to collect filesystem data") | ||
} | ||
break WAIT | ||
case err = <-errCh: | ||
out = nil | ||
break WAIT | ||
case <-time.After(2 * time.Second): | ||
// Kill the process if it takes too long | ||
if killErr := cmd.Process.Kill(); killErr != nil { | ||
log.Fatal("failed to kill:", killErr) | ||
// Force goroutine to exit | ||
<-outCh | ||
} | ||
} | ||
} | ||
|
||
return out, err | ||
} | ||
|
||
func TestSlowGetFileSystemInfo(t *testing.T) { | ||
out, err := MockSlowGetFileSystemInfo() | ||
if !reflect.DeepEqual(out, nil) { | ||
t.Fatalf("Failed! out should be nil. Instead it's %s", out) | ||
} | ||
if !reflect.DeepEqual(err, fmt.Errorf("df failed to collect filesystem data: signal: killed")) { | ||
t.Fatalf("Failed! Wrong error: %s", err) | ||
} | ||
} | ||
|
||
func TestGetFileSystemInfo(t *testing.T) { | ||
_, err := getFileSystemInfo() | ||
if !reflect.DeepEqual(err, nil) { | ||
t.Fatalf("getFileSystemInfo failed: %s", err) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have to kill the whole process anymore ? Can't we just return the error ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it made sense to ensure that multiple
df
calls spawned by gohai weren't running at the same time. The change was prompted by a case wheredf
was taking long and successive gohai calls kept spawning new, time-intensive processes. This will killdf
before it returns, and not interrupt the maingohai
processThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah misread the code. looks good to me!