Report workspace lock acquisition before waiting

This commit is contained in:
2026-12-02 00:38:15 +00:00
parent 4d7fc57a5e
commit 89c8e5de3c
8 changed files with 108 additions and 11 deletions

View File

@@ -87,7 +87,7 @@ func cleanSelectedSession(ctx context.Context, input *cleanCommandInput, deps co
}
if !input.DryRun {
store := deps.newWorkspaceStore(pipeline.Workspace.Root)
lock, lockErr := store.AcquireWorkspaceLockContext(ctx)
lock, lockErr := acquireWorkspaceLock(ctx, store, pipeline.Workspace.Root, deps.reporter)
if lockErr != nil {
return report, fmt.Errorf("clean: acquire workspace lock: %w", lockErr)
}

View File

@@ -6,6 +6,8 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
@@ -16,6 +18,71 @@ import (
"go.maximumdirect.net/commandkit"
)
type lockDiagnosticWriter struct {
mu sync.Mutex
data bytes.Buffer
path string
seen chan struct{}
once sync.Once
}
func (w *lockDiagnosticWriter) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
n, err := w.data.Write(p)
if strings.Contains(w.data.String(), w.path) {
w.once.Do(func() { close(w.seen) })
}
return n, err
}
func TestCleanReportsWorkspaceLockBeforeWaiting(t *testing.T) {
root := t.TempDir()
pipeline, _, _ := writeValidConfigFiles(t, root)
store := artifacts.NewLocalStore(root)
lock, err := store.AcquireWorkspaceLock()
if err != nil {
t.Fatal(err)
}
defer store.ReleaseWorkspaceLock(lock)
attempted := make(chan struct{})
app, err := newApplication(commandDependencies{newWorkspaceStore: func(root string) artifacts.Store {
return &observedWorkspaceStore{Store: artifacts.NewLocalStore(root), entering: attempted}
}}, executePlan)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
diagnostic := &lockDiagnosticWriter{path: artifacts.WorkspaceLockPath(root), seen: make(chan struct{})}
done := make(chan commandkit.Outcome, 1)
go func() {
done <- app.Execute(ctx, []string{"session", "clean", "--all", "--config", pipeline, "--progress"}, commandkit.Streams{Out: &bytes.Buffer{}, Err: diagnostic})
}()
select {
case <-diagnostic.seen:
case <-time.After(5 * time.Second):
t.Fatal("workspace lock path was not reported before acquisition completed")
}
select {
case <-attempted:
case <-time.After(5 * time.Second):
t.Fatal("command did not contend for the held lock")
}
cancel()
select {
case outcome := <-done:
if outcome.Status == 0 {
t.Fatal("cancelled cleanup succeeded")
}
case <-time.After(5 * time.Second):
t.Fatal("waiting command did not cancel")
}
if _, err := os.Stat(filepath.Join(root, "work")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("waiting command mutated managed work: %v", err)
}
}
type observedWorkspaceStore struct {
artifacts.Store
entering chan struct{}

View File

@@ -96,7 +96,7 @@ func restoreSessionState(ctx context.Context, cfg *config.Config, opts RestorePl
}
artifactStore := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
lock, err := artifactStore.AcquireWorkspaceLockContext(ctx)
lock, err := acquireWorkspaceLock(ctx, artifactStore, cfg.Pipeline.Workspace.Root, deps.reporter)
if err != nil {
return diagnostic, fmt.Errorf("restore: acquire workspace lock: %w", err)
}

View File

@@ -64,7 +64,7 @@ func withLockedInvocation(ctx context.Context, cfg *config.Config, plan BoundedP
if _, _, err := loadManifestAtPathIfPresent(ctx, &manifest.LocalStore{}, runtime.manifestPath); err != nil {
return nil, err
}
lock, err := runtime.artifactStore.AcquireWorkspaceLockContext(ctx)
lock, err := acquireWorkspaceLock(ctx, runtime.artifactStore, cfg.Pipeline.Workspace.Root, runtime.env.Reporter)
if err != nil {
return nil, fmt.Errorf("acquire workspace lock: %w", err)
}

View File

@@ -179,7 +179,7 @@ func initializeSession(ctx context.Context, request *sessionInitializationInput,
}
if managed {
store := deps.newWorkspaceStore(base.Pipeline.Workspace.Root)
lock, lockErr := store.AcquireWorkspaceLockContext(ctx)
lock, lockErr := acquireWorkspaceLock(ctx, store, base.Pipeline.Workspace.Root, deps.reporter)
if lockErr != nil {
return report, fmt.Errorf("session init: acquire workspace lock: %w", lockErr)
}

View File

@@ -0,0 +1,17 @@
package app
import (
"context"
"fmt"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/logging"
)
// acquireWorkspaceLock identifies acquisition before a potentially blocking wait.
func acquireWorkspaceLock(ctx context.Context, store artifacts.Store, root string, reporter logging.Reporter) (*artifacts.LockHandle, error) {
if reporter != nil {
reporter.Progress(fmt.Sprintf("acquiring workspace lock %q", artifacts.WorkspaceLockPath(root)))
}
return store.AcquireWorkspaceLockContext(ctx)
}