walcrash2.test 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # 2010 May 25
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. #
  12. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. source $testdir/lock_common.tcl
  15. source $testdir/wal_common.tcl
  16. ifcapable !wal {finish_test ; return }
  17. #-------------------------------------------------------------------------
  18. # This test case demonstrates a flaw in the wal-index manipulation that
  19. # existed at one point: If a process crashes mid-transaction, it may have
  20. # already added some entries to one of the hash-tables in the wal-index.
  21. # If the transaction were to be explicitly rolled back at this point, the
  22. # hash-table entries would be removed as part of the rollback. However,
  23. # if the process crashes, the transaction is implicitly rolled back and
  24. # the rogue entries remain in the hash table.
  25. #
  26. # Normally, this causes no problem - readers can tell the difference
  27. # between committed and uncommitted entries in the hash table. However,
  28. # if it happens often enough that all slots in the hash-table become
  29. # non-zero, the next process that attempts to read or write the hash
  30. # table falls into an infinite loop.
  31. #
  32. # Even if run with an SQLite version affected by the bug, this test case
  33. # only goes into an infinite loop if SQLite is compiled without SQLITE_DEBUG
  34. # defined. If SQLITE_DEBUG is defined, the program is halted by a failing
  35. # assert() before entering the infinite loop.
  36. #
  37. # walcrash2-1.1: Create a database. Commit a transaction that adds 8 frames
  38. # to the WAL (and 8 entry to the first hash-table in the
  39. # wal-index).
  40. #
  41. # walcrash2-1.2: Have an external process open a transaction, add 8 entries
  42. # to the wal-index hash-table, then crash. Repeat this 1023
  43. # times (so that the wal-index contains 8192 entries - all
  44. # slots are non-zero).
  45. #
  46. # walcrash2-1.3: Using a new database connection, attempt to query the
  47. # database. This should cause the process to go into the
  48. # infinite loop.
  49. #
  50. do_test walcrash2-1.1 {
  51. execsql {
  52. PRAGMA page_size = 1024;
  53. PRAGMA auto_vacuum = off;
  54. PRAGMA journal_mode = WAL;
  55. PRAGMA synchronous = NORMAL;
  56. BEGIN;
  57. CREATE TABLE t1(x);
  58. CREATE TABLE t2(x);
  59. CREATE TABLE t3(x);
  60. CREATE TABLE t4(x);
  61. CREATE TABLE t5(x);
  62. CREATE TABLE t6(x);
  63. CREATE TABLE t7(x);
  64. COMMIT;
  65. }
  66. file size test.db-wal
  67. } [wal_file_size 8 1024]
  68. for {set nEntry 8} {$nEntry < 8192} {incr nEntry 8} {
  69. do_test walcrash2-1.2.[expr $nEntry/8] {
  70. set C [launch_testfixture]
  71. testfixture $C {
  72. sqlite3 db test.db
  73. db eval {
  74. PRAGMA cache_size = 15;
  75. BEGIN;
  76. INSERT INTO t1 VALUES(randomblob(900)); -- 1 row, 1 page
  77. INSERT INTO t1 SELECT * FROM t1; -- 2 rows, 3 pages
  78. INSERT INTO t1 SELECT * FROM t1; -- 4 rows, 5 pages
  79. INSERT INTO t1 SELECT * FROM t1; -- 8 rows, 9 pages
  80. INSERT INTO t1 SELECT * FROM t1; -- 16 rows, 17 pages
  81. INSERT INTO t1 SELECT * FROM t1 LIMIT 3; -- 20 rows, 20 pages
  82. }
  83. }
  84. close $C
  85. file size test.db-wal
  86. } [wal_file_size 16 1024]
  87. }
  88. do_test walcrash2-1.3 {
  89. sqlite3 db2 test.db
  90. execsql { SELECT count(*) FROM t1 } db2
  91. } {0}
  92. catch { db2 close }
  93. finish_test