Skip to content

Commit 911df0f

Browse files
pieternclaude
andauthored
Remove context.Background() usage in libs/filer (#4657)
## Summary - Replace `context.Background()` with proper context propagation in the workspace files readahead cache - Add `ctx` field to cache struct, passed at construction time instead of using `context.Background()` in worker goroutines and enqueue calls - Update `NewReadOnlyWorkspaceFilesExtensionsClient` to accept a `context.Context` parameter - Simplify the two public constructors into direct implementations instead of delegating to a shared private function ## Test plan - [x] Unit tests pass for `libs/filer` and `bundle/config/mutator` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 344db5f commit 911df0f

4 files changed

Lines changed: 29 additions & 23 deletions

File tree

bundle/config/mutator/configure_wsfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *configureWSFS) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagno
5151
// If so, swap out vfs.Path instance of the sync root with one that
5252
// makes all Workspace File System interactions extension aware.
5353
p, err := vfs.NewFilerPath(ctx, root, func(path string) (filer.Filer, error) {
54-
return filer.NewReadOnlyWorkspaceFilesExtensionsClient(b.WorkspaceClient(), path)
54+
return filer.NewReadOnlyWorkspaceFilesExtensionsClient(ctx, b.WorkspaceClient(), path)
5555
})
5656
if err != nil {
5757
return diag.FromErr(err)

libs/filer/workspace_files_cache.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ type executable interface {
182182
// cache stores all entries for cacheable Workspace File System calls.
183183
// We care about caching only [ReadDir] and [Stat] calls.
184184
type cache struct {
185-
f Filer
186-
m sync.Mutex
185+
f Filer
186+
ctx context.Context
187+
m sync.Mutex
187188

188189
readDir map[string]*readDirEntry
189190
stat map[string]*statEntry
@@ -195,17 +196,17 @@ type cache struct {
195196
wg sync.WaitGroup
196197
}
197198

198-
func newWorkspaceFilesReadaheadCache(f Filer) *cache {
199+
func newWorkspaceFilesReadaheadCache(ctx context.Context, f Filer) *cache {
199200
c := &cache{
200-
f: f,
201+
f: f,
202+
ctx: ctx,
201203

202204
readDir: make(map[string]*readDirEntry),
203205
stat: make(map[string]*statEntry),
204206

205207
queue: make(chan executable, kMaxQueueSize),
206208
}
207209

208-
ctx := context.Background()
209210
for range kNumCacheWorkers {
210211
c.wg.Add(1)
211212
go c.work(ctx)
@@ -251,7 +252,7 @@ func (c *cache) completeReadDirForDir(name string, dirEntry fs.DirEntry) {
251252
if _, ok := c.readDir[name]; !ok {
252253
// Create a new cache entry and queue the operation.
253254
e := newReadDirEntry(name)
254-
err := c.enqueue(context.Background(), e)
255+
err := c.enqueue(c.ctx, e)
255256
if err != nil {
256257
e.markError(err)
257258
}
@@ -278,7 +279,7 @@ func (c *cache) completeReadDirForFile(name string, dirEntry fs.DirEntry) {
278279
// This is the only (?) case where this implementation is tied to the workspace filer.
279280

280281
// Queue a [Stat] call for the file.
281-
err := c.enqueue(context.Background(), e)
282+
err := c.enqueue(c.ctx, e)
282283
if err != nil {
283284
e.markError(err)
284285
}

libs/filer/workspace_files_cache_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestWorkspaceFilesCache_ReadDirCache(t *testing.T) {
8181
}
8282

8383
ctx := t.Context()
84-
cache := newWorkspaceFilesReadaheadCache(f)
84+
cache := newWorkspaceFilesReadaheadCache(ctx, f)
8585
defer cache.Cleanup()
8686

8787
// First read dir should hit the filer, second should hit the cache.
@@ -122,7 +122,7 @@ func TestWorkspaceFilesCache_ReadDirCacheIsolation(t *testing.T) {
122122
}
123123

124124
ctx := t.Context()
125-
cache := newWorkspaceFilesReadaheadCache(f)
125+
cache := newWorkspaceFilesReadaheadCache(ctx, f)
126126
defer cache.Cleanup()
127127

128128
// First read dir should hit the filer, second should hit the cache.
@@ -153,7 +153,7 @@ func TestWorkspaceFilesCache_StatCache(t *testing.T) {
153153
}
154154

155155
ctx := t.Context()
156-
cache := newWorkspaceFilesReadaheadCache(f)
156+
cache := newWorkspaceFilesReadaheadCache(ctx, f)
157157
defer cache.Cleanup()
158158

159159
// First stat should hit the filer, second should hit the cache.
@@ -222,7 +222,7 @@ func TestWorkspaceFilesCache_ReadDirPopulatesStatCache(t *testing.T) {
222222
}
223223

224224
ctx := t.Context()
225-
cache := newWorkspaceFilesReadaheadCache(f)
225+
cache := newWorkspaceFilesReadaheadCache(ctx, f)
226226
defer cache.Cleanup()
227227

228228
// Issue read dir to populate the stat cache.
@@ -289,7 +289,7 @@ func TestWorkspaceFilesCache_ReadDirTriggersReadahead(t *testing.T) {
289289
}
290290

291291
ctx := t.Context()
292-
cache := newWorkspaceFilesReadaheadCache(f)
292+
cache := newWorkspaceFilesReadaheadCache(ctx, f)
293293
defer cache.Cleanup()
294294

295295
// Issue read dir to populate the stat cache.

libs/filer/workspace_files_extensions_client.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,35 @@ func (e ReadOnlyError) Error() string {
166166
// errors for namespace clashes (e.g. a file and a notebook or a directory and a notebook).
167167
// Thus users of these methods should be careful to avoid such clashes.
168168
func NewWorkspaceFilesExtensionsClient(w *databricks.WorkspaceClient, root string) (Filer, error) {
169-
return newWorkspaceFilesExtensionsClient(w, root, false)
170-
}
169+
filer, err := NewWorkspaceFilesClient(w, root)
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
return &WorkspaceFilesExtensionsClient{
175+
workspaceClient: w,
171176

172-
func NewReadOnlyWorkspaceFilesExtensionsClient(w *databricks.WorkspaceClient, root string) (Filer, error) {
173-
return newWorkspaceFilesExtensionsClient(w, root, true)
177+
wsfs: filer,
178+
root: root,
179+
readonly: false,
180+
}, nil
174181
}
175182

176-
func newWorkspaceFilesExtensionsClient(w *databricks.WorkspaceClient, root string, readonly bool) (Filer, error) {
183+
func NewReadOnlyWorkspaceFilesExtensionsClient(ctx context.Context, w *databricks.WorkspaceClient, root string) (Filer, error) {
177184
filer, err := NewWorkspaceFilesClient(w, root)
178185
if err != nil {
179186
return nil, err
180187
}
181188

182-
if readonly {
183-
// Wrap in a readahead cache to avoid making unnecessary calls to the workspace.
184-
filer = newWorkspaceFilesReadaheadCache(filer)
185-
}
189+
// Wrap in a readahead cache to avoid making unnecessary calls to the workspace.
190+
filer = newWorkspaceFilesReadaheadCache(ctx, filer)
186191

187192
return &WorkspaceFilesExtensionsClient{
188193
workspaceClient: w,
189194

190195
wsfs: filer,
191196
root: root,
192-
readonly: readonly,
197+
readonly: true,
193198
}, nil
194199
}
195200

0 commit comments

Comments
 (0)