Skip to content

Commit

Permalink
Merge pull request #100309 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-23.1-99835

release-23.1: opt/execbuilder: add panic catching to buildRoutinePlanGenerator
  • Loading branch information
mgartner authored Mar 31, 2023
2 parents 5f85986 + 29332fd commit ddd3539
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/sql/opt/exec/execbuilder/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp"
"github.com/cockroachdb/cockroach/pkg/sql/sem/volatility"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
Expand Down Expand Up @@ -946,7 +948,29 @@ func (b *Builder) buildRoutinePlanGenerator(
var o xform.Optimizer
planGen := func(
ctx context.Context, ref tree.RoutineExecFactory, args tree.Datums, fn tree.RoutinePlanGeneratedFunc,
) error {
) (err error) {
defer func() {
if r := recover(); r != nil {
// This code allows us to propagate internal errors without
// having to add error checks everywhere throughout the code.
// This is only possible because the code does not update shared
// state and does not manipulate locks.
//
// This is the same panic-catching logic that exists in
// o.Optimize() below. It's required here because it's possible
// for factory functions to panic below, like
// CopyAndReplaceDefault.
if ok, e := errorutil.ShouldCatch(r); ok {
err = e
log.VEventf(ctx, 1, "%v", err)
} else {
// Other panic objects can't be considered "safe" and thus
// are propagated as crashes that terminate the session.
panic(r)
}
}
}()

for i := range stmts {
stmt := stmts[i]
o.Init(ctx, b.evalCtx, b.catalog)
Expand Down

0 comments on commit ddd3539

Please sign in to comment.