                 // Fetch and release the future
                 futureResult = Result { try block(db) }
                 futureSemaphore.signal()
-                
             }
         } catch {
             return Future { throw error }

            // Reset the schema cache before running user code in snapshot isolation
            db.schemaCache = SimpleDatabaseSchemaCache()
            block(db)
            
        }
        _ = semaphore.wait(timeout: .distantFuture)
        if let readError = readError {
            // TODO: write a test for this
            throw readError
        }
    }
    

        // isolation has been established:
        let semaphore = DispatchSemaphore(value: 0)
        
        var snapshotIsolationError: Error? = nil
        let (reader, releaseReader) = try readerPool.get()
        reader.async { db in
            defer {
                _ = try? db.commit() // Ignore commit error
                releaseReader()
            }
            do {
                try db.beginSnapshotIsolation()
            } catch {
                snapshotIsolationError = error
                semaphore.signal() // Release the writer queue and rethrow error
                return
            }
