Skip to content

Commit d92ac33

Browse files
simonfaltumIsaac
andcommitted
Guard the skills hints memo with a mutex
HintsEnabled may now be called from any goroutine that holds the run context, so the memo takes a lock instead of relying on the command goroutine. The lock is held on the caller's context, so the first call's logger is used. Shorten the root comment to point at WithHintsMemo. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent f6345f3 commit d92ac33

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

‎cmd/root/root.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ Stack Trace:
208208
ctx = context.WithValue(ctx, caughtSignalKey{}, &caught)
209209
var deferredHint func()
210210
ctx = context.WithValue(ctx, deferredHintKey{}, &deferredHint)
211-
// Skill hints resolve once for this run, so a deferred hint, its text and
212-
// its throttle agree even when auto-install refreshes the manifest cache
213-
// before the hint prints.
211+
// One skills hints value per run; see skills.WithHintsMemo.
214212
ctx = skills.WithHintsMemo(ctx)
215213
// slog.SetDefault also redirects the standard log package, so both are restored.
216214
prevLogger := slog.Default()

‎libs/skills/guard.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package skills
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/databricks/cli/internal/build"
78
"github.com/databricks/cli/libs/clicompat"
@@ -24,6 +25,7 @@ type hintsMemoKey struct{}
2425

2526
// hintsMemo holds the hints value resolved for one command run.
2627
type hintsMemo struct {
28+
mu sync.Mutex
2729
resolved bool
2830
on bool
2931
}
@@ -40,7 +42,7 @@ func Enabled(ctx context.Context) bool {
4042
// it, gives the answer of the first call. A cache refresh later in the run,
4143
// such as the auto-install before the deferred hint prints, cannot then change
4244
// that hint's text, its throttle or the skills command exemption. The memo is
43-
// not locked, so HintsEnabled must run on the command goroutine.
45+
// safe for concurrent use.
4446
func WithHintsMemo(ctx context.Context) context.Context {
4547
return context.WithValue(ctx, hintsMemoKey{}, &hintsMemo{})
4648
}
@@ -58,6 +60,8 @@ func HintsEnabled(ctx context.Context) bool {
5860
if !ok {
5961
return resolveHints(ctx)
6062
}
63+
memo.mu.Lock()
64+
defer memo.mu.Unlock()
6165
if !memo.resolved {
6266
memo.on = resolveHints(ctx)
6367
memo.resolved = true

0 commit comments

Comments
 (0)