Skip to content

Commit

Permalink
Merge pull request cockroachdb#98214 from smg260/backport22.1-81779-9…
Browse files Browse the repository at this point in the history
…2845-93124

release-22.1: roachtest: logger nil dereference guards
  • Loading branch information
smg260 authored Mar 8, 2023
2 parents 9cbb613 + 8a93c31 commit cbdd452
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 14 additions & 4 deletions pkg/cmd/roachtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1960,9 +1960,16 @@ func (c *clusterImpl) RunE(ctx context.Context, node option.NodeListOption, args
if err := ctx.Err(); err != nil {
l.Printf("(note: incoming context was canceled: %s", err)
}
physicalFileName := l.File.Name()
// We need to protect ourselves from a race where cluster logger is
// concurrently closed before child logger is created. In that case child
// logger will have no log file but would write to stderr instead and we can't
// create a meaningful ".failed" file for it.
physicalFileName := ""
if l.File != nil {
physicalFileName = l.File.Name()
}
l.Close()
if err != nil {
if err != nil && len(physicalFileName) > 0 {
failedPhysicalFileName := strings.TrimSuffix(physicalFileName, ".log") + ".failed"
if failedFile, err2 := os.Create(failedPhysicalFileName); err2 != nil {
failedFile.Close()
Expand Down Expand Up @@ -2003,7 +2010,10 @@ func (c *clusterImpl) RunWithDetails(
if err != nil {
return nil, err
}
physicalFileName := l.File.Name()
physicalFileName := ""
if l.File != nil {
physicalFileName = l.File.Name()
}

if err := ctx.Err(); err != nil {
l.Printf("(note: incoming context was canceled: %s", err)
Expand All @@ -2016,7 +2026,7 @@ func (c *clusterImpl) RunWithDetails(
}

results, err := roachprod.RunWithDetails(ctx, l, c.MakeNodes(nodes), "" /* SSHOptions */, "" /* processTag */, false /* secure */, args)
if err != nil {
if err != nil && len(physicalFileName) > 0 {
l.Printf("> result: %+v", err)
createFailedFile(physicalFileName)
return results, err
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/roachtest/test_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,13 @@ func (t *testImpl) addFailure(depth int, format string, args ...interface{}) {
// We don't actually log through this logger since it adds an unrelated
// file:line caller (namely ours). The error already has stack traces
// so it's better to write only it to the file to avoid confusion.
path := cl.File.Name()
if cl.File != nil {
path := cl.File.Name()
if len(path) > 0 {
_ = os.WriteFile(path, []byte(fmt.Sprintf("%+v", reportFailure.squashedErr)), 0644)
}
}
cl.Close() // we just wanted the filename
_ = os.WriteFile(path, []byte(fmt.Sprintf("%+v", reportFailure.squashedErr)), 0644)
}
}

Expand Down

0 comments on commit cbdd452

Please sign in to comment.