Fossil SCM

fossil-scm / tools / powerloss / README-POWERLOSS.md
1
# Power loss and killed-process tests for SQLite programs
2
3
Dan Shearer, November 2026
4
5
`powerloss.so` is an `LD_PRELOAD` library for Linux and BSD that stops a
6
program wherever its SQLite database files could reach the disk, and leaves the
7
files as a power loss or a killed process would. You can use it with any
8
program that links SQLite. In the example the driver is `powerloss.sh`, and
9
we're asking the question *what happens to Fossil repo and checkout databases
10
when bad things happen during `fossil commit`?* There are different cases for
11
WAL and DELETE mode repo dbs. `test/powerloss.test` in Fossil's test suite uses
12
the same library to interrupt other Fossil commands, such as merge, update,
13
push and clone.
14
15
## The point in power loss testing
16
17
One transaction committed across two SQLite database files can leave the
18
checkout database and the repo database out of step if the transaction is
19
interrupted, although both files pass an integrity check. The cause is that
20
SQLite can only do an atomic commit across both files via a *super-journal*,
21
and SQLite only makes a super-journal for databases in a rollback journal mode.
22
If one of the databases is in WAL, MEMORY or OFF journal mode, or at
23
`synchronous=OFF`, SQLite commits each file in turn (that is, not atomically).
24
The [SQLite ATTACH documentation](https://sqlite.org/lang_attach.html) says:
25
26
> If the main database is ":memory:" or if the journal_mode is WAL, then
27
> transactions continue to be atomic within each individual database file. But
28
> if the host computer crashes in the middle of a COMMIT where two or more
29
> database files are updated, some of those files might get the changes where
30
> others might not.
31
32
The Fossil data model commits across multiple databases, and there are good
33
reasons why a user might switch the repository database to [WAL
34
mode](https://sqlite.org/wal.html). This test tool was written to verify that
35
multi-database commits are safe.
36
37
A single database in WAL mode can lose a committed transaction too. The [PRAGMA
38
documentation](https://sqlite.org/pragma.html#pragma_synchronous) says:
39
40
> A transaction committed in WAL mode with synchronous=NORMAL might roll back
41
> following a power loss or system crash.
42
43
Whether a program survives an interruption depends on its journal modes, its
44
`synchronous` settings and the order in which it commits its databases. This
45
tool stops the program at every point where the outcome could differ, and
46
records the state of the databases after each stop.
47
48
These problems are less likely to occur when using LMDB as the SQLite backing
49
store via [LumoSQL](https://lumosql.org/src/lumosql), because LMDB syncs new
50
pages before writing the meta page that refers to them, so a power loss does
51
not lose a committed transaction from one database. A commit across two
52
databases still needs this test to check that failures are graceful.
53
54
## How it works
55
56
There are two ways to interrupt a program. When the process is killed, the
57
operating system retains everything the process wrote. When the operating
58
system crashes or the power fails, the disk has only what the process synced.
59
60
* **Power loss** (the default): the stop points are at every `fsync()`,
61
`fdatasync()` and `unlink()` of a database or journal file. The library saves
62
a copy of each database file when the first process of the run starts and
63
whenever a process syncs it. At the stop point it restores the saved copies,
64
deletes the files that were never synced and every `-shm` file, and exits
65
with status 77.
66
* **Killed process** (`STOP_MODE=kill`): each `pwrite()` or `write()` to those
67
files is also a stop point, and so is each `fwrite()` to one that was opened
68
with `fopen()`. The C library's own write from inside `fwrite()` cannot be
69
intercepted, so without this a file written through stdio, such as the
70
repository that `fossil clone` copies from a file, would have no stop points.
71
At the stop point the library exits with status 77 and leaves every file as
72
it is. The other processes continue, and see the stopped process disappear,
73
as a client sees a server that is killed during a request.
74
75
A transfer between two repositories writes them in two processes. A `fossil
76
push` over a `file:` URL runs `fossil http` to write the remote repository,
77
while `fossil server` writes its repository in the answering process. A power
78
loss stops every process on the machine at the same moment, and a killed
79
process stops only itself.
80
81
Every process that loads the library during a run counts its stop points in the
82
same sequence, including the programs that a program runs with `system()`. A
83
process locks the library's state file around each write, sync and unlink of a
84
tracked file, so a restore does not overlap another process's write, and a
85
process that locks the state file after a power loss exits with status 77.
86
87
The library reads its settings from the environment:
88
89
* `STOP_DIR`: a working directory. The library tracks files under
90
`STOP_DIR/run`, saves copies in `STOP_DIR/shadow` and appends one line per
91
stop point to `STOP_DIR/log`, with the number, the call, the file and the
92
process ID. A file's copy has its path under `STOP_DIR/run`, with each `/`
93
replaced by `%`, so the `.fslckout` files of two checkouts have separate
94
copies. `STOP_DIR/shadow` also has the stop-point count, so a driver deletes
95
it before each run.
96
* `STOP_AT`: the stop point to stop at, counting from 1. `0` runs the program
97
to the end and only records the stop points.
98
* `STOP_MODE`: `kill` for the killed-process mode.
99
100
A driver runs the program once with `STOP_AT=0` to count the stop points, then
101
once for each one from a fresh copy of the files, and examines the files after
102
each run. It should also check that each run stopped where it was told to,
103
because a run that does not stop leaves the files of a finished command, which
104
pass every other check. `powerloss.sh` requires that the run logged its stop
105
point, that in power mode it logged nothing after it, and that the exit status
106
isn't 0. `pl_stop_seen` in `test/powerloss.test` is stricter: for a command of
107
one process it also requires the same call on the same file as in the counting
108
run, and exit status 77.
109
110
## Running the Fossil check-in test
111
112
`powerloss.sh` uses `fossil` to create a repository and a check-out with an
113
edit, an add, a remove and a rename pending. It then runs the check-in once for
114
each stop point, and stops it there, using the command in `PL_COMMIT`, which
115
defaults to `fossil commit -m two`. After each run the script reads the
116
check-in that `.fslckout` says was just made and the repo database's latest
117
check-in, and runs `PRAGMA integrity_check` on both files. The third argument
118
sets the repo to WAL or DELETE mode.
119
120
From the root of the Fossil source tree, with the `fossil` you want to test:
121
122
```sh
123
cc -shared -fPIC -o tools/powerloss/powerloss.so tools/powerloss/powerloss.c
124
sh tools/powerloss/powerloss.sh "$(command -v fossil)" /tmp/pl wal
125
```
126
127
Give the `fossil` binary as an absolute path, because the script runs it from
128
other directories. A statically linked `fossil` doesn't load the library, so
129
the script finds no stop points.
130
131
Set `STOP_MODE=kill` in the environment of `powerloss.sh` for the
132
killed-process test.
133
134
Output from Fossil 2.29 with a WAL repository:
135
136
```text
137
repository journal mode: wal
138
check-out journal mode: delete
139
commit without a stop: exit 0, 13 stop points
140
1 unlink /r.fossil-wal checkout=5 repo-latest=5 integrity=ok/ok consistent
141
2 fsync /ck/.fslckout-journal checkout=5 repo-latest=5 integrity=ok/ok consistent
142
3 fsync /ck/.fslckout-journal checkout=5 repo-latest=5 integrity=ok/ok consistent
143
4 fsync /ck/.fslckout checkout=5 repo-latest=5 integrity=ok/ok consistent
144
5 unlink /ck/.fslckout-journal checkout=5 repo-latest=5 integrity=ok/ok consistent
145
6 fsync /ck/.fslckout-journal checkout=5 repo-latest=5 integrity=ok/ok consistent
146
7 fsync /ck/.fslckout-journal checkout=5 repo-latest=5 integrity=ok/ok consistent
147
8 fsync /ck/.fslckout checkout=5 repo-latest=5 integrity=ok/ok consistent
148
9 fsync /r.fossil-wal checkout=5 repo-latest=5 integrity=ok/ok consistent
149
10 unlink /ck/.fslckout-journal checkout=8 repo-latest=5 integrity=ok/ok AHEAD
150
11 fsync /r.fossil-wal checkout=8 repo-latest=8 integrity=ok/ok consistent
151
12 fsync /r.fossil checkout=8 repo-latest=8 integrity=ok/ok consistent
152
13 unlink /r.fossil-wal checkout=8 repo-latest=8 integrity=ok/ok consistent
153
```
154
155
Each line gives the verdict:
156
157
* **consistent:** the checkout db records the repository's latest check-in.
158
* **BEHIND:** the repository has a later check-in than the checkout db records.
159
`fossil status` works and still lists the changes as pending, and `fossil
160
commit` stops with "Would fork". `fossil update` moves the checkout to the
161
new check-in, warning of merge conflicts on the way, and after it nothing is
162
pending.
163
* **AHEAD:** the checkout db records a check-in that the repository does not
164
have. `fossil status`, `changes`, `commit`, `update` and `ls` all fail with
165
"Local check-out database is out of sync with repository file".
166
167
At point 10 above, the checkout db has committed, and the repository's commit
168
is in the WAL but SQLite has not synced it. Fossil builds SQLite with
169
`-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1` (see `tools/makemake.tcl`), so a WAL
170
repository runs at `synchronous=NORMAL`, where SQLite syncs the WAL at a
171
checkpoint and not at each commit. The checkout db is in DELETE mode at
172
`synchronous=FULL`, so its commit is synced. A power loss there loses the
173
repository's commit and leaves the checkout db ahead of the repository. `fossil
174
close` and `fossil open --force` fail there with the same message, so the way
175
out is to delete `.fslckout` and run `fossil open --force --keep`. That keeps
176
your edits, and you redo the pending adds, removes and renames. With a DELETE
177
repository the same run has 14 stop points and is consistent at every one. In
178
kill mode a WAL repository has 122 stop points, with one BEHIND and no AHEAD.
179
180
## Testing SQLite programs other than Fossil
181
182
Only `is_db()` in `powerloss.c` and all of `powerloss.sh` are specific to
183
Fossil. The rest of the C program is generic:
184
185
* `is_db()` decides which files to track, matching names that contain `.fossil`
186
or `.fslckout`. Change it to match the program's database files.
187
* The library intercepts `open()`, `open64()`, `close()`, `fsync()`,
188
`fdatasync()`, `unlink()`, `pwrite()`, `pwrite64()`, `write()`, `fopen()`,
189
`fopen64()`, `fclose()` and `fwrite()`. Other programs and other operating
190
systems may write through other calls. Check your binary or library that
191
contains SQLite with `nm -D FILE | grep -E ' U
192
(f?open|f?close|f(data)?sync|unlink|[fp]?write|lseek|ftruncate|rename)'`.
193
* Write a driver for the program to replace `powerloss.sh`.
194
195
## Stuff it doesn't do
196
197
* At a power loss, every write since a file's last sync is lost. A real disk
198
may retain some unsynced writes, in any order, or a partly written page; the
199
library does not test those cases.
200
* The library treats an `unlink()` as reaching the disk at once and ignores
201
directory syncs.
202
* The library tracks file descriptors below 4096 and at most 64 stdio streams.
203
`MAXFD` and `MAXFILE` in `powerloss.c` set those limits.
204
* A process that does not load the library, such as one started with
205
`LD_PRELOAD` removed from its environment, is neither counted nor stopped.
206
* At a power loss, the other processes of the run aren't killed. Each one exits
207
with status 77 at its next write, sync or unlink of a tracked file, and can
208
write other files until then.
209
* The library tracks only the database files. The other files under
210
`STOP_DIR/run`, such as the files of a check-out, stay as the program wrote
211
them. A real power loss can lose what was written to a file that was never
212
synced, and Fossil doesn't sync the files it writes into a check-out.
213
* The library doesn't intercept `ftruncate()` or `rename()`. A truncate isn't a
214
stop point, and a tracked file that the program renames has no saved copy
215
under its new name, so a power loss deletes it.
216
* The library is for a single-threaded program such as Fossil. It locks its
217
state file with `fcntl()`, which doesn't stop two threads of one process from
218
writing at the same time.
219

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button