Repository navigation
Description
snapshot_sum_anomaly.py
Hello. I confirmed the bug on Windows by running given Python script.
Below are the results of analysis performed by AI.
Summary
In 4.0.8, 5.0.4, 5.0.5.1973 and 6.0.0.2471 (Super, SuperClassic and Classic) a SNAPSHOT READ ONLY transaction can return an aggregate that matches no committed state of the table while another attachment keeps updating the same rows. In the script below the writer keeps SUM(val) at 2000, and a reader gets e.g. 1970 or 2130 from a single SELECT. The same transaction returns 2000 after the writer has stopped and gfix -v -full finds no errors, so the wrong value is a transient error in the record read, not stored damage. Not seen in 3.0.14. The anomaly is timing-dependent (2-144 s to the first wrong result in the runs below).
Code reading points to VIO_chase_record_version(): after VIO_data() has released the data page, the reader keeps the address of the next back version and fetches it with DPM_fetch(). Cooperative intermediate GC of another attachment may in the meantime have rebuilt the version chain and freed that slot, and the slot may already hold a back version of the other row. DPM_fetch() accepts the occupied slot and VIO_data() applies the delta of the other row to the image of the first row (see Analysis).
This was found while verifying PR #9303 (#9301) and is not the problem of #9301: the code involved is not changed by that PR, the wrong results occur on the official snapshot 5.0.5.1973 without the PR and also on a build of the PR head (2f0e5e8), and in the script below readers start their transactions only while no writer transaction is open.
Reproduction
snapshot_sum_anomaly.py (attached, firebird-driver only):
python snapshot_sum_anomaly.py --dsn localhost:<dir>/snapshot_sum_anomaly.fdb --client <path to fbclient.dll or libfbclient.so> --user SYSDBA --password masterkey
Needs Python 3 with pip install firebird-driver and a Firebird client library of the same bitness as Python (--client can be omitted only if the driver finds the library itself). <dir> must be an existing directory on the server (otherwise the script ends with a driver traceback and no RESULT line); if the server does not listen on port 3050 use localhost/<port>:<dir>/.... The database (page size 16384) is (re)created by the script. Defaults: --seconds 120 --readers 4 --reads-per-transaction 20. The script uses 5 processes (it is CPU-heavy) and stops at the first wrong result; the last line is RESULT: BUG ... or RESULT: OK .... The time to the first wrong result varies a lot, so run it several times.
Scenario (timing-dependent, so there is no deterministic step table):
create table t (id integer not null primary key, val integer not null);
insert into t values (1, 1000);
insert into t values (2, 1000);
commit;| Attachment | Repeated actions |
|---|---|
| Writer (1 process, SNAPSHOT) | update t set val = val - 1 where id = 1; update t set val = val + 1 where id = 2; commit; in a loop |
| Reader (4 processes) | set transaction read only snapshot; then up to 20 times select count(*), sum(val), min(rdb$record_version), max(rdb$record_version) from t;, then commit; and a new transaction |
The writer holds a lock for the length of each of its transactions and a reader takes the same lock only to start its transaction, so a reader never starts while a writer transaction is open (the situation of #9301). Without this lock (--no-start-gate) wrong sums also appear with 1 reader or 1 read per transaction, and on builds without the fix of #9301 also missing rows (seen on 5.0.5.1973 with 1 reader).
Expected: every select returns 2, 2000. Actual (5.0.5.1973, Classic, default firebird.conf except port, IpcName and ServerMode):
ENGINE_VERSION=5.0.5 ServerMode=Classic GCPolicy=cooperative PageSize=16384 no_gc=False start_gate=True
WRONG RESULT: reader2 transaction 15737 snapshot number 15731 read 10 of the transaction: count, sum, min/max record version = (2, 1970, 15735, 15735)
same transaction after the writer stopped: rows = [(1, -11401, 15735), (2, 13401, 15735)] aggregates = [(2, 2000, 15735, 15735), (2, 2000, 15735, 15735), (2, 2000, 15735, 15735)]
RESULT: BUG 1 wrong result(s) (66610 reads, 12405 writer commits, 35 s)
Both rows have the same record version in the wrong result (the writer updates them in one transaction), so min/max(rdb$record_version) does not show the problem.
Results
Linux x64 (Ubuntu 24.04, 2 vCPU), official packages and snapshots, runs of 180 s one after another. Each cell: runs with a wrong result / all runs (seconds to the first wrong result). "OK" means no wrong result within the time limit, not that it is impossible. The instances had ReadConsistency = 0, DataTypeCompatibility = 3.0 and OuterJoinConversion = false added to firebird.conf.
| Version | Super | SuperClassic | Classic |
|---|---|---|---|
| 3.0.14.33856 | OK (3 runs) | OK (2 runs) | OK (3 runs) |
| 4.0.8.3342-a02a946 | BUG 1/3 (2 s) | BUG 2/2 (131 s, 8 s) | BUG 3/3 (41 s, 7 s, 92 s) |
| 5.0.4.1812 | BUG 1/3 (103 s) | BUG 1/2 (117 s) | BUG 2/3 (144 s, 17 s) |
| 5.0.5.1973-3a24e7d | BUG 2/3 (90 s, 8 s) | BUG 1/5 (76 s) * | BUG 2/3 (119 s, 73 s) |
| 6.0.0.2471-2db58d2 | BUG 1/6 (23 s) * | BUG 1/2 (49 s) | BUG 3/3 (81 s, 78 s, 134 s) |
* Includes 3 more runs of 120 s each, made on separate instances. The single wrong result of 5.0.5.1973 SuperClassic is the weakest cell.
Same script on 5.0.5.1973 Classic, 3 runs each: default firebird.conf BUG 3/3 (35 s, 6 s, 63 s); --no-gc (DPB flag NO_GARBAGE_COLLECT on all attachments) OK; --readers 1 --reads-per-transaction 3 OK; --reads-per-transaction 1 (4 readers) OK.
Not tested: Windows, page sizes other than 16384, GCPolicy other than the default of the mode, embedded, read committed readers.
Analysis
Code at 3a24e7d (v5.0-release); the same code is in 4.0.7, master (6bc8aac) and the PR #9303 head (that PR changes only tra.cpp). 3.0.14 has the reader code but no VIO_intermediate_gc().
- vio.cpp#L1501-L1514: branch of
VIO_chase_record_version()for a not visible chained delta back version. It copies the header and back pointer totemp, callsVIO_data(), sets the next address fromtemp.rpb_b_page/rpb_b_lineand callsDPM_fetch(). Only an empty slot makes it start again from the primary version (the neighbouring non-chained branch re-checksrpb_transaction_nrafter the fetch). - vio.cpp#L1860:
VIO_data()releases the page before the delta is applied, so nothing protects the saved address afterwards. - vio.cpp#L2557 (
VIO_intermediate_gc): rewrites all kept back versions into a new chain, repoints the primary version and deletes the old chain. It runs in the updating attachment afterVIO_modify()when cooperative GC is enabled (from code reading: Classic and SuperClassic ignoreGCPolicyand use cooperative GC, Super uses it with the defaultcombined). - dpm.epp#L755-L758, dpm.epp#L3189, dpm.epp#L1136, dpm.epp#L3274:
DPM_delete()clears the slot andfind_space()hands out the lowest free slot to the next stored version;DPM_fetch()keeps the caller's record number andget_header()rejects only an empty or out-of-range slot. A record header holds no record number, so an occupied slot of another row looks valid, andDifference::apply()(sqz.cpp#L519) checks only bounds.
A trace from a build of the PR head with passive logging added to the engine (Classic, same scenario, not attached) shows the sequence in one wrong result (reader snapshot 7580, row 1 expected -3800, row 2 5800): the reader keeps the address 192:12 as the next back version of row 1; GC then repoints row 2 to a new chain that starts in the slot 192:12; the reader fetches 192:12 and applies the delta of row 2 (transaction 7593, f8 01 ae fd) to the image of row 1; the deltas from 192:12, 192:10 and 192:8 give row 1 VAL -3666, -3668 and -3672 instead of -3800, row 2 is read correctly (5800) and the sum is 2128. The delta changes only the lowest byte of VAL (0xa8 instead of 0x28, i.e. +128), which also explains why count and min/max(rdb$record_version) stay unchanged. The commit numbers of the writer transactions show that the version taken was correctly visible for the snapshot, and re-applying the logged deltas to the logged base images gives exactly the logged wrong images.
Not established: the interleaving was not forced with a debugger, so the other runs above are assumed (not proven) to have the same cause. No fix was tried.
Activity
hvlad commentedon Nov 17, 2026
So far I can't reproduce it
tomaszdubiel18 commentedon Nov 17, 2026
How about not specifying the protocol? Just host/port:dir.
I tested this in that way.
hvlad commentedon Nov 17, 2026
Tested with inet too, of course. xnet faster, inet slower, not reproduced. Same with SC.
tomaszdubiel18 commentedon Nov 17, 2026
I reproduced this now on the latest snapshot:
ENGINE_VERSION=5.0.5 ServerMode=Super GCPolicy=combined PageSize=16384 no_gc=False start_gate=True WRONG RESULT: reader4 transaction 39178 snapshot number 39172 read 16 of the transaction: count, sum, min/max record version = (2, 2014, 39177, 39177) same transaction after the writer stopped: rows = [(1, -23327, 39177), (2, 25327, 39177)] aggregates = [(2, 2000, 39177, 39177), (2, 2000, 39177, 39177), (2, 2000, 39177, 39177)] RESULT: BUG 1 wrong result(s) (296956 reads, 24333 writer commits, 87 s)tomaszdubiel18 commentedon Nov 17, 2026
Two further attempts with no reproduction on SuperClassic, one on Super and then second try on Super and the bug was reproduced:
ENGINE_VERSION=5.0.5 ServerMode=Super GCPolicy=combined PageSize=16384 no_gc=False start_gate=True WRONG RESULT: reader2 transaction 50510 snapshot number 50503 read 19 of the transaction: count, sum, min/max record version = (2, 1946, 50509, 50509) same transaction after the writer stopped: rows = [(1, -35709, 50509), (2, 37709, 50509)] aggregates = [(2, 2000, 50509, 50509), (2, 2000, 50509, 50509), (2, 2000, 50509, 50509)] RESULT: BUG 1 wrong result(s) (275959 reads, 36719 writer commits, 116 s)tomaszdubiel18 commentedon Nov 18, 2026
Hello. Please try with this script:
snapshot_sum_anomaly.py
I reproduced the bug in 5 out of 5 attempts in 3-4 seconds.
hvlad commentedon Nov 24, 2026
It is still not reproducible to me. I need to add artificial sleep(0) in race path (after VIO_data() at line 1503 ) to reproduce it.