Skip to content

fix(app): flaky startup data race between attempt recovery and Build config mutation #2268

Closed

Description

@jbeda

Summary

A pre-existing startup data race intermittently fails CI: attempt recovery invokes a local-placement runner callback while Build is still mutating the Config captured by that callback. This is a real production race exposed by a flaky test result, not an effort-picker regression. Track and fix separately from #2266.

CI evidence

Root cause

At this head, internal/app/build.go:2352-2355 installs runnerForRoot on the local placement provider. Its closure captures the mutable local cfg and passes it by value to buildCommandRunnerForRoot (internal/app/build.go:6680), reading/copying the whole struct.

Build starts attempt recovery at internal/app/build.go:2368. The immediate recovery timer can reattach queued work before startup finishes. Meanwhile Build writes cfg.maintenanceMutationAvailable at internal/app/build.go:2943.

Race-detector read stack:

Build.func12                         build.go:2355
localPlacementProvider.bindLocal     placement.go:284
localPlacementProvider.Reattach      placement.go:160
recoverAttempt.func4                 attempt_recovery.go:159
attemptWorker.Run                    attempt_worker.go:297
recoverAttempt                       attempt_recovery.go:218
startAttemptRecovery.func1           attempt_recovery.go:77
newAttemptRecoveryLoop.func1         attempt_recovery.go:55

Write stack:

Build                               build.go:2943
TestCloudNativeLearning_Scenario3_ExplicitProcedureAttemptSurvivesRestart
                                    cloud_native_learning_scenario3_test.go:102

All race-stack source/test files are unchanged by #2266 relative to c6d7338888b94935b67535a0bd37902dd944ff71. The callback and later assignment both predate the PR.

Local reproduction

An initial 30-repeat focused race run passed. This command subsequently reproduced the identical stacks in about 45 seconds:

GOMAXPROCS=2 GOWORK=off go test -mod=readonly -race ./internal/app \
  -run '^TestCloudNativeLearning_Scenario3_ExplicitProcedureAttemptSurvivesRestart$' \
  -count=200

Minimal proposed fix and verification

Capture an immutable configuration snapshot before publishing the callback, and use that snapshot for runner creation rather than the still-mutating cfg. Preserve the later service-dependent maintenance assignment. No sleeps, recovery suppression, or blind retries.

Retain the restart scenario, repeat the focused race reproduction, and run recovery/placement tests plus the full race gate. Preserve shell-disable, root selection, and environment-scrubbing behavior.

Activity

  1. added
    bug
    flake
    on Oct 11, 2026
  2. jbeda commented on Oct 12, 2026

    @jbeda
    ContributorAuthor

    Investigation summary and proposed handoff

    This is a server-startup configuration ownership race, not construction of a special learning session. app.Build composes the shared service used by ordinary sessions. During composition, it starts durable learning-attempt recovery, which can discover queued work left by a previous process.

    Core issue

    The local placement provider's runnerForRoot closure captures Build's mutable cfg and passes it by value to buildCommandRunnerForRoot. Recovery reaches that callback through exact placement reattachment. Meanwhile, Build assigns cfg.maintenanceMutationAvailable after creating the service. The argument copy reads the whole Config and races with that assignment—even if Shell is disabled, because copying precedes the shell-disable check.

    Current source anchors (investigated at 56c5534a3):

    • internal/app/build.go:2352-2368: callback publication and recovery launch.
    • internal/app/build.go:2943: late maintenance-predicate assignment.
    • internal/app/build.go:6680: by-value runner constructor.
    • internal/app/attempt_recovery.go:31-43: immediate initial discovery.
    • internal/app/attempt_recovery.go:149-169: source-session placement reattachment.
    • internal/app/placement.go:274-284: environment/runner construction.

    Recovery has its own by-value Config parameter; the unsafe reference is indirect, through the placement callback that still captures Build's variable. Delaying recovery alone therefore does not establish safe Config ownership for other callbacks.

    Proposed immediate plan for #2268

    1. Remove conflicting shared configuration mutation. Prefer a maintenance-local Config copy carrying the live svc.MaintenanceMutationAvailable method, passed to the maintenance workers, rather than modifying the Config already captured by placement/service callbacks. Do not cache availability as a boolean. Capture diagnostics separately for the earlier reservation-reconciliation callback, whose value-receiver cfg.diag() also copies mutable Config. Check remaining publication/teardown captures, including closeProfiles, rather than asserting all startup/close races are solved by the one assignment change.
    2. Start the two durable learning loops at the successful end of composition. Move attempt recovery and automatic-reservation reconciliation after dependencies are wired and cleanup ownership is established, before Build returns. This is a defined lifecycle boundary, not a sleep or listener-readiness requirement. Preserve immediate discovery after launch. Explicitly stop/join the loops before closing their dependencies; cover startup failures without orphaning workers or double-closing resources. Keep this scoped rather than rewriting all Build workers.
    3. Verify through real composition. Retain the two-Build restart scenario and focused race stress. Test that neither loop discovers work while composition is blocked or fails, and that Built.Close joins them before backend teardown. Maintenance tests must prove unavailable exclusion prevents GC scans/scheduling—not merely deletion, since the service deleter independently refuses unavailable exclusion. Include an available positive control and live availability-loss coverage. Preserve exact placement identity, alternate-root behavior, shell-disable gates and environment scrubbing.

    Automatic-reservation reconciliation is accounting repair, not model analysis: the admission ledger and attempt repository are separate stores. The loop resolves expired, still-held budget reservations against the corresponding attempt, retaining charges for admitted work or reclaiming unused reservations where creation authority was not consumed. Already committed create authority is conservatively charged; this is not a refund loop for failed attempts. It currently launches immediately and normally polls every second (internal/app/automatic_reservation_reconciliation.go, engine/learning/automatic_ledger.go).

    Provisioner / execution-environment implications

    The immediate race/lifecycle fix does not require changing provisioner APIs, persisted environment identities, or execution setup. Keep existing exact reattachment initially.

    There is a separate coupling worth evaluating with the placement/provisioner owner:

    • Learning recovery reads stored conversation/event evidence and uses the source session's verified project context to resolve learning policy and scope publication. The inspected reflection path does not execute Shell or consume the returned runner.
    • Today, obtaining that authoritative context goes through full PlacementReattacher.Reattach, which may construct workspace, ledger, runner and environment. For microVM, cold resolve can restore/boot a VM even though learning only needs validated context.
    • A context-only operation would need to preserve exact ref/revision, owner/session binding, deployment scope and host governance-root authority. Never substitute a stored path, configured cwd, guest /workspace, or a fresh Bind/provision operation.
    • Local/Redis/Kubernetes adapters have potential narrower validation paths. MicroVM's current inspect requires an active attachment; avoiding cold boot needs client/daemon metadata-validation work, including deletion state and repository/source-checkout association. Existing binding checks alone are not the complete substitute for reattachment.
    • Treat that separation as follow-up design work, not a dependency of the fix(app): flaky startup data race between attempt recovery and Build config mutation #2268 fix. Compatibility with custom providers and cold microVM behavior must be explicitly decided before claiming recovery never acquires execution resources.
    • One related integration finding to verify: recovery currently omits BindingID, while the Kubernetes execution provider requires it for exact reattachment (internal/adapter/executionclient/client.go:770-779). The context-resolution follow-up should forward/validate the source session ID rather than weaken binding checks.

    Status / verification

    Investigation and provisional planning only; no fix has been implemented, no implementation branch or commits created. The earlier narrower plan received static review; the expanded lifecycle/context work is not finalized or implementation-approved. A subsequent delegated 200-repeat race run passed without reproducing the interleaving; that does not invalidate the issue's recorded race or prove a fix.

    Use the focused reproduction in the issue, recovery/placement/maintenance tests, then the repository gates (task test, task lint, task test:race, offline demo) and implementation review. No retries, recovery suppression or timing sleeps to mask the race. #1785's authorization-stream flake remains separate and deferred.

  3. jbeda commented on Oct 12, 2026

    @jbeda
    ContributorAuthor

    Ozz -- I assigned this one to you. I hit this flake a couple of times but fixing it includes a mix of the dream/learning features and the environment provisioning. Right now there is no way to inspect an environment without creating a full microVM. Not sure how we want to resolve this but having the API capability to at least examine things without loading the full environment seems like a good thing.

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

Metadata

Metadata

Assignees

Labels

bugciflake

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions