{"record":{"id":"e232093efd51701e","repo":"microsoft/aspire","slug":"not-connected-to-apphost-transport","errorCode":null,"errorMessage":"not connected to AppHost","messagePattern":"not connected to AppHost","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Go/Resources/transport.go","lineNumber":452,"sourceCode":"\t\tctx = context.Background()\n\t}\n\tif err := ctx.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\n\tid := c.nextID.Add(1)\n\trespCh := make(chan map[string]any, 1)\n\tmsg := map[string]any{\n\t\t\"jsonrpc\": \"2.0\",\n\t\t\"id\":      id,\n\t\t\"method\":  method,\n\t\t\"params\":  params,\n\t}\n\n\tc.mu.Lock()\n\tif c.closed {\n\t\tc.mu.Unlock()\n\t\treturn nil, errors.New(\"not connected to AppHost\")\n\t}\n\tc.pending[id] = respCh\n\tc.mu.Unlock()\n\n\tselect {\n\tcase c.writeQueue <- msg:\n\tcase <-ctx.Done():\n\t\tc.mu.Lock()\n\t\tdelete(c.pending, id)\n\t\tc.mu.Unlock()\n\t\treturn nil, ctx.Err()\n\tcase <-c.done:\n\t\tc.mu.Lock()\n\t\tdelete(c.pending, id)\n\t\tc.mu.Unlock()\n\t\treturn nil, errors.New(\"not connected to AppHost\")\n\t}\n","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Go/Resources/transport.go#L434-L470","documentation":"This error is returned by the JSON-RPC connection's sendRequest when the connection has already been closed (c.closed is true) at the moment a request is queued or while it is awaiting a response. It indicates the AppHost server side of the Unix-socket transport is gone (disconnected, shut down, or never fully started), so pending JSON-RPC calls cannot be delivered or answered.","triggerScenarios":"Calling any generated capability-invoking API after the AppHost server closed the socket: (1) the writeQueue send races with connection close (line 452), (2) the connection's done channel fires while the request is queued (line 468) or while waiting for the response and no response arrived (line 484). Typically triggered by AppHost process exit, server-initiated disconnect, or calling APIs after a prior error tore the connection down.","commonSituations":"The `aspire run` AppHost was stopped or crashed while the generated Go client was mid-operation; long-running requests outliving the AppHost; calling client APIs after an authentication failure caused connect() to disconnect; reusing a client whose connection was dropped by the onConnectionClose handler.","solutions":["Check that the AppHost is still running (`aspire run` active) before or while making calls","Reconnect by calling CreateBuilder again (connect is idempotent and re-establishes c.conn) or implement a reconnect-on-disconnect loop via client.onDisconnect","Wrap calls in retry logic that treats this error as a transient disconnection and re-attempts after reconnecting","Verify no earlier authentication or connection error silently tore down the connection; log onDisconnect callbacks"],"exampleFix":"// before\nbuilder, err := CreateBuilder(ctx) // AppHost may have exited\n\n// after\nbuilder, err := CreateBuilder(ctx)\nif err != nil {\n\tif strings.Contains(err.Error(), \"not connected to AppHost\") {\n\t\ttime.Sleep(retryDelay)\n\t\tbuilder, err = CreateBuilder(ctx) // reconnect and retry\n\t}\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}","handlingStrategy":"try-catch","validationCode":"if client == nil || !clientHealthy(client) {\n\t// skip the call and (re)connect first\n}","typeGuard":"func isConnected(c *AppHostClient) bool {\n\treturn c != nil && c.Connected()\n}","tryCatchPattern":"builder, err := CreateBuilder(ctx)\nif err != nil {\n\tif strings.Contains(err.Error(), \"not connected to AppHost\") {\n\t\t// reconnect + retry with backoff\n\t}\n\treturn err\n}","preventionTips":["Keep the AppHost process alive for the client's whole lifetime","Register onDisconnect to detect drops early and trigger reconnect","Add retry-with-backoff around capability calls treating this as transient","Check the error returned by CreateBuilder before using any derived handles"],"tags":["go","rpc","connection","apphost","transport"],"backgroundTag":"connection-refused","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"}