{"record":{"id":"98e86c38cd5c3a16","repo":"wavetermdev/waveterm","slug":"error-getting-rpc-response-handler-from-context","errorCode":null,"errorMessage":"error getting rpc response handler from context","messagePattern":"error getting rpc response handler from context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/remote/connparse/connparse.go","lineNumber":88,"sourceCode":"\tif conn.Host == ConnHostCurrent {\n\t\tsource, err := GetConnNameFromContext(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error getting connection name from context: %v\", err)\n\t\t}\n\n\t\t// RPC context connection is empty for local connections\n\t\tif source == \"\" {\n\t\t\tsource = wshrpc.LocalConnName\n\t\t}\n\t\tconn.Host = source\n\t}\n\treturn conn, nil\n}\n\nfunc GetConnNameFromContext(ctx context.Context) (string, error) {\n\thandler := wshutil.GetRpcResponseHandlerFromContext(ctx)\n\tif handler == nil {\n\t\treturn \"\", fmt.Errorf(\"error getting rpc response handler from context\")\n\t}\n\treturn handler.GetRpcContext().Conn, nil\n}\n\n// ParseURI parses a connection URI and returns the connection type, host/path, and parameters.\nfunc ParseURI(uri string) (*Connection, error) {\n\tisWshShorthand := strings.HasPrefix(uri, \"//\")\n\tsplit := strings.SplitN(uri, \"://\", 2)\n\tvar scheme string\n\tvar rest string\n\tif isWshShorthand {\n\t\trest = strings.TrimPrefix(uri, \"//\")\n\t} else if len(split) > 1 {\n\t\tscheme = split[0]\n\t\trest = strings.TrimPrefix(split[1], \"//\")\n\t} else {\n\t\trest = split[0]\n\t}","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/remote/connparse/connparse.go#L70-L106","documentation":"GetConnNameFromContext extracts the connection name from the wshutil.RpcResponseHandler stored in ctx. This error is thrown when the context contains no RPC response handler, meaning the function was called outside of an active wsh RPC request and the connection name cannot be recovered. It is the direct source of the 'error getting connection name from context' wrapper in ParseURIAndReplaceCurrentHost.","triggerScenarios":"Calling GetConnNameFromContext (directly or via ParseURIAndReplaceCurrentHost when conn.Host == ConnHostCurrent) with a context created without wshutil's context middleware, so wshutil.GetRpcResponseHandlerFromContext(ctx) returns nil.","commonSituations":"Background goroutines that drop the original request context; CLI tools or scripts calling remote copy/move helpers outside the RPC server; tests using context.TODO(); handlers spawned with a detached context.","solutions":["Ensure the code path originates from an RPC request whose context was enriched via wshutil's context-with-handler middleware.","Attach a wshutil.RpcResponseHandler to the context before calling (wshutil package provides the With-style constructor used by the RPC server).","If running outside RPC, resolve the connection name by other means (config/default connection) and avoid the 'current' host placeholder.","In tests, set up a stub handler whose GetRpcContext().Conn returns the expected connection name."],"exampleFix":"// before\nname, err := connparse.GetConnNameFromContext(ctx)\n// after\nif wshutil.GetRpcResponseHandlerFromContext(ctx) == nil {\n    ctx = wshutil.ContextWithRpcResponseHandler(ctx, handler)\n}\nname, err := connparse.GetConnNameFromContext(ctx)","handlingStrategy":"try-catch","validationCode":"if wshutil.GetRpcResponseHandlerFromContext(ctx) == nil {\n    return \"\", fmt.Errorf(\"context has no rpc response handler\")\n}","typeGuard":null,"tryCatchPattern":"name, err := connparse.GetConnNameFromContext(ctx)\nif err != nil {\n    // fall back to a default/local connection\n    name = wshrpc.LocalConnName\n}","preventionTips":["Propagate the original RPC request ctx into goroutines instead of context.Background().","Use wshutil's context middleware consistently in all RPC entry points.","Add a debug assertion logging when the handler is absent during development.","Document that functions resolving 'current' require an RPC context."],"tags":["go","context","rpc"],"backgroundTag":"missing-rpc-handler-in-context","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}