Skip to content

[claude] Fix crash on /sync-harmony when request scope already has project context - #2516

Open
myieye wants to merge 1 commit into
developfrom
claude/crdt-project-context-error-1aac28
Open

[claude] Fix crash on /sync-harmony when request scope already has project context#2516
myieye wants to merge 1 commit into
developfrom
claude/crdt-project-context-error-1aac28

Conversation

@myieye

@myieye myieye commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

[Claude, autonomous]

POST /api/merge/sync-harmony throws InvalidOperationException: Can't setup project context for crdt when already in context of project crdt.

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: Can't setup project context for crdt when already in context of project crdt
         at LcmCrdt.CurrentProjectService.SetupProjectContext(CrdtProject project) in /FwLite/LcmCrdt/CurrentProjectService.cs:line 78
         at LcmCrdt.LcmCrdtKernel.OpenCrdtProject(IServiceProvider services, CrdtProject project) in /FwLite/LcmCrdt/LcmCrdtKernel.cs:line 425
         at FwHeadless.Services.SyncWorker.ExecuteSync(CancellationToken stoppingToken, Boolean onlyHarmony) in /FwHeadless/Services/SyncHostedService.cs:line 196
         at FwHeadless.Services.SyncWorker.ExecuteSync(CancellationToken stoppingToken, Boolean onlyHarmony) in /FwHeadless/Services/SyncHostedService.cs:line 276
         at FwHeadless.Routes.MergeRoutes.SyncHarmonyProject(Guid projectId, IProjectLookupService projectLookupService, CrdtSyncService crdtSyncService, IServiceProvider services, CancellationToken stoppingToken) in /FwHeadless/Routes/MergeRoutes.cs:line 98
         at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteTaskResult[T](Task`1 task, HttpContext httpContext)
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger)
         at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.<Invoke>g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

The ProjectContextFromIdService middleware runs first on every request carrying projectId and (when the crdt file exists) calls SetupProjectContext on the request-scoped CurrentProjectService. The handler then ran SyncWorker in that same request scope, so the worker's OpenCrdtProject tried to set up a second, different CrdtProject instance and tripped the reference-equality guard. Both instances carry the hardcoded name "crdt", hence the self-referential-looking message.

The queued path (/executeSyncHostedService.ExecuteAsync) never hits this because it runs the worker in a fresh scope. Fix: /sync-harmony now does the same.

Only reproduces on a previously-synced project (the middleware's File.Exists gate) — i.e. exactly the "restore a reset project" case this endpoint serves.

Test: SyncWorkerTests.SyncHarmonyRoute_SucceedsWhenRequestScopeAlreadyHasProjectContext invokes the real handler on a pre-populated request scope; it fails with the exact production exception on the old code and passes with the fix.

The /api/merge/sync-harmony handler ran the SyncWorker in the HTTP request
scope, which the ProjectContextFromIdService middleware had already called
SetupProjectContext on. The worker's own OpenCrdtProject then tried to set up
a second (different) CrdtProject instance in the same scope and hit the
reference-equality guard in CurrentProjectService.SetupProjectContext:
"Can't setup project context for crdt when already in context of project crdt".

Run the worker in its own DI scope, matching the queued sync path
(SyncHostedService.ExecuteAsync), so the middleware's request-scoped context
no longer collides.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions github-actions Bot added the 📦 Lexbox issues related to any server side code, fw-headless included label Jul 30, 2026
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@myieye, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: eb1c2d4a-d430-4f96-bcca-71328e5df840

📥 Commits

Reviewing files that changed from the base of the PR and between a425984 and 8b587f4.

📒 Files selected for processing (4)
  • backend/FwHeadless/FwHeadless.csproj
  • backend/FwHeadless/Routes/MergeRoutes.cs
  • backend/Testing/FwHeadless/Services/SyncWorkerTestHarness.cs
  • backend/Testing/FwHeadless/Services/SyncWorkerTests.cs

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📦 Lexbox issues related to any server side code, fw-headless included

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant