Skip to content
quarry Artifact [2cbb9a10dd]
Login

Artifact [2cbb9a10dd]

Artifact 2cbb9a10ddf35a9322960924e34e2923a6fab996042d75a21259e41e6784933b:


package sqlmem

import (
	"context"
	"database/sql/driver"
	"fmt"
	"log"
	"reflect"
	"runtime/debug"
	"strings"
	"sync/atomic"
)

// The ncruces driver panics, rather than returns an error, when its
// module cannot grow past the connection's memory ceiling
// (errutil.OOMErr), and any fault inside the module (an assertion, a
// trap, an out-of-range slice in the transpiled code) is a panic too.
// database/sql does not recover it, so the connection stays checked out
// of the pool for good and a few such queries exhaust the pool, and
// unrecovered the panic ends the process. Every call into the driver is
// guarded below: ANY panic raised inside it becomes a *PanicError, is
// logged once with its stack, is counted, and marks the connection dead
// so IsValid reports false and database/sql closes it when it is
// released, freeing its slot.
//
// Everything is recovered, not only the driver's own values, because the
// forge registers no Go callbacks the driver could call back into (no
// custom SQL functions, collations, hooks, VFS or tracing; fts5 is
// compiled into the module), so a panic inside a guarded call is the
// driver's. If such a callback is ever added, a panic that began in it
// must be let through here.
//
// Why an ordinary error and not driver.ErrBadConn for the call that
// panicked: database/sql retries a call that returns ErrBadConn on a new
// connection, and the statement may have partly run (an INSERT of
// several rows, a write inside a transaction), and a statement that ran
// out of memory would only run out again. So the call that panicked
// returns a *PanicError and the dead mark does the discarding. A call
// that reaches an already dead connection ran nothing, so outside a
// transaction it returns ErrBadConn and a retry on a new connection is
// safe; inside a transaction it returns an error that wraps the
// *PanicError, never ErrBadConn, so the transaction is not retried
// half-way.
//
// The guards are written inline, a deferred method per call, because
// they sit on every row and every query: no closure, no generic helper.

var panicOOM, panicOther atomic.Int64

// Panics is how many driver panics this process has turned into errors:
// each one a connection that would have been lost from its pool.
func Panics() int64 { return panicOOM.Load() + panicOther.Load() }

// PanicsOOM is those that were the driver running out of memory, which
// is a query past its connection's ceiling.
func PanicsOOM() int64 { return panicOOM.Load() }

// PanicsOther is those that were anything else: a fault in the driver,
// worth a look.
func PanicsOther() int64 { return panicOther.Load() }

// PanicError is a driver panic returned as an error. The connection it
// happened on is discarded; the call may have partly run.
type PanicError struct{ Value any }

func (e *PanicError) Error() string {
	return fmt.Sprintf("sqlmem: the driver panicked and its connection was discarded (a query past the connection's memory ceiling does this): %v", e.Value)
}

func (e *PanicError) Unwrap() error {
	err, _ := e.Value.(error)
	return err
}

// fail is the first thing a recovered value meets: it marks the
// connection dead, and the first panic on a connection is counted and
// logged with the stack it was raised on.
func (l *lean) fail(r any) error {
	pe := &PanicError{Value: r}
	if !l.dead.CompareAndSwap(nil, pe) {
		return l.dead.Load()
	}
	if oom(r) {
		panicOOM.Add(1)
		log.Printf("sqlmem: ERROR: a query ran the connection out of memory; the connection is discarded: %v\n%s", r, debug.Stack())
	} else {
		panicOther.Add(1)
		log.Printf("sqlmem: ERROR: the driver panicked (%T); the connection is discarded: %v\n%s", r, r, debug.Stack())
	}
	return pe
}

func oom(r any) bool {
	err, ok := r.(error)
	return ok && strings.Contains(err.Error(), "out of memory")
}

// deadErr is nil on a live connection. On a dead one it is what a call
// that ran nothing returns.
func (l *lean) deadErr() error {
	pe := l.dead.Load()
	if pe == nil {
		return nil
	}
	if l.inTx.Load() {
		return fmt.Errorf("sqlmem: the connection was discarded after a driver panic earlier in this transaction: %w", pe)
	}
	return driver.ErrBadConn
}

// recovered is deferred by every guarded call that returns an error.
func (l *lean) recovered(err *error) {
	if r := recover(); r != nil {
		*err = l.fail(r)
	}
}

// swallowed is deferred by a guarded call that has no error to return;
// the connection is dead and the caller's next call finds out.
func (l *lean) swallowed() {
	if r := recover(); r != nil {
		l.fail(r)
	}
}

// recoveredClose is deferred by the calls that release what a call
// holds. They still run on a dead connection (dead is its state before
// the call): the driver cannot close a connection that still has
// unfinalized statements, and one left open keeps its locks and its
// memory. A dead connection's release never reports an error; the
// connection's own Close frees the rest.
func (l *lean) recoveredClose(err *error, dead bool) {
	if r := recover(); r != nil {
		e := l.fail(r)
		if !dead {
			*err = e
			return
		}
	}
	if dead {
		*err = nil
	}
}

func (l *lean) Close() (err error) {
	defer l.recovered(&err)
	return l.conn.Close()
}

func (l *lean) Begin() (driver.Tx, error) {
	return l.BeginTx(context.Background(), driver.TxOptions{})
}

func (l *lean) BeginTx(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) {
	if err = l.deadErr(); err != nil {
		return nil, err
	}
	defer l.recovered(&err)
	t, err = l.conn.BeginTx(ctx, opts)
	if err != nil {
		return nil, err
	}
	l.inTx.Store(true)
	return &tx{t, l}, nil
}

func (l *lean) Prepare(q string) (driver.Stmt, error) {
	return l.PrepareContext(context.Background(), q)
}

func (l *lean) PrepareContext(ctx context.Context, q string) (st driver.Stmt, err error) {
	if err = l.deadErr(); err != nil {
		return nil, err
	}
	defer l.recovered(&err)
	st, err = l.conn.PrepareContext(ctx, q)
	if err != nil {
		return nil, err
	}
	if full, ok := st.(stmtI); ok {
		return &stmt{full, l}, nil
	}
	return st, nil
}

func (l *lean) ExecContext(ctx context.Context, q string, args []driver.NamedValue) (res driver.Result, err error) {
	if err = l.deadErr(); err != nil {
		return nil, err
	}
	defer l.recovered(&err)
	return l.conn.ExecContext(ctx, q, args)
}

func (l *lean) CheckNamedValue(nv *driver.NamedValue) (err error) {
	defer l.recovered(&err)
	return l.conn.CheckNamedValue(nv)
}

type tx struct {
	driver.Tx
	l *lean
}

func (t *tx) Commit() (err error) {
	defer t.l.inTx.Store(false)
	if err = t.l.deadErr(); err != nil {
		return err // the transaction never committed; the connection is gone
	}
	defer t.l.recovered(&err)
	return t.Tx.Commit()
}

func (t *tx) Rollback() (err error) {
	defer t.l.inTx.Store(false)
	dead := t.l.dead.Load() != nil
	defer t.l.recoveredClose(&err, dead)
	return t.Tx.Rollback()
}

// stmtI is what the driver's statement implements and database/sql
// looks for.
type stmtI interface {
	driver.Stmt
	driver.StmtExecContext
	driver.StmtQueryContext
	driver.NamedValueChecker
}

type stmt struct {
	s stmtI
	l *lean
}

func (s *stmt) Close() (err error) {
	dead := s.l.dead.Load() != nil
	defer s.l.recoveredClose(&err, dead)
	return s.s.Close()
}

func (s *stmt) NumInput() (n int) {
	if s.l.dead.Load() != nil {
		return -1
	}
	n = -1
	defer s.l.swallowed()
	return s.s.NumInput()
}

func (s *stmt) Exec(args []driver.Value) (res driver.Result, err error) {
	if err = s.l.deadErr(); err != nil {
		return nil, err
	}
	defer s.l.recovered(&err)
	return s.s.Exec(args)
}

func (s *stmt) Query(args []driver.Value) (rs driver.Rows, err error) {
	if err = s.l.deadErr(); err != nil {
		return nil, err
	}
	defer s.l.recovered(&err)
	rs, err = s.s.Query(args)
	if err != nil {
		return nil, err
	}
	return s.l.wrapRows(rs), nil
}

func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
	if err = s.l.deadErr(); err != nil {
		return nil, err
	}
	defer s.l.recovered(&err)
	return s.s.ExecContext(ctx, args)
}

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rs driver.Rows, err error) {
	if err = s.l.deadErr(); err != nil {
		return nil, err
	}
	defer s.l.recovered(&err)
	rs, err = s.s.QueryContext(ctx, args)
	if err != nil {
		return nil, err
	}
	return s.l.wrapRows(rs), nil
}

func (s *stmt) CheckNamedValue(nv *driver.NamedValue) (err error) {
	defer s.l.recovered(&err)
	return s.s.CheckNamedValue(nv)
}

// rowsI is what the driver's rows implement and database/sql looks for.
type rowsI interface {
	driver.Rows
	driver.RowsColumnTypeDatabaseTypeName
	driver.RowsColumnTypeNullable
	driver.RowsColumnTypeScanType
}

type rows struct {
	r rowsI
	l *lean
}

func (l *lean) wrapRows(r driver.Rows) driver.Rows {
	if full, ok := r.(rowsI); ok {
		return newRows(full, l)
	}
	return r
}

func (r *rows) Close() (err error) {
	dead := r.l.dead.Load() != nil
	defer r.l.recoveredClose(&err, dead)
	return r.r.Close()
}

func (r *rows) Next(dest []driver.Value) (err error) {
	if err = r.l.deadErr(); err != nil {
		return err
	}
	defer r.l.recovered(&err)
	return r.r.Next(dest)
}

func (r *rows) Columns() (c []string) {
	if r.l.dead.Load() != nil {
		return nil
	}
	defer r.l.swallowed()
	return r.r.Columns()
}

func (r *rows) ColumnTypeDatabaseTypeName(i int) (s string) {
	if r.l.dead.Load() != nil {
		return ""
	}
	defer r.l.swallowed()
	return r.r.ColumnTypeDatabaseTypeName(i)
}

func (r *rows) ColumnTypeNullable(i int) (nullable, ok bool) {
	if r.l.dead.Load() != nil {
		return false, false
	}
	defer r.l.swallowed()
	return r.r.ColumnTypeNullable(i)
}

func (r *rows) ColumnTypeScanType(i int) (t reflect.Type) {
	t = reflect.TypeFor[any]()
	if r.l.dead.Load() != nil {
		return t
	}
	defer r.l.swallowed()
	return r.r.ColumnTypeScanType(i)
}