Skip to content

Commit c9bfb54

Browse files
authored
Merge pull request #22 from rqlite/fix-malformed-db
Fix in-memory database locking
2 parents 7116c96 + 4dde487 commit c9bfb54

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

sqlite3-binding.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50141,15 +50141,28 @@ static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
5014150141
** Lock an memdb-file.
5014250142
*/
5014350143
static int memdbLock(sqlite3_file *pFile, int eLock){
50144-
MemFile *pThis = (MemFile*)pFile;
50144+
MemFile *pThis = (MemFile*)pFile;
5014550145
MemStore *p = pThis->pStore;
5014650146
int rc = SQLITE_OK;
5014750147
if( eLock==pThis->eLock ) return SQLITE_OK;
5014850148
memdbEnter(p);
5014950149
if( eLock>SQLITE_LOCK_SHARED ){
50150+
assert( pThis->eLock>=SQLITE_LOCK_SHARED );
5015050151
if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){
5015150152
rc = SQLITE_READONLY;
50153+
}else if( eLock==SQLITE_LOCK_EXCLUSIVE ){
50154+
/* Taking an EXCLUSIVE lock. Fail if we only have SHARED and any
50155+
** other client has any kind of write-lock. Also fail if any other
50156+
** client is holding read-lock. */
50157+
if( pThis->eLock<=SQLITE_LOCK_SHARED && p->nWrLock ){
50158+
rc = SQLITE_BUSY;
50159+
}else if( p->nRdLock>1 ){
50160+
rc = SQLITE_BUSY;
50161+
}
50162+
p->nWrLock = 1;
5015250163
}else if( pThis->eLock<=SQLITE_LOCK_SHARED ){
50164+
/* Upgrading to RESERVED or PENDING from SHARED. Fail if any other
50165+
** client has a write-lock of any kind. */
5015350166
if( p->nWrLock ){
5015450167
rc = SQLITE_BUSY;
5015550168
}else{

0 commit comments

Comments
 (0)