SQLite plugin treats SQLITE_BUSY and other unrelated errors as duplicate-key errors

Aperta Adatta ai principianti
#12,837 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ratdaddy ci sta già lavorando.

Dal 5/11/2026.

  • #12872 di @cestercian — aperta
  • #12877 di @Vamsi-klu — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, sqlite
Ambito
database

Direzione di ricerca

Inizia da common/persistence/sql/sqlplugin/sqlite/driver.go ed esamina i test esistenti del plugin SQLite. Aggiungi test di regressione per gli errori dovuti a chiavi duplicate e per gli errori SQLite non correlati, quindi esegui go test ./common/persistence/sql/sqlplugin/sqlite/ -run TestReproIsDupEntryError -v e i test di persistenza SQLite. Il lavoro è concluso quando solo le violazioni della chiave primaria e dei vincoli di unicità vengono trattate come duplicati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Expected Behavior

IsDupEntryError in the SQLite plugin should return true only for duplicate keys (primary key / unique constraint violations), the same as the MySQL and PostgreSQL plugins. Other errors, such as "database is locked", should be returned to the caller as errors.

Actual Behavior

It returns true for many unrelated errors, so callers treat real failures as "the row already exists".

The check in common/persistence/sql/sqlplugin/sqlite/driver.go is a bitwise AND against three OR'ed codes:

sqlConstraintCodes = sqlite3.SQLITE_CONSTRAINT | sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY | sqlite3.SQLITE_CONSTRAINT_UNIQUE // 19 | 1555 | 2067 = 0xE13
...
return sqlErr.Code()&sqlConstraintCodes != 0

modernc returns extended result codes, so any code that shares a bit with 0xE13 matches. That covers SQLITE_ERROR (1), SQLITE_BUSY (5), SQLITE_LOCKED (6), SQLITE_IOERR (10), SQLITE_FULL (13) and NOT NULL violations (1299), among others.

What this looks like from the store side:

  • PutReplicationTaskToDLQ (execution_tasks.go:608) ignores duplicates on purpose. When the database is locked, it returns nil and the replication task is never written to the DLQ.
  • CreateQueue (queue_v2.go:203) returns queue already exists. service/history/queues/dlq_writer.go then ignores that error.
  • createExecution, AppendHistoryNodes and CreateNamespace return "already running", "condition failed" and "namespace already exists" errors instead of the real one.
  • task_queues.go, task_user_data.go, nexus_endpoint_store.go and shard.go use the same check.

SQLITE_BUSY happens in file mode when another connection holds the write lock (e.g. MaxConns > 1, or a second process on the same file), since busy_timeout defaults to 0.

Steps to Reproduce the Problem

1. Call IsDupEntryError on real driver errors

Put this in common/persistence/sql/sqlplugin/sqlite/ and run go test ./common/persistence/sql/sqlplugin/sqlite/ -run TestReproIsDupEntryError -v:

func TestReproIsDupEntryError(t *testing.T) {
	dsn := "file:" + filepath.Join(t.TempDir(), "t.db")
	conn, _ := sqlx.Connect(goSQLDriverName, dsn)
	defer conn.Close()
	conn.MustExec(`CREATE TABLE t (id INTEGER PRIMARY KEY, nn TEXT NOT NULL)`)
	conn.MustExec(`INSERT INTO t VALUES (1, 'x')`)

	_, dupErr := conn.Exec(`INSERT INTO t VALUES (1, 'x')`)
	_, notNullErr := conn.Exec(`INSERT INTO t (id) VALUES (2)`)
	_, noTableErr := conn.Exec(`SELECT * FROM missing`)

	// Another connection holds the write lock.
	other, _ := sqlx.Connect(goSQLDriverName, dsn)
	defer other.Close()
	lock, _ := other.Conn(context.Background())
	lock.ExecContext(context.Background(), "BEGIN IMMEDIATE")
	_, busyErr := conn.Exec(`INSERT INTO t VALUES (3, 'x')`)
	lock.ExecContext(context.Background(), "ROLLBACK")

	d := &db{}
	for _, err := range []error{dupErr, notNullErr, noTableErr, busyErr} {
		t.Logf("IsDupEntryError=%-5v  %v", d.IsDupEntryError(err), err)
	}
}

Output on main:

IsDupEntryError=true   constraint failed: UNIQUE constraint failed: t.id (1555)
IsDupEntryError=true   constraint failed: NOT NULL constraint failed: t.nn (1299)
IsDupEntryError=true   SQL logic error: no such table: missing (1)
IsDupEntryError=true   database is locked (5) (SQLITE_BUSY)

Only the first line should be true.

2. Same thing through the store

Create the SQLite DB in file mode through the plugin, hold the write lock from a second connection with BEGIN IMMEDIATE, then call the store:

err = store.PutReplicationTaskToDLQ(ctx, &persistence.PutReplicationTaskToDLQRequest{
	ShardID:           1,
	SourceClusterName: "remote",
	TaskInfo:          &persistencespb.ReplicationTaskInfo{TaskId: 42},
})
// main: err == nil, but nothing was written

_, err = queue.CreateQueue(ctx, &persistence.InternalCreateQueueRequest{
	QueueType: persistence.QueueTypeHistoryDLQ,
	QueueName: "q",
})
// main: "queue already exists: queue type 2 and name q", but the queue was never created

Possible fix

Compare the exact extended codes:

switch sqlErr.Code() {
case sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY, sqlite3.SQLITE_CONSTRAINT_UNIQUE:
	return true
}
return false

With this change, the test above prints:

IsDupEntryError=true   constraint failed: UNIQUE constraint failed: t.id (1555)
IsDupEntryError=false  constraint failed: NOT NULL constraint failed: t.nn (1299)
IsDupEntryError=false  SQL logic error: no such table: missing (1)
IsDupEntryError=false  database is locked (5) (SQLITE_BUSY)

Both store calls return the database is locked error, and go test ./common/persistence/tests/ -run SQLite still passes (966 subtests, including the *_Fail_Duplicate cases).

I have this change and a regression test ready, and I'm happy to open a PR if this looks right.

Specifications

  • Version: main (7726647d6)
  • Platform: SQLite plugin (modernc.org/sqlite), macOS arm64, Go 1.27.3
Lingua principale
Go
Stelle
24.7k
Fork
2.2k
Merge medio
3g 7h
PR unite (30g)
222

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di temporalio/temporal

Tutte le issue di temporalio/temporal

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.