Skip to content

refactor(client): share workspace lock helper - #1539

Merged
skevetter merged 1 commit into
mainfrom
refactor/client-share-lock-helper
Oct 18, 2026
Merged

skevetter merged 1 commit into
mainfrom
refactor/client-share-lock-helper

Conversation

@skevetter

@skevetter skevetter commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Moves the duplicated lazy workspace/machine lock logic from proxy_client.go and workspace_client.go into clientLocks in new locks.go.
  • Both clients now delegate Lock/Unlock to the helper. IDs are read lazily on first lock.
  • Adds locks_test.go covering round trip, contention, cached init failure, machine lock, and machine-failure cleanup.

Behaviour changes:

  • Proxy init failure no longer panics. Lock returns init lock: ..., and Unlock logs and returns.
  • The machine lock is created only when the machine ID is non-empty. The old code keyed it on providerMachine != nil.
  • When machine lock acquisition fails, the workspace lock is now released. The old code leaked it.

Why

Both clients carried near-identical lock init code, so fixes had to be made twice. A single helper keeps lock file naming and ordering in one place.

Validation

  • mise run cli:format
  • mise run cli:lint:ci
  • golangci-lint run --new-from-rev=origin/main ./pkg/...
  • go test ./pkg/client/... -count=1
  • GOOS=windows go build ./pkg/client/...
  • GOOS=linux go vet ./pkg/client/...
  • go build ./pkg/...

CodeRabbit CLI

  • Pass 1: 1 minor finding, "Release the workspace lock when machine lock acquisition fails." Fixed; regression test added.
  • Pass 2 (committed, amended commit): 0 findings across the 4 files.

Auto-merge

Eligible: yes. 4 files, 469 changed lines (335+, 134-).

Summary by CodeRabbit

  • Bug Fixes
    • Workspace locking now honors cancellation while waiting and reports lock acquisition timeouts.
    • If machine-lock acquisition fails, the workspace lock is released to avoid leaving it held.
    • Lock initialization errors are handled safely and reported during locking and unlocking.

@skevetter skevetter added the auto-refactor Automated refactor PR label Oct 18, 2026
@netlify

netlify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit 07d0a31
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6ad44af9f5ff26b2209a7215

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bb3bbaef-e904-4393-8fba-738b40b529b6
📥 Commits

Reviewing files that changed from the base of the PR and between 58162b7 and 07d0a31.

📒 Files selected for processing: 4
  • pkg/client/clientimplementation/locks.go
  • pkg/client/clientimplementation/locks_test.go
  • pkg/client/clientimplementation/proxy_client.go
  • pkg/client/clientimplementation/workspace_client.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Walkthrough

A shared lock manager now handles lazy lock initialization, workspace and optional machine lock acquisition, retries, cancellation, and unlock errors. Proxy and workspace clients delegate their lock operations to this manager.

Changes: Client locking

Layer / File(s) Summary
Centralized lock acquisition and release
pkg/client/clientimplementation/locks.go, pkg/client/clientimplementation/locks_test.go
The new clientLocks manager initializes lock files once, retries acquisition, and handles cancellation and timeout. It acquires the workspace lock before an optional machine lock, releases the workspace lock if machine-lock acquisition fails, and unlocks in reverse order. Tests cover acquisition, release, cancellation, initialization failure, and machine-lock behavior.
Client delegation
pkg/client/clientimplementation/proxy_client.go, pkg/client/clientimplementation/workspace_client.go
Both clients initialize clientLocks with their lock identifiers and delegate lock and unlock operations to it. The proxy client provides an empty machine identifier; the workspace client provides the optional machine identifier.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant WorkspaceClient
  participant clientLocks
  participant WorkspaceLock
  participant MachineLock
  WorkspaceClient->>clientLocks: Lock(ctx)
  clientLocks->>WorkspaceLock: Acquire with retry and context
  WorkspaceLock-->>clientLocks: Acquisition result
  clientLocks->>MachineLock: Acquire when machine ID is set
  MachineLock-->>clientLocks: Acquisition result
  clientLocks-->>WorkspaceClient: Return result
Loading
Merge Risk: ⚪ Minimal · up to 07d0a

No actionable locking regression was established. The change is mergeable after normal checks.

Security architecture risk: 🔵 Low · up to 07d0a

Normal lock identities and ordering are preserved, and acquisition-failure cleanup improves. A narrow compatibility risk remains: an accepted machine configuration with an empty machine ID now skips machine-wide locking without necessarily preventing machine operations. No attacker-controlled path or privilege escalation was established.

Retained concerns

  • Low · reliability · inferred: Machine presence and machine-lock participation can now diverge. A nonnil machine with an empty loaded ID previously participated in the shared .machine.lock; it now receives no machine lock. The constructor validates the workspace’s machine reference rather than the loaded machine’s ID, and machine lifecycle methods still accept that object. This can remove cross-workspace failure containment for inconsistent configuration. Normal creation supplies an ID; attacker influence and harmful concurrent outcomes were not established.
Security review details

Security Blast Radius

  • inferred — The demonstrated coordination scope is cooperating client operations sharing a context and workspace or machine lock identity. Machine locks provide a broader coordination domain than individual workspace locks. The evidence does not establish an expanded tenant, credential, network, or deployment authority boundary.

Security Findings and Attack Paths

  • inferred — The supported risk path requires an inconsistent machine object, such as persisted machine JSON with an empty ID, reaching workspace-client construction. Loading does not validate that field, locking then skips machine coordination, and machine actions remain callable. This establishes a conditional synchronization gap, not a verified attacker-controlled exploit.

Trust Boundaries and Controls

  • observed — Ordinary managed-machine provisioning assigns a machine reference, and machine creation writes that ID into the machine object. This limits the empty-ID concern to inconsistent inputs rather than the demonstrated normal creation path. It does not guarantee the identity of subsequently loaded configuration.

Resilience and Maintainability Implications

  • observed — Initialization fails closed before acquisition, and partial machine-acquisition failure attempts workspace cleanup. These changes improve operational failure containment; cleanup errors remain logged rather than repaired automatically.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: consolidating workspace lock handling into a shared helper.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
✨ Simplify code
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@netlify

netlify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit 07d0a31
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6ad44af914442e35bb7c7991

@skevetter
skevetter marked this pull request as ready for review October 18, 2026 05:16
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@skevetter
skevetter merged commit adbf08d into main Oct 18, 2026
111 checks passed
@skevetter
skevetter deleted the refactor/client-share-lock-helper branch October 18, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-refactor Automated refactor PR size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant