Skip to content

Commit 54eb1df

Browse files
craig[bot]yuzefovich
andcommitted
Merge #150115
150115: sql: harden stmt bundle building code a bit r=yuzefovich a=yuzefovich We just saw a sentry report where on explicit stmt bundle collection via EXPLAIN ANALYZE (DEBUG) we hit a nil pointer because `flowInfo.diagram` was nil. I've audited the code and don't know how this could've happened, so this commit hardens the code a bit by adding nil checks (and crashing in the test-only builds). Fixes: #149987. Release note: None Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents a685bfb + 8d7ea77 commit 54eb1df

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

pkg/sql/explain_bundle.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ func (b *stmtBundleBuilder) addDistSQLDiagrams() {
434434
}
435435

436436
for i, d := range b.plan.distSQLFlowInfos {
437+
if d.diagram == nil {
438+
if buildutil.CrdbTestBuild {
439+
panic(errors.AssertionFailedf("diagram shouldn't be nil when building the bundle"))
440+
}
441+
continue
442+
}
437443
d.diagram.AddSpans(b.trace)
438444
_, url, err := d.diagram.ToURL()
439445

pkg/sql/instrumentation.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,19 @@ func (ih *instrumentationHelper) setExplainAnalyzeResult(
942942
} else {
943943
buf.WriteString("Diagram: ")
944944
}
945-
d.diagram.AddSpans(trace)
946-
_, url, err := d.diagram.ToURL()
947-
if err != nil {
948-
buf.WriteString(err.Error())
945+
if d.diagram != nil {
946+
d.diagram.AddSpans(trace)
947+
_, url, err := d.diagram.ToURL()
948+
if err != nil {
949+
buf.WriteString(err.Error())
950+
} else {
951+
buf.WriteString(url.String())
952+
}
949953
} else {
950-
buf.WriteString(url.String())
954+
if buildutil.CrdbTestBuild {
955+
panic(errors.AssertionFailedf("diagram shouldn't be nil in EXPLAIN ANALYZE (DISTSQL)"))
956+
}
957+
buf.WriteString("<missing>")
951958
}
952959
rows = append(rows, buf.String())
953960
}

pkg/sql/plan.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,10 @@ type planNodeSpooled interface {
320320
var _ planNodeSpooled = &spoolNode{}
321321

322322
type flowInfo struct {
323-
typ planComponentType
323+
typ planComponentType
324+
// diagram is only populated when instrumentationHelper.shouldSaveDiagrams()
325+
// returns true. (Even in that case we've seen a sentry report #149987 where
326+
// it was nil.)
324327
diagram execinfrapb.FlowDiagram
325328
// explainVec and explainVecVerbose are only populated when collecting a
326329
// statement bundle when the plan was vectorized.

0 commit comments

Comments
 (0)