Do not call the xDisconnect method on a virtual table while xUpdate is
pending.  Instead, defer the xDisconnect until after xUpdate completes. (CVS 3387)

FossilOrigin-Name: 61148f4c36255c4ed3552f888fa75252b300589d
diff --git a/manifest b/manifest
index 39a46df..a524a6a 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Test\sfor\sbusted\sTCL\sbuilds\sthat\sdo\snot\ssupport\s64-bit\sintegers\sand\sprint\na\swarning\smessage\sto\susers\sthat\stest\sfailures\smay\sbe\sa\sresult\sof\sthe\sbad\nTCL\sbuild\sand\snot\ssome\sproblem\swith\sSQLite.\s\sTicket\s#1953.\s(CVS\s3386)
-D 2006-09-02T14:50:24
+C Do\snot\scall\sthe\sxDisconnect\smethod\son\sa\svirtual\stable\swhile\sxUpdate\sis\npending.\s\sInstead,\sdefer\sthe\sxDisconnect\suntil\safter\sxUpdate\scompletes.\s(CVS\s3387)
+D 2006-09-02T20:57:52
 F Makefile.in 659b63368cfbb95a224c9d2f2a9897802d96a4ea
 F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -88,7 +88,7 @@
 F src/shell.c 3496a1b0a23a6b77f90231b9e2f521fd93dcb5b1
 F src/sqlite.h.in 84ac26ca94a84dd603fb57a27d862f51bfd9f687
 F src/sqlite3ext.h 11a046b3519c4b9b7709e6d6a95c3a36366f684a
-F src/sqliteInt.h 325a2d45be2b22c1e21ad649e0a898c74eaec7de
+F src/sqliteInt.h 259adce944cc3b28da1fa3df9beb9ba86017a45d
 F src/table.c d8817f43a6c6bf139487db161760b9e1e02da3f1
 F src/tclsqlite.c e029f739bed90071789fe81a226d53e97a80a4d8
 F src/test1.c cb3688f5d2094dc708a7959bd99aed411e096e49
@@ -112,14 +112,14 @@
 F src/utf.c 4459801e9b00cfd69993bfca58545d3775682d6e
 F src/util.c 5409031819ee4672c5f9c3ac7cf517e267a25845
 F src/vacuum.c 5b37d0f436f8e1ffacd17934e44720b38d2247f9
-F src/vdbe.c 24c2cc3b08223140b45d15d05644210ac4363573
+F src/vdbe.c a77869949ddd0afe01443611edb949e24e67c91c
 F src/vdbe.h 258b5d1c0aaa72192f09ff0568ce42b383f156fa
 F src/vdbeInt.h e3eaab262b67b84474625cfc38aec1125c32834b
 F src/vdbeapi.c 81f531d7dc5c898131b02ef85f6c6144ab2892cf
 F src/vdbeaux.c 9fab61427a0741c9c123e8ff16e349b1f90397be
 F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5
 F src/vdbemem.c 5f0afe3b92bb2c037f8d5d697f7c151fa50783a3
-F src/vtab.c 6067f9ec011880fadb04be1ca24dbe30c575a5bf
+F src/vtab.c 7fc0624c2bb6156c3d99e2ce706f2a5b54094e36
 F src/where.c 75a89957fcb8c068bec55caa4e9d2ed5fa0b0724
 F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
@@ -395,7 +395,7 @@
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P 098cbafcd6dcf57142b0417e796d27ffddcc0920
-R a457dec7946d047fe968e63a72fc157e
+P ca864ee913ea5ae88761e617bcac300ffa339369
+R a007e2ad3282d7bf3c46df0a1cb84279
 U drh
-Z f4d682ad54f7e32ce0b41ddbb807a8aa
+Z cb0935641e94f4c2fc376bd17950fce1
diff --git a/manifest.uuid b/manifest.uuid
index 47693a6..93b6aab 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-ca864ee913ea5ae88761e617bcac300ffa339369
\ No newline at end of file
+61148f4c36255c4ed3552f888fa75252b300589d
\ No newline at end of file
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index bf571ca..5826854 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.524 2006/08/23 20:07:22 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.525 2006/09/02 20:57:52 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1862,6 +1862,8 @@
    int sqlite3VtabRollback(sqlite3 *db);
    int sqlite3VtabCommit(sqlite3 *db);
 #endif
+void sqlite3VtabLock(sqlite3_vtab*);
+void sqlite3VtabUnlock(sqlite3_vtab*);
 void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
 void sqlite3VtabFinishParse(Parse*, Token*);
 void sqlite3VtabArgInit(Parse*);
diff --git a/src/vdbe.c b/src/vdbe.c
index 3df482b..e4540da 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.574 2006/08/15 14:21:16 drh Exp $
+** $Id: vdbe.c,v 1.575 2006/09/02 20:57:52 drh Exp $
 */
 #include "sqliteInt.h"
 #include "os.h"
@@ -4857,7 +4857,9 @@
       apArg[i] = pX;
     }
     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
+    sqlite3VtabLock(pVtab);
     rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
+    sqlite3VtabUnlock(pVtab);
     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
     if( pOp->p1 && rc==SQLITE_OK ){
       assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
diff --git a/src/vtab.c b/src/vtab.c
index be6b988..9207fc4 100644
--- a/src/vtab.c
+++ b/src/vtab.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to help implement virtual tables.
 **
-** $Id: vtab.c,v 1.30 2006/09/02 14:17:00 drh Exp $
+** $Id: vtab.c,v 1.31 2006/09/02 20:57:52 drh Exp $
 */
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 #include "sqliteInt.h"
@@ -41,6 +41,29 @@
 }
 
 /*
+** Lock the virtual table so that it cannot be disconnected.
+** Locks nest.  Every lock should have a corresponding unlock.
+** If an unlock is omitted, resources leaks will occur.  
+**
+** If a disconnect is attempted while a virtual table is locked,
+** the disconnect is deferred until all locks have been removed.
+*/
+void sqlite3VtabLock(sqlite3_vtab *pVtab){
+  pVtab->nRef++;
+}
+
+/*
+** Unlock a virtual table.  When the last lock is removed,
+** disconnect the virtual table.
+*/
+void sqlite3VtabUnlock(sqlite3_vtab *pVtab){
+  pVtab->nRef--;
+  if( pVtab->nRef==0 ){
+    pVtab->pModule->xDisconnect(pVtab);
+  }
+}
+
+/*
 ** Clear any and all virtual-table information from the Table record.
 ** This routine is called, for example, just before deleting the Table
 ** record.
@@ -49,10 +72,7 @@
   sqlite3_vtab *pVtab = p->pVtab;
   if( pVtab ){
     assert( p->pMod && p->pMod->pModule );
-    pVtab->nRef--;
-    if( pVtab->nRef==0 ){
-      pVtab->pModule->xDisconnect(pVtab);
-    }
+    sqlite3VtabUnlock(pVtab);
     p->pVtab = 0;
   }
   if( p->azModuleArg ){
@@ -359,7 +379,7 @@
 
   /* Add pVtab to the end of sqlite3.aVTrans */
   db->aVTrans[db->nVTrans++] = pVtab;
-  pVtab->nRef++;
+  sqlite3VtabLock(pVtab);
   return SQLITE_OK;
 }
 
@@ -492,10 +512,7 @@
     int (*x)(sqlite3_vtab *);
     x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
     if( x ) x(pVtab);
-    pVtab->nRef--;
-    if( pVtab->nRef==0 ){
-      pVtab->pModule->xDisconnect(pVtab);
-    }
+    sqlite3VtabUnlock(pVtab);
   }
   sqliteFree(db->aVTrans);
   db->nVTrans = 0;