From: dan Date: Wed, 6 May 2020 19:14:41 +0000 (+0000) Subject: Block on the WRITER lock when attempting to run recovery. X-Git-Tag: version-3.32.0~38^2~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=861fb1e9dc039f00ff4997254b956352f37d71ac;p=thirdparty%2Fsqlite.git Block on the WRITER lock when attempting to run recovery. FossilOrigin-Name: 105d6c9bbcadc64faa2b24e315cb13227b17cfc6bf1b3512713f80ce56976a3d --- diff --git a/manifest b/manifest index 755c31ef40..df00f221f3 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\stests\sfor\srunning\srecovery\swhen\sopening\sa\swrite\stransaction\sor\sperforming\sa\scheckpoint\swith\sblocking\slocks\senabled.\sFix\ssome\sfailing\sassert()\sstatements. -D 2020-05-06T17:18:57.298 +C Block\son\sthe\sWRITER\slock\swhen\sattempting\sto\srun\srecovery. +D 2020-05-06T19:14:41.236 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -474,7 +474,7 @@ F src/auth.c a3d5bfdba83d25abed1013a8c7a5f204e2e29b0c25242a56bc02bb0c07bf1e06 F src/backup.c 5e617c087f1c2d6005c2ec694ce80d6e16bc68d906e1b1c556d7c7c2228b636b F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c e810b07f2212f36c1f317b94bd9af818be4a0aec24e935c11c39470dcd77c149 +F src/btree.c 4d5a7d2208dd8d921049780c9f06201cd6a656822c4f0931807b54f3dcd79097 F src/btree.h 989ef3c33413549e3e148f3dcb46c030f317dac130dc86809ba6b9aa4b16c72a F src/btreeInt.h 887cdd2ea7f4a65143074a8a7c8928b0546f8c18dda3c06a408ce7992cbab0c0 F src/build.c ec6c0bda1e43ef55e5f5121a77ba19fac51fc6585f95ce2da795bcedcf6e8f36 @@ -497,7 +497,7 @@ F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c 8e4211d04eb460c0694d486c6ba1c068d468c6f653c3f237869a802ad82854de F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 421310045bd78afefb772294a99e50f37d87ae578786a6169074e6291e30d969 -F src/main.c 5ae818ae552d3f68d1e67707a200d557125cdfb4f9e14e5cf1b4f12181974934 +F src/main.c 685d391fc37ad2c1162ad6784cae4a454dcd3632bbab319464af733d71eca72f F src/malloc.c a3e13b001f988ecec6bdb90c0ea8912c8c786e623724d7098da623d8d01d19b1 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 F src/mem1.c c12a42539b1ba105e3707d0e628ad70e611040d8f5e38cf942cee30c867083de @@ -520,8 +520,8 @@ F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 F src/os_unix.c 0e1acfbb8a75f2a4acede6078b072238df63981b172a9cd16f8b090d68291c15 F src/os_win.c 035a813cbd17f355bdcad7ab894af214a9c13a1db8aeac902365350b98cd45a7 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a -F src/pager.c be4d6d58df5c5d02b84547fd253a80f035ea2ee53931a5393d96af0ba3f04edb -F src/pager.h 5e2c0e5bd32aa4ae6c25eda2722490bb3f671f24d2e5d665d33df13285c1d520 +F src/pager.c 96436cb1920074d4ade120a1a8a9d0ae3f52df06651e21b7eccc5eae2f02b111 +F src/pager.h 8d1dc9a2c3fc5eb6eeed75f48a076f425e77706f8935f05817fa05a308f587b5 F src/parse.y c8eff38606f443d5ba245263fa7abc05e4116d95656e050c4b78e9bfbf931add F src/pcache.c 385ff064bca69789d199a98e2169445dc16e4291fa807babd61d4890c3b34177 F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586 @@ -617,8 +617,8 @@ F src/vdbetrace.c fa3bf238002f0bbbdfb66cc8afb0cea284ff9f148d6439bc1f6f2b4c3b7143 F src/vdbevtab.c 8094dfc28dad82d60a1c832020a1b201a5381dc185c14638affc6d4e9d54c653 F src/vtab.c 7b704a90515a239c6cdba6a66b1bb3a385e62326cceb5ecb05ec7a091d6b8515 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 9cf3f288cd4e48b49725e03037b60246a154ca8775892c75c319db8f5652ae8a -F src/wal.h 484890eaaefb89c0f2043ae34c20bf367383f201f63da631ecae424797a7f60d +F src/wal.c d23410a4330d11b8c699e0e463f334f2fca6c5af2c144e34674ccaef5b73f8fc +F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a F src/walker.c 7c429c694abd12413a5c17aec9f47cfe9eba6807e6b0a32df883e8e3a14835ed F src/where.c 9546c82056e8cdb27291f98cf1adca5d271240b399bb97b32f77fc2bea6146c9 F src/whereInt.h 6b874aa15f94e43a2cec1080be64d955b04deeafeac90ffb5d6975c0d511be3c @@ -1864,7 +1864,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P a3727dba10b476f414f0a049b35f2a5c169a408eccefa6833821560684e07f8b -R e1d7b36a881e7c026e918bb1e2d9268d +P d096ea3fe20f8af90f9e3f798fd0a69527b32ac8e208ade58b9d0af0e831f41b +R 452bcfebade640ff986656d7117060f0 U dan -Z 92e7a8a964d43aa5533a6dd765b70a70 +Z 21f20f1df91440f33362da9da7770293 diff --git a/manifest.uuid b/manifest.uuid index 78c3a449bb..1ed2372a34 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d096ea3fe20f8af90f9e3f798fd0a69527b32ac8e208ade58b9d0af0e831f41b \ No newline at end of file +105d6c9bbcadc64faa2b24e315cb13227b17cfc6bf1b3512713f80ce56976a3d \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index e80add6eb4..47b1136fd7 100644 --- a/src/btree.c +++ b/src/btree.c @@ -3442,9 +3442,10 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ #ifdef SQLITE_ENABLE_SETLK_TIMEOUT /* If transitioning from no transaction directly to a write transaction, ** block for the WRITER lock first if possible. */ + sqlite3PagerWalDb(pPager, p->db); if( pBt->pPage1==0 && wrflag ){ assert( pBt->inTransaction==TRANS_NONE ); - rc = sqlite3PagerWalWriteLock(p->db, pPager, 1); + rc = sqlite3PagerWalWriteLock(pPager, 1); if( rc!=SQLITE_OK ) break; } #endif @@ -3475,9 +3476,10 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ } if( rc!=SQLITE_OK ){ - sqlite3PagerWalWriteLock(p->db, pPager, 0); + sqlite3PagerWalWriteLock(pPager, 0); unlockBtreeIfUnused(pBt); } + sqlite3PagerWalDb(pPager, 0); }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && btreeInvokeBusyHandler(pBt) ); diff --git a/src/main.c b/src/main.c index e96f5e1133..0bc8edfec0 100644 --- a/src/main.c +++ b/src/main.c @@ -4481,11 +4481,11 @@ int sqlite3_snapshot_open( rc = SQLITE_OK; } if( rc==SQLITE_OK ){ - rc = sqlite3PagerSnapshotOpen(pPager, db, pSnapshot); + rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot); } if( rc==SQLITE_OK ){ rc = sqlite3BtreeBeginTrans(pBt, 0, 0); - sqlite3PagerSnapshotOpen(pPager, 0, 0); + sqlite3PagerSnapshotOpen(pPager, 0); } if( bUnlock ){ sqlite3PagerSnapshotUnlock(pPager); diff --git a/src/pager.c b/src/pager.c index 1570b3e704..a826f256bf 100644 --- a/src/pager.c +++ b/src/pager.c @@ -7581,13 +7581,23 @@ int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ ** with the same db and bLock parameters as were passed to this function. ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. */ -int sqlite3PagerWalWriteLock(sqlite3 *db, Pager *pPager, int bLock){ +int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){ int rc = SQLITE_OK; if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){ - rc = sqlite3WalWriteLock(db, pPager->pWal, bLock); + rc = sqlite3WalWriteLock(pPager->pWal, bLock); } return rc; } + +/* +** Set the database handle used by the wal layer to determine if +** blocking locks are required. +*/ +void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){ + if( pagerUseWal(pPager) ){ + sqlite3WalDb(pPager->pWal, db); + } +} #endif #ifdef SQLITE_ENABLE_SNAPSHOT @@ -7610,12 +7620,11 @@ int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ */ int sqlite3PagerSnapshotOpen( Pager *pPager, - sqlite3 *db, sqlite3_snapshot *pSnapshot ){ int rc = SQLITE_OK; if( pPager->pWal ){ - sqlite3WalSnapshotOpen(pPager->pWal, db, pSnapshot); + sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); }else{ rc = SQLITE_ERROR; } diff --git a/src/pager.h b/src/pager.h index c203908c83..62a371ef89 100644 --- a/src/pager.h +++ b/src/pager.h @@ -178,7 +178,7 @@ int sqlite3PagerSharedLock(Pager *pPager); int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); # ifdef SQLITE_ENABLE_SNAPSHOT int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot); - int sqlite3PagerSnapshotOpen(Pager*, sqlite3*, sqlite3_snapshot *pSnapshot); + int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot); int sqlite3PagerSnapshotRecover(Pager *pPager); int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); void sqlite3PagerSnapshotUnlock(Pager *pPager); @@ -186,9 +186,11 @@ int sqlite3PagerSharedLock(Pager *pPager); #endif #if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT) - int sqlite3PagerWalWriteLock(sqlite3*, Pager*, int); + int sqlite3PagerWalWriteLock(Pager*, int); + void sqlite3PagerWalDb(Pager*, sqlite3*); #else -# define sqlite3PagerWalWriteLock(x,y,z) SQLITE_OK +# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK +# define sqlite3PagerWalDb(x,y) #endif #ifdef SQLITE_DIRECT_OVERFLOW_READ diff --git a/src/wal.c b/src/wal.c index 7abff64078..41a11d6548 100644 --- a/src/wal.c +++ b/src/wal.c @@ -466,9 +466,9 @@ struct Wal { #endif #ifdef SQLITE_ENABLE_SNAPSHOT WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ -# ifdef SQLITE_ENABLE_SETLK_TIMEOUT - sqlite3 *dbSnapshot; -# endif +#endif +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + sqlite3 *db; #endif }; @@ -2118,6 +2118,88 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){ */ #define WAL_RETRY (-1) +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT +/* +** Attempt to enable blocking locks. Blocking locks are enabled only if (a) +** they are supported by the VFS, and (b) the database handle is configured +** with a busy-timeout. Return 1 if blocking locks are successfully enabled, +** or 0 otherwise. +*/ +static int walEnableBlocking(Wal *pWal){ + int res = 0; + if( pWal->db ){ + int tmout = pWal->db->busyTimeout; + if( tmout ){ + int rc; + rc = sqlite3OsFileControl( + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout + ); + res = (rc==SQLITE_OK); + } + } + return res; +} + +/* +** Disable blocking locks. +*/ +static void walDisableBlocking(Wal *pWal){ + int tmout = 0; + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); +} + +/* +** If parameter bLock is true, attempt to enable blocking locks, take +** the WRITER lock, and then disable blocking locks. If blocking locks +** cannot be enabled, no attempt to obtain the WRITER lock is made. Return +** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not +** an error if blocking locks can not be enabled. +** +** If the bLock parameter is false and the WRITER lock is held, release it. +*/ +int sqlite3WalWriteLock(Wal *pWal, int bLock){ + int rc = SQLITE_OK; + assert( pWal->readLock<0 || bLock==0 ); + if( bLock ){ + assert( pWal->db ); + if( walEnableBlocking(pWal) ){ + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); + if( rc==SQLITE_OK ){ + pWal->writeLock = 1; + } + walDisableBlocking(pWal); + } + }else if( pWal->writeLock ){ + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + pWal->writeLock = 0; + } + return rc; +} + +/* +** Set the database handle used to determine if blocking locks are required. +*/ +void sqlite3WalDb(Wal *pWal, sqlite3 *db){ + pWal->db = db; +} + +/* +** Take an exclusive WRITE lock. Blocking if so configured. +*/ +static int walLockWriter(Wal *pWal){ + int rc; + walEnableBlocking(pWal); + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); + walDisableBlocking(pWal); + return rc; +} +#else +# define walEnableBlocking(x) 0 +# define walDisableBlocking(x) +# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1) +# define sqlite3WalDb(pWal) +#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ + /* ** Read the wal-index header from the wal-index and into pWal->hdr. ** If the wal-header appears to be corrupt, try to reconstruct the @@ -2183,9 +2265,7 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ } }else{ int bWriteLock = pWal->writeLock; - if( bWriteLock - || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) - ){ + if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){ pWal->writeLock = 1; if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ badHdr = walIndexTryHdr(pWal, pChanged); @@ -2737,65 +2817,6 @@ int sqlite3WalSnapshotRecover(Wal *pWal){ } #endif /* SQLITE_ENABLE_SNAPSHOT */ -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT -/* -** Attempt to enable blocking locks. Blocking locks are enabled only if (a) -** they are supported by the VFS, and (b) the database handle is configured -** with a busy-timeout. Return 1 if blocking locks are successfully enabled, -** or 0 otherwise. -*/ -static int walEnableBlocking(sqlite3 *db, Wal *pWal){ - int res = 0; - if( db->busyTimeout ){ - int rc; - int tmout = db->busyTimeout; - rc = sqlite3OsFileControl( - pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout - ); - res = (rc==SQLITE_OK); - } - return res; -} - -/* -** Disable blocking locks. -*/ -static void walDisableBlocking(Wal *pWal){ - int tmout = 0; - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); -} - -/* -** If parameter bLock is true, attempt to enable blocking locks, take -** the WRITER lock, and then disable blocking locks. If blocking locks -** cannot be enabled, no attempt to obtain the WRITER lock is made. Return -** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not -** an error if blocking locks can not be enabled. -** -** If the bLock parameter is false and the WRITER lock is held, release it. -*/ -int sqlite3WalWriteLock(sqlite3 *db, Wal *pWal, int bLock){ - int rc = SQLITE_OK; - assert( pWal->readLock<0 || bLock==0 ); - if( bLock ){ - if( walEnableBlocking(db, pWal) ){ - rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); - if( rc==SQLITE_OK ){ - pWal->writeLock = 1; - } - walDisableBlocking(pWal); - } - }else if( pWal->writeLock ){ - walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); - pWal->writeLock = 0; - } - return rc; -} -#else -# define walEnableBlocking(x,y) 0 -# define walDisableBlocking(x) -#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ - /* ** Begin a read transaction on the database. ** @@ -2832,7 +2853,7 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ ** its intent. To avoid the race condition this leads to, ensure that ** there is no checkpointer process by taking a shared CKPT lock ** before checking pInfo->nBackfillAttempted. */ - walEnableBlocking(pWal->dbSnapshot, pWal); + walEnableBlocking(pWal); rc = walLockShared(pWal, WAL_CKPT_LOCK); walDisableBlocking(pWal); @@ -3666,7 +3687,8 @@ int sqlite3WalCheckpoint( /* Enable blocking locks, if possible. If blocking locks are successfully ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */ - if( walEnableBlocking(db, pWal) ){ + sqlite3WalDb(pWal, db); + if( walEnableBlocking(pWal) ){ xBusy2 = 0; } @@ -3710,7 +3732,7 @@ int sqlite3WalCheckpoint( if( rc==SQLITE_OK ){ walDisableBlocking(pWal); rc = walIndexReadHdr(pWal, &isChanged); - walEnableBlocking(db, pWal); + walEnableBlocking(pWal); if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ sqlite3OsUnfetch(pWal->pDbFd, 0, 0); } @@ -3743,6 +3765,7 @@ int sqlite3WalCheckpoint( } walDisableBlocking(pWal); + sqlite3WalDb(pWal, 0); /* Release the locks. */ sqlite3WalEndWriteTransaction(pWal); @@ -3869,13 +3892,9 @@ int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ */ void sqlite3WalSnapshotOpen( Wal *pWal, - sqlite3 *db, sqlite3_snapshot *pSnapshot ){ pWal->pSnapshot = (WalIndexHdr*)pSnapshot; -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT - pWal->dbSnapshot = db; -#endif } /* diff --git a/src/wal.h b/src/wal.h index 99ebc937af..02e2bab360 100644 --- a/src/wal.h +++ b/src/wal.h @@ -130,7 +130,7 @@ int sqlite3WalHeapMemory(Wal *pWal); #ifdef SQLITE_ENABLE_SNAPSHOT int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); -void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3*, sqlite3_snapshot *pSnapshot); +void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); int sqlite3WalSnapshotRecover(Wal *pWal); int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot); void sqlite3WalSnapshotUnlock(Wal *pWal); @@ -147,7 +147,8 @@ int sqlite3WalFramesize(Wal *pWal); sqlite3_file *sqlite3WalFile(Wal *pWal); #ifdef SQLITE_ENABLE_SETLK_TIMEOUT -int sqlite3WalWriteLock(sqlite3 *db, Wal *pWal, int bLock); +int sqlite3WalWriteLock(Wal *pWal, int bLock); +void sqlite3WalDb(Wal *pWal, sqlite3 *db); #endif #endif /* ifndef SQLITE_OMIT_WAL */