In sqlite3_table_column_metadata(), hold the mutex on all attached BtShared objects while accessing schema objects. Fix for #3679. (CVS 6328)

FossilOrigin-Name: d197afd658eecfc0e24913e5a779c8f1e2a138a6
diff --git a/manifest b/manifest
index 26dc9b3..09f44c5 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Move\sthe\snew\sgenfkey\sshell\scommand\sout\sfrom\swithin\s#ifdef\s_WIN32_\s(CVS\s6327)
-D 2009-02-25T19:07:25
+C In\ssqlite3_table_column_metadata(),\shold\sthe\smutex\son\sall\sattached\sBtShared\sobjects\swhile\saccessing\sschema\sobjects.\sFix\sfor\s#3679.\s(CVS\s6328)
+D 2009-02-26T07:15:59
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in d64baddbf55cdf33ff030e14da837324711a4ef7
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -122,7 +122,7 @@
 F src/journal.c e00df0c0da8413ab6e1bb7d7cab5665d4a9000d0
 F src/legacy.c 8b3b95d48d202614946d7ce7256e7ba898905c3b
 F src/loadext.c 3f96631089fc4f3871a67f02f2e4fc7ea4d51edc
-F src/main.c f7f7c2fcbda632bfb7995c77636660a019a172c7
+F src/main.c fd98b89bcc80d27e303c913c8facab9ee939ef15
 F src/malloc.c 072ddad9b7e908e9b9a2a494d4758dadeced61ae
 F src/mem0.c f2f84062d1f35814d6535c9f9e33de3bfb3b132c
 F src/mem1.c 3bfb39e4f60b0179713a7c087b2d4f0dc205735f
@@ -493,7 +493,7 @@
 F test/printf.test 47e9e5bbec8509023479d54ceb71c9d05a95308a
 F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 x
 F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc
-F test/quick.test 4a09b89a44be46b3ee5a5dbe25b72cb1e5c3ead8
+F test/quick.test b7acd5b3df583391979d9f9edf98aa85fc95a3f6
 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6
 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459
 F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a
@@ -556,6 +556,7 @@
 F test/thread001.test 06c45ed9597d478e7bbdc2a8937e1ebea2a20a32
 F test/thread002.test 3c03900f03fd2fe8e2fbb1bbdef7fa8206fdb7ad
 F test/thread003.test 6d360c15afe7f6ef6186801d2cb8407bccbe3aa3
+F test/thread004.test 9d8ea6a9b0d62d35ad0b967e010d723ed99f614a
 F test/thread1.test 862dd006d189e8b0946935db17399dcac2f8ef91
 F test/thread2.test 91f105374f18a66e73a3254c28fe7c77af69bdea
 F test/thread_common.tcl 047f80288b5e1e86bed181097d67e640f1a54a74
@@ -701,7 +702,7 @@
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 36d699660bc328e65170d72be715338e82dbbb50
-R 7f42e226ab2b746ac50fedc5f267f111
-U drh
-Z 46bbb2602043845ae8e84b264be6c003
+P 48ee0e47e2d9669cc7425104e6b04ce49caf2e56
+R 42441caf74da644bc7eebc8b71116801
+U danielk1977
+Z 25037733e5d25d45bee1e03b1015374a
diff --git a/manifest.uuid b/manifest.uuid
index 4f7ea80..fd74eb2 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-48ee0e47e2d9669cc7425104e6b04ce49caf2e56
\ No newline at end of file
+d197afd658eecfc0e24913e5a779c8f1e2a138a6
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 9b4578a..e4f92e8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.529 2009/02/23 14:42:53 danielk1977 Exp $
+** $Id: main.c,v 1.530 2009/02/26 07:15:59 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -1933,7 +1933,6 @@
   (void)sqlite3SafetyOn(db);
   sqlite3BtreeEnterAll(db);
   rc = sqlite3Init(db, &zErrMsg);
-  sqlite3BtreeLeaveAll(db);
   if( SQLITE_OK!=rc ){
     goto error_out;
   }
@@ -1989,6 +1988,7 @@
   }
 
 error_out:
+  sqlite3BtreeLeaveAll(db);
   (void)sqlite3SafetyOff(db);
 
   /* Whether the function call succeeded or failed, set the output parameters
diff --git a/test/quick.test b/test/quick.test
index c27f1d2..4d8dca6 100644
--- a/test/quick.test
+++ b/test/quick.test
@@ -6,7 +6,7 @@
 #***********************************************************************
 # This file runs all tests.
 #
-# $Id: quick.test,v 1.92 2009/02/03 16:51:25 danielk1977 Exp $
+# $Id: quick.test,v 1.93 2009/02/26 07:15:59 danielk1977 Exp $
 
 proc lshift {lvar} {
   upvar $lvar l
@@ -85,6 +85,7 @@
   thread001.test
   thread002.test
   thread003.test
+  thread004.test
   trans2.test
   vacuum3.test
 
diff --git a/test/thread004.test b/test/thread004.test
new file mode 100644
index 0000000..5931e4b
--- /dev/null
+++ b/test/thread004.test
@@ -0,0 +1,80 @@
+# 2009 February 26
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# $Id: thread004.test,v 1.1 2009/02/26 07:15:59 danielk1977 Exp $
+
+set testdir [file dirname $argv0]
+
+source $testdir/tester.tcl
+ifcapable !mutex||!shared_cache {
+  return
+}
+source $testdir/thread_common.tcl
+if {[info commands sqlthread] eq "" 
+ || [info commands sqlite3_table_column_metadata] eq ""
+} {
+  return
+}
+
+# Use shared-cache mode for this test.
+# 
+db close
+set ::enable_shared_cache [sqlite3_enable_shared_cache]
+sqlite3_enable_shared_cache 1
+
+# Create a table in database test.db
+#
+sqlite3 db test.db
+do_test thread004-1.1 {
+  execsql { CREATE TABLE t1(a, b, c) }
+} {}
+
+do_test thread004-1.2 {
+
+  set ThreadOne {
+    set iStart [clock_seconds]
+    while {[clock_seconds]<$iStart+20} {
+      set ::DB [sqlite3_open test.db]
+      sqlite3_close $::DB
+    }
+  }
+  set ThreadTwo {
+    set ::DB [sqlite3_open test.db]
+    set iStart [clock_seconds]
+    set nErr 0
+    while {[clock_seconds] <$iStart+20} {
+      incr nErr [catch {sqlite3_table_column_metadata $::DB main t1 a}]
+    }
+    sqlite3_close $::DB
+    set nErr
+  }
+  
+  # Run two threads. The first thread opens and closes database test.db
+  # repeatedly. Each time this happens, the in-memory schema used by
+  # all connections to test.db is discarded.
+  #
+  # The second thread calls sqlite3_table_column_metadata() over and
+  # over again. Each time it is called, the database schema is loaded
+  # if it is not already in memory. At one point this was crashing.
+  #
+  unset -nocomplain finished
+  thread_spawn finished(1) $thread_procs $ThreadOne
+  thread_spawn finished(2) $thread_procs $ThreadTwo
+  
+  foreach t {1 2} {
+    if {![info exists finished($t)]} { vwait finished($t) }
+  }
+
+  set finished(2)
+} {0}
+sqlite3_enable_shared_cache $::enable_shared_cache
+finish_test
+