{"record":{"id":"b5c3367b43de565f","repo":"microsoft/aspire","slug":"initial-resource-snapshot-load-has-not-completed-call","errorCode":null,"errorMessage":"Initial resource snapshot load has not completed. Call WaitForInitialLoadAsync first.","messagePattern":"Initial resource snapshot load has not completed\\. Call WaitForInitialLoadAsync first\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Backchannel/ResourceSnapshotWatcher.cs","lineNumber":266,"sourceCode":"                    .ToArray()\n                : updates\n                    .Where(update => update.Sequence > afterSequence)\n                    .OrderBy(update => update.Sequence)\n                    .Select(update => update.Snapshot)\n                    .ToArray();\n\n            if (snapshots.Length > 0)\n            {\n                yield return new ResourceSnapshotUpdateBatch(snapshots, isResync);\n            }\n        }\n    }\n\n    private void EnsureInitialLoadComplete()\n    {\n        if (!_initialLoadTcs.Task.IsCompletedSuccessfully)\n        {\n            throw new InvalidOperationException(\"Initial resource snapshot load has not completed. Call WaitForInitialLoadAsync first.\");\n        }\n    }\n\n    /// <summary>\n    /// Gets a resource snapshot by name, or <see langword=\"null\"/> if not found.\n    /// </summary>\n    public ResourceSnapshot? GetResource(string name)\n    {\n        EnsureInitialLoadComplete();\n        lock (_resourcesLock)\n        {\n            return _resources.GetValueOrDefault(name);\n        }\n    }\n\n    /// <summary>\n    /// Gets all current resource snapshots, using <see cref=\"IncludeHidden\"/> to determine visibility.\n    /// </summary>","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Backchannel/ResourceSnapshotWatcher.cs#L248-L284","documentation":"ResourceSnapshotWatcher methods that read state (GetResource, GetResources, CaptureAllResources, WatchResourceSnapshotBatchesAsync) call EnsureInitialLoadComplete, which throws InvalidOperationException until the initial snapshot load task has completed successfully. The watcher requires callers to await WaitForInitialLoadAsync first so reads observe a consistent baseline.","triggerScenarios":"Calling GetResource/GetResources/CaptureAllResources/WatchResourceSnapshotBatchesAsync before awaiting WaitForInitialLoadAsync, or after the initial load failed/cancelled (Task not completed successfully).","commonSituations":"Fire-and-forget startup where reads race the initial GET from the AppHost; initial load failed due to a dropped AppHost connection so the task never completes successfully; calling snapshot APIs synchronously right after constructing the watcher.","solutions":["Await watcher.WaitForInitialLoadAsync() before any snapshot read or update streaming.","Handle failures of WaitForInitialLoadAsync (it propagates the initial-load exception) and reconnect/retry before reading.","If reads keep racing, restructure startup so watcher initialization is awaited before dependent services start."],"exampleFix":"// before\nvar watcher = new ResourceSnapshotWatcher(connection);\nvar resources = watcher.GetResources(); // throws\n// after\nvar watcher = new ResourceSnapshotWatcher(connection);\nawait watcher.WaitForInitialLoadAsync();\nvar resources = watcher.GetResources();","handlingStrategy":"validation","validationCode":"// gate all snapshot reads on initial load\nawait watcher.WaitForInitialLoadAsync();\n// then safe to call GetResource/GetResources/WatchResourceSnapshotBatchesAsync","typeGuard":null,"tryCatchPattern":"try { await watcher.WaitForInitialLoadAsync(ct); }\ncatch (Exception ex) { /* initial load failed: reconnect/retry before any reads */ }","preventionTips":["Always await WaitForInitialLoadAsync immediately after creating the watcher","Sequence dependent services after watcher initialization completes","Watch for propagated initial-load failures — they also block reads"],"tags":["cli","watcher","initialization-order","async"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}