Grinnz / Mojo-SQLite

Mojo::SQLite - A tiny Mojolicious wrapper for SQLite
https://metacpan.org/pod/Mojo::SQLite
Other
27 stars 12 forks source link

Fix race condition about "UNIQUE constraint failed" - alternative 1 #25

Closed okurz closed 2 years ago

okurz commented 2 years ago

We observed a problem where two services are accessing the same SQLite database and one of the processes would run into an error "DBD::SQLite::st execute failed: UNIQUE constraint failed: mojo_migrations.name". The problem is simple that the method "_active" already foresees to only check the version and only initializating the table within a transaction but the wrong parameter was passed :)

Verified within the scope of https://progress.opensuse.org/issues/108125 using the so called worker cacherservice for "openQA" with the following command for manual verification:

for i in {1..10000}; do echo "## Run $i" && \
systemctl stop openqa-worker-cacheservice-minion.service openqa-worker-cacheservice.service && \
rm -f /var/lib/openqa/cache/cache.sqlite* && \
systemctl start openqa-worker-cacheservice-minion.service openqa-worker-cacheservice.service && \
for j in {1..30}; do echo "waiting for sqlite" && \
test -f /var/lib/openqa/cache/cache.sqlite && break || sleep 1; done && \
journalctl --since=today _SYSTEMD_UNIT=openqa-worker-cacheservice.service + _SYSTEMD_UNIT=openqa-worker-cacheservice-minion.service | grep -c 'UNIQUE';
done

Reference: https://progress.opensuse.org/issues/108125

okurz commented 2 years ago

Alternative to https://github.com/Grinnz/Mojo-SQLite/pull/24

Grinnz commented 2 years ago

Thank you!

kraih commented 2 years ago

Fyi. Mojo::Pg went for a slightly different solution, because just replacing 1 with 0 did not have the desired effect there. https://github.com/mojolicious/mojo-pg/commit/8b6e9b0828a548d4f8a837840495512d012c4cbe