Index: src/wal.c ================================================================== --- src/wal.c +++ src/wal.c @@ -1745,11 +1745,11 @@ ** and can be safely ignored. */ rc = walLockShared(pWal, WAL_READ_LOCK(0)); sqlite3OsShmBarrier(pWal->pDbFd); if( rc==SQLITE_OK ){ - if( memcmp(pHdr, &pWal->hdr, sizeof(WalIndexHdr)) ){ + if( memcmp((void *)pHdr, &pWal->hdr, sizeof(WalIndexHdr)) ){ /* It is not safe to allow the reader to continue here if frames ** may have been appended to the log before READ_LOCK(0) was obtained. ** When holding READ_LOCK(0), the reader ignores the entire log file, ** which implies that the database file contains a trustworthy ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from @@ -1830,19 +1830,19 @@ ** copied into the database by a checkpointer. If either of these things ** happened, then reading the database with the current value of ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry ** instead. ** - ** This does not guarantee that the copy wal-index header is up to - ** date before proceeding. This would not be possible without somehow - ** blocking writers. It only guarantees that a damaging checkpoint or + ** This does not guarantee that the copy of the wal-index header is up to + ** date before proceeding. That would not be possible without somehow + ** blocking writers. It only guarantees that a dangerous checkpoint or ** log-wrap (either of which would require an exclusive lock on ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid. */ sqlite3OsShmBarrier(pWal->pDbFd); if( pInfo->aReadMark[mxI]!=mxReadMark - || memcmp(pHdr, &pWal->hdr, sizeof(WalIndexHdr)) + || memcmp((void *)pHdr, &pWal->hdr, sizeof(WalIndexHdr)) ){ walUnlockShared(pWal, WAL_READ_LOCK(mxI)); return WAL_RETRY; }else{ pWal->readLock = mxI; Index: test/wal3.test ================================================================== --- test/wal3.test +++ test/wal3.test @@ -418,12 +418,12 @@ T delete #------------------------------------------------------------------------- # When opening a read-transaction on a database, if the entire log has # already been copied to the database file, the reader grabs a special -# kind of read lock (on aReadMark[0]). This test case tests the outcome -# of the following: +# kind of read lock (on aReadMark[0]). This set of test cases tests the +# outcome of the following: # # + The reader discovering that between the time when it determined # that the log had been completely backfilled and the lock is obtained # that a writer has written to the log. In this case the reader should # acquire a different read-lock (not aReadMark[0]) and read the new @@ -561,8 +561,79 @@ db2 close db close T delete +#------------------------------------------------------------------------- +# When opening a read-transaction on a database, if the entire log has +# not yet been copied to the database file, the reader grabs a read +# lock on aReadMark[x], where x>0. The following test cases experiment +# with the outcome of the following: +# +# + The reader discovering that between the time when it read the +# wal-index header and the lock was obtained that a writer has +# written to the log. In this case the reader should re-read the +# wal-index header and lock a snapshot corresponding to the new +# header. +# +# + The value in the aReadMark[x] slot has been modified since it was +# read. +# +catch {db close} +testvfs T -default 1 +do_test wal3-7.1.1 { + file delete -force test.db test.db-journal test.db wal + sqlite3 db test.db + execsql { + PRAGMA journal_mode = WAL; + CREATE TABLE blue(red PRIMARY KEY, green); + } +} {wal} + +T script method_callback +T filter xOpen +proc method_callback {method args} { + if {$method == "xOpen"} { return "reader" } +} +do_test wal3-7.1.2 { + sqlite3 db2 test.db + execsql { SELECT * FROM blue } db2 +} {} + +T filter xShmLock +set ::locks [list] +proc method_callback {method file handle spec} { + if {$handle != "reader" } { return } + if {$method == "xShmLock"} { + catch { execsql { INSERT INTO blue VALUES(1, 2) } } + catch { execsql { INSERT INTO blue VALUES(3, 4) } } + } + lappend ::locks $spec +} +do_test wal3-7.1.3 { + execsql { SELECT * FROM blue } db2 +} {1 2 3 4} +do_test wal3-7.1.4 { + set ::locks +} {{4 1 lock shared} {4 1 unlock shared} {5 1 lock shared} {5 1 unlock shared}} + +set ::locks [list] +proc method_callback {method file handle spec} { + if {$handle != "reader" } { return } + if {$method == "xShmLock"} { + catch { execsql { INSERT INTO blue VALUES(5, 6) } } + } + lappend ::locks $spec +} +do_test wal3-7.2.1 { + execsql { SELECT * FROM blue } db2 +} {1 2 3 4 5 6} +do_test wal3-7.2.2 { + set ::locks +} {{5 1 lock shared} {5 1 unlock shared} {4 1 lock shared} {4 1 unlock shared}} + +db close +db2 close +T delete finish_test