Skip to content

Commit 5c0a632

Browse files
committed
opt: prevent apply-join panics from crashing nodes
Previously, it was possible for the execution of an apply-join to crash a node due to an uncaught optimizer panic when calling the `planRightSideFn` closure. This closure is invoked for every input row to the apply-join. It replaces variables in the expression on the right side of the join with constants using `Factory.CopyAndReplace`, which can panic. This panic won't be caught by the panic-catching logic in `Optimizer.Optimize` because the closure is invoked outside the context of `Optimizer.Optimize` - it's occurring during execution instead. This commit copies the panic-catching logic of `Optimizer.Optimize` to the apply-join's `planRightSideFn` closure to ensure that any panics are caught. Release Note (bug fix): A bug has been fixed that could cause nodes to crash in rare cases when executing apply-joins in query plans.
1 parent 31754ac commit 5c0a632

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

pkg/sql/opt/exec/execbuilder/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ go_library(
4848
"//pkg/util/encoding",
4949
"//pkg/util/errorutil",
5050
"//pkg/util/errorutil/unimplemented",
51+
"//pkg/util/log",
5152
"//pkg/util/timeutil",
5253
"//pkg/util/treeprinter",
5354
"@com_github_cockroachdb_errors//:errors",

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ import (
4141
"github.com/cockroachdb/cockroach/pkg/util"
4242
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
4343
"github.com/cockroachdb/cockroach/pkg/util/encoding"
44+
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
4445
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
46+
"github.com/cockroachdb/cockroach/pkg/util/log"
4547
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
4648
"github.com/cockroachdb/errors"
4749
"github.com/cockroachdb/redact"
@@ -1024,7 +1026,29 @@ func (b *Builder) buildApplyJoin(join memo.RelExpr) (execPlan, error) {
10241026
//
10251027
// Note: we put o outside of the function so we allocate it only once.
10261028
var o xform.Optimizer
1027-
planRightSideFn := func(ctx context.Context, ef exec.Factory, leftRow tree.Datums) (exec.Plan, error) {
1029+
planRightSideFn := func(ctx context.Context, ef exec.Factory, leftRow tree.Datums) (_ exec.Plan, err error) {
1030+
defer func() {
1031+
if r := recover(); r != nil {
1032+
// This code allows us to propagate internal errors without having to add
1033+
// error checks everywhere throughout the code. This is only possible
1034+
// because the code does not update shared state and does not manipulate
1035+
// locks.
1036+
//
1037+
// This is the same panic-catching logic that exists in
1038+
// o.Optimize() below. It's required here because it's possible
1039+
// for factory functions to panic below, like
1040+
// CopyAndReplaceDefault.
1041+
if ok, e := errorutil.ShouldCatch(r); ok {
1042+
err = e
1043+
log.VEventf(ctx, 1, "%v", err)
1044+
} else {
1045+
// Other panic objects can't be considered "safe" and thus are
1046+
// propagated as crashes that terminate the session.
1047+
panic(r)
1048+
}
1049+
}
1050+
}()
1051+
10281052
o.Init(ctx, b.evalCtx, b.catalog)
10291053
f := o.Factory()
10301054

0 commit comments

Comments
 (0)