{"record":{"id":"ba81d47d7d93eda4","repo":"t8y2/dbx","slug":"close-hive-initfile-statement-result-w","errorCode":null,"errorMessage":"close Hive initFile statement result: %w","messagePattern":"close Hive initFile statement result: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"agents/drivers/argo-go/main.go","lineNumber":384,"sourceCode":"}\n\nfunc runHiveInitStatements(ctx context.Context, connection *sql.Conn, statements []string, fetchSize int) error {\n\tfor _, statement := range statements {\n\t\trows, _, hasResultSet, err := executeHiveStatement(ctx, connection, statement, fetchSize)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"execute Hive initFile statement: %w\", err)\n\t\t}\n\t\tif !hasResultSet {\n\t\t\tcontinue\n\t\t}\n\t\tfor rows.Next() {\n\t\t}\n\t\tif rowsErr := rows.Err(); rowsErr != nil {\n\t\t\t_ = rows.Close()\n\t\t\treturn fmt.Errorf(\"read Hive initFile statement result: %w\", rowsErr)\n\t\t}\n\t\tif closeErr := rows.Close(); closeErr != nil {\n\t\t\treturn fmt.Errorf(\"close Hive initFile statement result: %w\", closeErr)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (server *server) dispatch(method string, params map[string]json.RawMessage) (any, bool, error) {\n\tswitch method {\n\tcase \"validate_connection\":\n\t\treturn map[string]bool{\"ok\": true}, false, server.validateConnection()\n\tcase \"connection_info\":\n\t\tresult, err := server.connectionInfo()\n\t\treturn result, false, err\n\tcase \"list_databases\":\n\t\tresult, err := server.listDatabases()\n\t\treturn result, false, err\n\tcase \"list_schemas\":\n\t\tresult, err := server.listSchemas(stringSliceParam(params, \"visible_schemas\"))\n\t\treturn result, false, err","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/argo-go/main.go#L366-L402","documentation":"Error from `runHiveInitStatements` when `rows.Close()` returns a non-nil error after successfully draining an initFile statement's result set. The close error is wrapped with %w and returned; it is the rarest of the three init-statement errors and usually reflects the underlying Hive driver's connection being in a bad state when releasing the result set.","triggerScenarios":"An initFile statement with a result set is drained without error, but `rows.Close()` fails — typically because the underlying *sql.Conn was concurrently closed or errored (context cancellation racing the drain, server already closed the connection) so the driver cannot cleanly release the rows.","commonSituations":"Connect timeout expiring just as the drain finishes so the context-cancelled connection fails on Close; Hive driver version bugs surfacing errors from Close after a server-side abort; connection pooling returning an already-broken conn.","solutions":["Unwrap (%w) and check whether Close failed due to context cancellation — if so, increase the connect timeout; the statement itself succeeded, so this is usually benign.","Upgrade the Hive database/sql driver; several versions leak errors from rows.Close after server-side connection aborts.","Ensure nothing closes the shared *sql.Conn concurrently while init statements run (openConnection holds connectionMu, but external cancel() can fire).","Retry the connection open; a failed Close after a successful read rarely indicates a bad init statement."],"exampleFix":"// before\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) // too tight\n// after\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) // allow drain+close to finish","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := runHiveInitStatements(ctx, conn, stmts, fetchSize)\nif err != nil {\n    var closeErr error\n    if errors.As(err, &closeErr) && strings.Contains(err.Error(), \"close Hive initFile statement result\") {\n        log.Printf(\"init statement read succeeded but rows.Close failed (likely benign): %v\", err)\n        return nil // or reconnect once if the conn is now unusable\n    }\n    return err\n}","preventionTips":["Don't cancel the connect context while init statements are still draining; give the timeout enough headroom.","Keep the Hive database/sql driver up to date; old versions leak errors from rows.Close after server aborts.","Never share or close the *sql.Conn from outside the openConnection critical section.","If it recurs, reconnect — a failed Close after a successful read rarely invalidates the init statements themselves."],"tags":["go","hive","sql","resource-cleanup","rows-close"],"backgroundTag":"connection-closed-error","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}