Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(oracle): retire the session after a response decode error

Jeffda3c8cbc964b
diff --git a/agents/drivers/oracle-go/main.go b/agents/drivers/oracle-go/main.go
index 2b41f12e76..151df21a1c 100644
--- a/agents/drivers/oracle-go/main.go
+++ b/agents/drivers/oracle-go/main.go
@@ -5067,6 +5067,10 @@ func shouldRetryOracleXMLTypeRewrite(err error) bool {
if err == nil {
return false
}
+ var decodeErr oracleProtocolDecodeError
+ if errors.As(err, &decodeErr) {
+ return true
+ }
message := err.Error()
return strings.Contains(message, "abnormal data representation for date") ||
strings.Contains(message, "TTC error: received code ")
@@ -6160,10 +6164,10 @@ func (s *server) setSchema(schema string) error {
}
// repinPoisonedStatementConn replaces the statement's pinned connection once a
-// driver panic has poisoned it, so the placeholder fallbacks of the same
-// statement do not fail on the dead session (issue #13455). The fresh
-// connection gets the statement's CURRENT_SCHEMA back; the statement's owner
-// closes it together with the original one.
+// driver panic or a response decode error has poisoned it, so the fallbacks of
+// the same statement do not fail on the dead session (issues #13455, #13667).
+// The fresh connection gets the statement's CURRENT_SCHEMA back; the
+// statement's owner closes it together with the original one.
func (s *server) repinPoisonedStatementConn() error {
if s.manualTx != nil || s.statementConn == nil || !oracleConnPoisoned(s.statementConn) {
return nil
diff --git a/agents/drivers/oracle-go/main_test.go b/agents/drivers/oracle-go/main_test.go
index 9a7cb88989..56819ae309 100644
--- a/agents/drivers/oracle-go/main_test.go
+++ b/agents/drivers/oracle-go/main_test.go
@@ -4457,6 +4457,72 @@ func TestManualTransactionRewritesObjectColumnsBeforeTheFirstAttempt(t *testing.
}
}
+// sdoGeometryDecodeErrorFallbackSteps scripts a SELECT whose response go-ora
+// misreads (a NULL MDSYS.SDO_GEOMETRY value), followed by the WKT rewrite. The
+// retry must not run on the session that misread the response (issue #13667).
+func sdoGeometryDecodeErrorFallbackSteps(decodeErr error, rows [][]driver.Value) []oracleViewSourceQueryStep {
+ return []oracleViewSourceQueryStep{
+ {exec: true, queryContains: `ALTER SESSION SET CURRENT_SCHEMA = "DBXT"`},
+ {queryContains: `SELECT * FROM "T_GEOM"`, args: []driver.Value{}, err: decodeErr},
+ // The poisoned pinned connection is replaced and gets the editor schema back.
+ {exec: true, queryContains: `ALTER SESSION SET CURRENT_SCHEMA = "DBXT"`},
+ {queryContains: "CURRENT_SCHEMA", args: []driver.Value{}, columns: []string{"SCHEMA"}, rows: [][]driver.Value{{"DBXT"}}},
+ {
+ queryContains: "ALL_TAB_COLUMNS",
+ args: []driver.Value{"DBXT", "T_GEOM"},
+ columns: []string{"COLUMN_NAME", "DATA_TYPE", "DATA_TYPE_OWNER"},
+ rows: [][]driver.Value{{"ID", "NUMBER", nil}, {"GEOM", "SDO_GEOMETRY", "MDSYS"}},
+ },
+ {queryContains: "SDO_UTIL.TO_WKTGEOMETRY", args: []driver.Value{}, columns: []string{"ID", "GEOM"}, rows: rows},
+ }
+}
+
+func TestExecuteQueryRetriesDecodeErrorOnFreshPinnedConnection(t *testing.T) {
+ steps := sdoGeometryDecodeErrorFallbackSteps(
+ errors.New("TTC error: received code 3 during response reading"),
+ [][]driver.Value{{int64(1), nil}, {int64(2), "POINT (121.5 25.03)"}},
+ )
+ db, scripted := openGuardedOracleViewSourceTestDB(t, steps)
+ s := newServer()
+ s.db = db
+ result, err := s.executeQuery(queryOptions{SQL: `SELECT * FROM "T_GEOM"`, Schema: "DBXT", MaxRows: 10})
+ if err != nil {
+ t.Fatalf("expected the WKT rewrite to recover the decode error, got %v", err)
+ }
+ if !reflect.DeepEqual(result.Rows, [][]any{{int64(1), nil}, {int64(2), "POINT (121.5 25.03)"}}) {
+ t.Fatalf("unexpected rows: %#v", result.Rows)
+ }
+ if scripted.next != len(steps) {
+ t.Fatalf("expected all %d scripted steps, ran %d", len(steps), scripted.next)
+ }
+ if s.statementConn != nil || s.repinnedStatementConn != nil || s.statementSchema != "" || db.Stats().InUse != 0 {
+ t.Fatalf("statement leaked its pinned connections: %+v", db.Stats())
+ }
+}
+
+func TestExecuteQueryPageRetriesNonTTCDecodeErrorOnFreshPinnedConnection(t *testing.T) {
+ steps := sdoGeometryDecodeErrorFallbackSteps(
+ errors.New("invalid NUMBER"),
+ [][]driver.Value{{int64(1), nil}},
+ )
+ db, scripted := openGuardedOracleViewSourceTestDB(t, steps)
+ s := newServer()
+ s.db = db
+ result, err := s.executeQueryPage(queryOptions{SQL: `SELECT * FROM "T_GEOM"`, Schema: "DBXT", MaxRows: 10}, 10)
+ if err != nil {
+ t.Fatalf("expected the WKT rewrite to recover the decode error, got %v", err)
+ }
+ if !reflect.DeepEqual(result.Rows, [][]any{{int64(1), nil}}) || result.HasMore {
+ t.Fatalf("unexpected page: %+v", result)
+ }
+ if scripted.next != len(steps) {
+ t.Fatalf("expected all %d scripted steps, ran %d", len(steps), scripted.next)
+ }
+ if s.statementConn != nil || s.repinnedStatementConn != nil || db.Stats().InUse != 0 {
+ t.Fatalf("statement leaked its pinned connections: %+v", db.Stats())
+ }
+}
+
func TestReadQuerySessionPageConvertsPanicToError(t *testing.T) {
// A nil *sql.Rows makes rows.Next() panic with a nil pointer dereference,
// which stands in for go-ora panicking while decoding an unknown type.
diff --git a/agents/drivers/oracle-go/query_guard.go b/agents/drivers/oracle-go/query_guard.go
index 4db4a68574..b02296118d 100644
--- a/agents/drivers/oracle-go/query_guard.go
+++ b/agents/drivers/oracle-go/query_guard.go
@@ -4,7 +4,9 @@ import (
"context"
"database/sql"
"database/sql/driver"
+ "errors"
"fmt"
+ "strings"
)
// Preserve the thin driver's optional interfaces (notably transaction options,
@@ -44,6 +46,10 @@ type oracleQueryConnection struct {
}
func (c *oracleQueryConnection) unusableError() error {
+ var decodeErr oracleProtocolDecodeError
+ if errors.As(c.failed, &decodeErr) {
+ return fmt.Errorf("Oracle connection is unusable after a response decode error; roll back the transaction or reconnect: %w", c.failed)
+ }
return fmt.Errorf("Oracle connection is unusable after a driver panic; roll back the transaction or reconnect: %w", c.failed)
}
@@ -57,10 +63,41 @@ func (c *oracleQueryConnection) QueryContext(ctx context.Context, query string,
rows, err = nil, c.unusableError()
}
}()
- return c.oracleThinConnection.QueryContext(ctx, query, args)
+ rows, err = c.oracleThinConnection.QueryContext(ctx, query, args)
+ if isOracleProtocolDecodeFailure(err) {
+ // The driver stopped reading in the middle of the response, so the rest
+ // of it is still on the wire and the next call on this session reads it
+ // as its own reply (issue #13667). Retire the session like a panic does.
+ c.failed = oracleProtocolDecodeError{err: err}
+ return nil, c.failed
+ }
+ return rows, err
+}
+
+// oracleProtocolDecodeError marks a go-ora failure to decode a server response.
+// It keeps the driver's message so callers see the original error text.
+type oracleProtocolDecodeError struct{ err error }
+
+func (e oracleProtocolDecodeError) Error() string { return e.err.Error() }
+
+func (e oracleProtocolDecodeError) Unwrap() error { return e.err }
+
+// isOracleProtocolDecodeFailure reports whether err is one of the go-ora errors
+// raised when it misreads a response, for example on a NULL value of an object
+// type such as MDSYS.SDO_GEOMETRY. Server errors (ORA-xxxxx) carry none of these
+// messages, so they leave the session in service.
+func isOracleProtocolDecodeFailure(err error) bool {
+ if err == nil {
+ return false
+ }
+ message := err.Error()
+ return strings.Contains(message, "TTC error: received code ") ||
+ strings.Contains(message, "invalid size for GetInt") ||
+ strings.Contains(message, "invalid NUMBER")
}
-// oracleConnPoisoned reports whether a driver panic already made conn unusable.
+// oracleConnPoisoned reports whether a driver panic or a response decode error
+// already made conn unusable.
func oracleConnPoisoned(conn *sql.Conn) bool {
poisoned := false
_ = conn.Raw(func(driverConn any) error {
diff --git a/agents/drivers/oracle-go/query_guard_test.go b/agents/drivers/oracle-go/query_guard_test.go
index c383eb36aa..cd8e4f43a9 100644
--- a/agents/drivers/oracle-go/query_guard_test.go
+++ b/agents/drivers/oracle-go/query_guard_test.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "strings"
"testing"
"time"
)
@@ -256,3 +257,47 @@ func TestOracleQueryPanicAllowsManualTransactionCleanup(t *testing.T) {
})
}
}
+
+func TestOracleQueryDecodeErrorRetiresConnectionButServerErrorDoesNot(t *testing.T) {
+ cases := []struct {
+ name string
+ err error
+ retired bool
+ }{
+ {"ttc", errors.New("TTC error: received code 112 during response reading"), true},
+ {"number", errors.New("invalid NUMBER"), true},
+ {"int64", errors.New("invalid size for GetInt64: 9"), true},
+ {"server", errors.New("ORA-00942: table or view does not exist"), false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ conn := &oracleQueryConnection{oracleThinConnection: oracleGuardErrorConn{err: tc.err}}
+ _, err := conn.QueryContext(context.Background(), "SELECT * FROM T_GEOM", nil)
+ if err == nil || err.Error() != tc.err.Error() {
+ t.Fatalf("expected the driver message %q, got %v", tc.err, err)
+ }
+ var decodeErr oracleProtocolDecodeError
+ if errors.As(err, &decodeErr) != tc.retired || conn.IsValid() == tc.retired {
+ t.Fatalf("retired=%v, want %v (err %T)", !conn.IsValid(), tc.retired, err)
+ }
+ if !tc.retired {
+ return
+ }
+ _, next := conn.QueryContext(context.Background(), "SELECT 1 FROM DUAL", nil)
+ if next == nil || !strings.Contains(next.Error(), "unusable after a response decode error") {
+ t.Fatalf("a retired session must not run the next statement, got %v", next)
+ }
+ })
+ }
+}
+
+// oracleGuardErrorConn returns err from every query, like go-ora does after it
+// misreads a response.
+type oracleGuardErrorConn struct {
+ oracleThinConnection
+ err error
+}
+
+func (c oracleGuardErrorConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) {
+ return nil, c.err
+}