Files
skillshare/internal/install/checkout_lock.go
T
Willie 23c9453f87 fix(update): serialize checkout updates across skillshare processes (#666)
Two dashboards (or a dashboard and the CLI) serving the same source ran
"Update all" at once. The server mutex only covers one process, so both
pulled into the same checkouts: one saw the other's half-applied merge as
"local changes would be overwritten", and concurrent fetches into a
blob:none clone failed with "fetch-pack: invalid index-pack output".
update.TrackedRepo, install --track --update, and the in-place pull of a
git-installed skill now hold a flock in the checkout's git directory; a
second process waits up to a minute, then fails with a clear message.

Force on a git-installed skill only skipped the local-modification check
and then ran a plain pull, which refused to merge over the edits the docs
say --force discards. It now fetches and resets to upstream, so a failed
fetch still keeps the edits. Followed checkouts keep pulling, since they
must be clean and may hold the user's unpushed commits.

The pull error dropped the file list git indents under it, and the
finished-update subtitle said everything was up to date even when items
had failed.
2026-10-29 16:20:30 +08:00

42 lines
1.5 KiB
Go

package install
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gofrs/flock"
)
// checkoutLockWait bounds how long an update waits for another process to
// finish updating the same checkout. The dashboard waits while holding its
// server lock, so keep it short.
var checkoutLockWait = time.Minute
// LockCheckout holds an exclusive lock for updating the git checkout at
// repoPath until the returned unlock runs. A dashboard's own mutex cannot see a
// second dashboard or the CLI, and two git processes pulling into one checkout
// fail each other with spurious "local changes" and index-pack errors.
func LockCheckout(repoPath string) (func(), error) {
gitDir := filepath.Join(repoPath, ".git")
if info, err := os.Stat(gitDir); err != nil || !info.IsDir() {
// A .git file points at the real directory, as in a linked worktree.
if gitDir, err = gitOutput(repoPath, "rev-parse", "--absolute-git-dir"); err != nil {
return nil, fmt.Errorf("failed to locate git directory of %s: %w", repoPath, err)
}
}
lock := flock.New(filepath.Join(gitDir, "skillshare-update.lock"))
ctx, cancel := context.WithTimeout(context.Background(), checkoutLockWait)
defer cancel()
ok, err := lock.TryLockContext(ctx, 200*time.Millisecond)
if err != nil && ctx.Err() == nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("another skillshare process is still updating %s; retry when it finishes", repoPath)
}
return func() { _ = lock.Unlock() }, nil
}