Skip to content

Commit 26ba2d9

Browse files
committed
properly lock schema in case when we work with attached DB
1 parent badbbe9 commit 26ba2d9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

libsql-sqlite3/src/vectorIndex.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ int vectorIndexSearch(
981981
int *nWrites,
982982
char **pzErrMsg
983983
) {
984-
int type, dims, k, rc;
984+
int type, dims, k, rc, iDb = -1;
985985
double kDouble;
986986
const char *zIdxFullName;
987987
char *zIdxDbSNameAlloc = NULL; // allocated managed schema name string - must be freed if not null
@@ -1059,6 +1059,18 @@ int vectorIndexSearch(
10591059
} else{
10601060
zIdxDbSName = zIdxDbSNameAlloc;
10611061
zIdxName = zIdxNameAlloc;
1062+
iDb = sqlite3FindDbName(db, zIdxDbSName);
1063+
if( iDb < 0 ){
1064+
*pzErrMsg = sqlite3_mprintf("vector index(search): unknown schema '%s'", zIdxDbSName);
1065+
rc = SQLITE_ERROR;
1066+
goto out;
1067+
}
1068+
// we need to hold mutex to protect schema against unwanted changes
1069+
// this code is necessary, otherwise sqlite3SchemaMutexHeld assert will fail
1070+
if( iDb !=1 ){
1071+
// not "main" DB which we already hold mutex for
1072+
sqlite3BtreeEnter(db->aDb[iDb].pBt);
1073+
}
10621074
}
10631075

10641076
if( vectorIndexGetParameters(db, zIdxDbSName, zIdxName, &idxParams) != 0 ){
@@ -1094,6 +1106,9 @@ int vectorIndexSearch(
10941106
}
10951107
sqlite3DbFree(db, zIdxNameAlloc);
10961108
sqlite3DbFree(db, zIdxDbSNameAlloc);
1109+
if( iDb >= 0 && iDb != 1 ){
1110+
sqlite3BtreeLeave(db->aDb[iDb].pBt);
1111+
}
10971112
return rc;
10981113
}
10991114

0 commit comments

Comments
 (0)