# An aborted SQLite BLOB handle can leave a committed partial write

**Research candidate, 15 November 2026.** Native software controls and primary-source
synthesis, not a newly discovered SQLite defect. No production publication is
implied by this packet.

## Main finding

An incremental BLOB operation is not an application-level replacement transaction.
In the accepted controls, the application intends to replace AAAA with BCDE in
two writes. It writes BC, updates only a revision column in the same row, then
tries to write DE. The second write returns SQLITE_ABORT (4). On reopening, the
committed payload is **BCAA**, not AAAA or BCDE; integrity_check reports ok.

With no explicit transaction, a fresh connection can already read BCAA after
the failed write, before sqlite3_blob_close. The writer's main transaction state
has changed from WRITE (2) to NONE (0). ROLLBACK then returns SQLITE_ERROR (1)
because no transaction is active. This precise timing is an observation on the
two recorded builds, not a promise that every SQLite error commits work.

With BEGIN IMMEDIATE, the same error leaves the writer in WRITE. An explicit
ROLLBACK restores AAAA. ROLLBACK TO a savepoint also restores the original target
row while retaining an earlier outer-transaction update to the sentinel row.

## Why the result follows from different contracts

The [BLOB-open contract](https://www.sqlite.org/c3ref/blob_open.html) says that
changing any column of the referenced row can expire its handle. Updating a
revision or progress field is therefore relevant even when the payload column
is not an SQL assignment target. The operation is on the same connection;
this packet does not test concurrent writers.

The [BLOB-write contract](https://www.sqlite.org/c3ref/blob_write.html) separates
the failed call from earlier completed calls. Expiration does not roll earlier
writes back. An out-of-bounds request, by contrast, rejects that request without
writing bytes and need not destroy the handle. Our corrected-offset retry
succeeds on the same handle; a retry after a failed reopen does not.

The [BLOB-reopen contract](https://www.sqlite.org/c3ref/blob_reopen.html) makes a
failed relocation terminal for that handle. Our nonexistent-row control first
returns ERROR (1); returning to the original valid row then returns ABORT (4).
The handle reports zero bytes, yet the database still contains a four-byte BLOB
with the earlier partial write. Handle-reported size is not a database row-size
measurement after abort.

[Transaction completion](https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions)
and [BLOB cleanup](https://www.sqlite.org/c3ref/blob_close.html) are separate from
the success of the intended multi-call update. A close result of zero does not
erase the earlier error. In the pinned old and new implementations,
blobReadWrite finalizes its internal statement when the B-tree call returns
SQLITE_ABORT and clears the statement pointer. This explains why the tested
implicit transaction can finish before the allocated BLOB object is closed.
See source receipts implementation-old and implementation-new in sources.json.

Do not infer transaction ownership from the word "abort" or the continued
existence of a handle. [sqlite3_txn_state](https://www.sqlite.org/c3ref/txn_state.html)
describes a connection/schema state, not whether an application has produced
the complete intended object.

## Method and evidence

The final matrix comprises ten designed schedules, each in DELETE and WAL
journal mode, on SQLite 3.45.1 and 3.54.1: **40 case executions**. These are the
two available binaries on one Linux host, not an estimate for a population and
not independent laboratory replication. Exact source IDs, compile options,
loaded-library paths and SHA-256 values are retained in each observations.json.

Each fixture begins with two four-byte payloads: row 1 AAAA and row 2 CCCC,
both revision zero. The savepoint case changes row 2's revision to seven before
opening the savepoint. First-write bytes are BC at offset zero. The intended
second-write bytes are DE at offset two; bounds controls deliberately request
offset three, which cannot fit a two-byte write in four bytes.

The harness calls the declared C APIs through ctypes, without a Python sqlite3
transaction wrapper. New read-only connections observe committed visibility
after the first and second writes, and are closed before the next writer step.
All calls are ordered, not concurrent. Accepted cases keep their complete SQL
and API order, return codes, per-step transaction state, reopened rows, integrity
results, and final 8192-byte database images. All final images are closed and
fully checkpointed before retention.

A separate offline verifier queries disposable copies through Python's sqlite3
module. It checks the actual images against hand-authored expected rows, not
merely agreement between JSON fields. No production database path is accepted
by the native capture entry point.

[results.md](results.md) and [summary.json](summary.json) are generated from the
validated final captures. They must not be edited as source data.

## What the controls establish

- Normal and different-row updates produce BCDE; updating an unrelated row did not expire this handle.
- Same-row revision updates followed by implicit completion or explicit COMMIT preserve BCAA.
- Explicit ROLLBACK restores the target and sentinel. ROLLBACK TO preserves the earlier sentinel revision seven.
- Failed reopen leaves BCAA after COMMIT; the aborted handle cannot be revived by targeting the original row.
- Out-of-bounds write plus COMMIT leaves BCAA; retrying the valid offset on the still-live handle produces BCDE.
- Replacing the payload with SQL yields ZZZZ. Expiration does not protect previous bytes from an intentional overwriting statement.
- All final images pass integrity_check. This is consistent with valid partial application state, not evidence that the checker is broken.

## Practical consequence

This is Hesela's engineering inference, not a complete upload library:
define the atomic unit before the first partial write. If replacement requires
all chunks plus metadata to change together, own an explicit transaction or
an appropriate savepoint; retain error results, close valid resources, and
roll back that owned unit when any required step fails. A transaction that
continues to COMMIT after an error can deliberately preserve partial work.

[Savepoint rollback](https://www.sqlite.org/lang_savepoint.html) retains work
before the savepoint and leaves the enclosing transaction active. Do not issue
an indiscriminate rollback on an unrelated pooled transaction. Closing a BLOB
or seeing an integrity-check success does not substitute for application-byte
and metadata acceptance.

Updating progress in the same row can invalidate the handle. Reopen a new valid
handle when appropriate, but do not treat reopening as a repair for already
committed incomplete data. Choose whether partial data is valid, resumable, or
must be discarded before designing a recovery protocol.

## Fit with the existing corpus

The existing [runtime-dependency analysis](https://hesela.com/analyses/sqlite-index-runtime-dependencies/)
already covers historical BLOB writes and stale generated values. This study
uses **no generated columns, expression indexes, callbacks or triggers**. Its
question is error/transaction scope, not recalculation.

The existing sqlite-transaction-state and sqlite-savepoint records are the
appropriate machine-readable anchors. The source-driven close-lifecycle study
already shows that cleanup and transaction outcome differ; this packet adds
an expiration-triggered internal-statement completion and a visible partial
payload. No new article, term identity or duplicate generated-column grid is
proposed. Integration text is in publication.md.

## Reproduce

From this directory, using Python 3.11+ with ctypes and sqlite3:

~~~sh
python3 -B verify.py captures/system-v2/observations.json
python3 -B verify.py captures/upstream-v2/observations.json
python3 -B probe.py --output-dir /tmp/new-blob-capture
python3 -B -m unittest -v test_probe
python3 -B -O -m unittest -v test_probe
python3 -B summarize.py
~~~

Use --library /absolute/path/libsqlite3.so.0 for a separately installed build.
Set SQLITE_LIBRARY to that path for native regression tests. The regression
test requires the recorded build identity; observations on a different engine
should be reviewed as a new run, not relabeled as either retained build.
Each native subprocess loads only the selected engine. No network is required
for capture/replay; collect_sources.py is a separate optional refresh operation.

## Limits and provenance

This is not a power-loss test, storage-device measurement, concurrent-writing
test, timing guarantee, defect-discovery claim or security vulnerability report.
The one small schema does not cover triggers, cascading constraints, schema
changes, attached databases, replacement rows or every I/O error class.
The replacement-payload case is an ordinary UPDATE, not INSERT OR REPLACE.
The distribution 3.45.1 source ID ends in alt1; inspected upstream source is
explanatory, not asserted to be the exact distribution build source.

Preliminary system/upstream captures lack the new independent connection
observations. They and the initial probe remain available as provenance;
the final matrix uses only system-v2/upstream-v2. Replays repeat those schedules
and are not extra independent discoveries. A first observer insertion had an
indentation error and executed no native case. Initial in-process upstream
regression testing detected two loaded SQLite libraries; the corrected tests
run the native probe in a fresh process without weakening the identity check.

API and source URLs, retrieval times, HTTP status and SHA-256 receipts are in
sources.json. No inaccessible paper was represented as reviewed. Website
integration, independent review, full publication checks and authorized release
remain separate tasks.
