Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 100 additions & 22 deletions apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
private let helloBudget: SQLiteAgentHelloBudget
private var busyTimeoutMilliseconds: Int32 = 0

// Every reply goes to the connection's one pending operation, and an await lets the next call in, so statements queue.
private var statementRunning = false
private var waitingStatements: [CheckedContinuation<Void, Never>] = []

nonisolated let canceller: SQLiteCanceller
nonisolated var resolvedServerVersion: String? { connection.cachedServerVersion }

Expand All @@ -32,9 +36,10 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
port: Int,
path: String,
token: String,
helloBudget: SQLiteAgentHelloBudget
helloBudget: SQLiteAgentHelloBudget,
heartbeatInterval: TimeInterval = SQLiteAgentConnection.defaultHeartbeatInterval
) {
let connection = SQLiteAgentConnection(host: host, port: port)
let connection = SQLiteAgentConnection(host: host, port: port, heartbeatInterval: heartbeatInterval)
self.connection = connection
self.path = path
self.token = token
Expand Down Expand Up @@ -98,6 +103,8 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
rules: SQLiteStatementRules,
rowCap: Int
) async throws -> SQLiteRawResult {
await takeTurn()
defer { endTurn() }
let startTime = Date()
let accumulator = SQLiteAgentResultAccumulator()

Expand Down Expand Up @@ -141,8 +148,13 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
rules: SQLiteStatementRules,
continuation: AsyncThrowingStream<PluginStreamElement, Error>.Continuation
) async throws {
await takeTurn()
defer { endTurn() }
try Task.checkCancellation()
_ = try await withTaskCancellationHandler {
try await connection.execute(
// A cancel frame sent before the statement starts does nothing, so a reader that left by now ends it here.
try Task.checkCancellation()
return try await connection.execute(
sql: query,
parameters: [],
scope: Self.agentScope(rules),
Expand All @@ -162,7 +174,8 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
batch.append(values[index..<(index + columnCount)].map(Self.decode))
index += columnCount
}
continuation.yield(.rows(batch))
// Blocks the reader thread, so the channel and the agent wait too; heartbeats go out on their own timer.
_ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation)
}
)
} onCancel: {
Expand All @@ -171,6 +184,22 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend {
continuation.finish()
}

private func takeTurn() async {
guard statementRunning else {
statementRunning = true
return
}
await withCheckedContinuation { waitingStatements.append($0) }
}

private func endTurn() {
guard !waitingStatements.isEmpty else {
statementRunning = false
return
}
waitingStatements.removeFirst().resume()
}

// Nil runs the plain execute, so an unrestricted statement sends the frame it always sent. The agent loads no
// extension, so a Safe Mode read loses nothing under its read-only scope.
static func agentScope(_ rules: SQLiteStatementRules) -> SQLiteAgentStatementScope? {
Expand Down Expand Up @@ -263,16 +292,18 @@ struct SQLiteAgentExecuteOutcome: Sendable {
/// the reader thread's cancel and heartbeat frames never interleave with a partial request.
final class SQLiteAgentConnection: @unchecked Sendable {
private static let logger = Logger(subsystem: "com.TablePro", category: "SQLiteAgentConnection")
private static let heartbeatInterval: TimeInterval = 15
static let defaultHeartbeatInterval: TimeInterval = 15

private let host: String
private let port: Int
private let heartbeatInterval: TimeInterval

private let stateLock = NSLock()
private var socketFD: Int32 = -1
private var pending: SQLiteAgentOperation?
private var terminalFailure: Error?
private var closed = false
private var readerOwnsDescriptor = false

private let writeLock = NSLock()
private var reader = SQLiteAgentFrameReader()
Expand All @@ -286,14 +317,18 @@ final class SQLiteAgentConnection: @unchecked Sendable {
set { versionLock.lock(); _cachedServerVersion = newValue; versionLock.unlock() }
}

init(host: String, port: Int) {
init(host: String, port: Int, heartbeatInterval: TimeInterval) {
self.host = host
self.port = port
self.heartbeatInterval = heartbeatInterval
}

func connect(token: String) throws {
let fd = socket(AF_INET, SOCK_STREAM, 0)
guard fd >= 0 else { throw SQLitePluginError.connectionFailed("could not create a local socket") }
// A heartbeat after the session went away gets EPIPE; the default SIGPIPE would end the app.
var noSignal: Int32 = 1
setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &noSignal, socklen_t(MemoryLayout<Int32>.size))

var addr = sockaddr_in()
addr.sin_family = sa_family_t(AF_INET)
Expand All @@ -313,11 +348,31 @@ final class SQLiteAgentConnection: @unchecked Sendable {
stateLock.lock()
socketFD = fd
closed = false
readerOwnsDescriptor = false
stateLock.unlock()

try writeAll(SQLiteAgentProtocol.admissionPreamble(token: token))
do {
try writeAll(SQLiteAgentProtocol.admissionPreamble(token: token))
} catch {
close()
throw error
}

stateLock.lock()
guard !closed else {
stateLock.unlock()
throw SQLitePluginError.notConnected
}
readerOwnsDescriptor = true
stateLock.unlock()

let thread = Thread { [weak self] in self?.readLoop() }
let thread = Thread { [weak self] in
guard let self else {
Darwin.close(fd)
return
}
self.readLoop(fd: fd)
}
thread.name = "com.TablePro.sqlite.agent-reader"
thread.stackSize = 512 * 1_024
readerThread = thread
Expand Down Expand Up @@ -374,19 +429,29 @@ final class SQLiteAgentConnection: @unchecked Sendable {
closed = true
let fd = socketFD
socketFD = -1
let readerOwnsDescriptor = self.readerOwnsDescriptor
// Under the lock, so a reader on its way out cannot close the number before this reaches it.
if fd >= 0 { shutdown(fd, SHUT_RDWR) }
let inflight = pending
pending = nil
let timer = heartbeatTimer
heartbeatTimer = nil
stateLock.unlock()

heartbeatTimer?.cancel()
heartbeatTimer = nil
if fd >= 0 {
shutdown(fd, SHUT_RDWR)
Darwin.close(fd)
timer?.cancel()
if fd >= 0, !readerOwnsDescriptor {
closeDescriptor(fd)
}
inflight?.fail(SQLitePluginError.notConnected)
}

// After any writer that read the number before it was cleared, so none writes to a reused descriptor.
private func closeDescriptor(_ fd: Int32) {
writeLock.lock()
Darwin.close(fd)
writeLock.unlock()
}

// MARK: - Operation lifecycle

private func withOperation<T: Sendable>(
Expand All @@ -408,21 +473,28 @@ final class SQLiteAgentConnection: @unchecked Sendable {
}

stateLock.lock()
if closed {
stateLock.unlock()
continuation.resume(throwing: SQLitePluginError.notConnected)
return
}
/// A terminal reply that arrived before this operation registered, such as the launcher's
/// "no python3" notice landing before the hello op was set, is delivered to this
/// operation rather than lost. The reader thread stores it under the same lock, so the
/// two orderings, store-then-register and register-then-route, both reach here correctly.
/// It comes before the closed check, since the reader closes the connection once it stops.
if let stored = terminalFailure {
terminalFailure = nil
stateLock.unlock()
operation.fail(stored)
return
}
if closed {
stateLock.unlock()
continuation.resume(throwing: SQLitePluginError.notConnected)
return
}
// The backend runs one statement at a time; replacing the pending one would hand it another's replies.
guard pending == nil else {
stateLock.unlock()
operation.fail(SQLitePluginError.queryFailed("another statement is still running on the remote SQLite session"))
return
}
pending = operation
stateLock.unlock()

Expand Down Expand Up @@ -474,14 +546,14 @@ final class SQLiteAgentConnection: @unchecked Sendable {

// MARK: - Reader

private func readLoop() {
// The reader closes its own descriptor on the way out, so it never reads a number something else reused.
private func readLoop(fd: Int32) {
var buffer = [UInt8](repeating: 0, count: 32 * 1_024)
while true {
stateLock.lock()
let fd = socketFD
let isClosed = closed
stateLock.unlock()
guard !isClosed, fd >= 0 else { break }
guard !isClosed else { break }

let count = read(fd, &buffer, buffer.count)
if count <= 0 { break }
Expand All @@ -497,6 +569,9 @@ final class SQLiteAgentConnection: @unchecked Sendable {
}
}
completeOrStoreFailure(SQLitePluginError.notConnected)
// Nothing reads the socket from here on, so a later request would wait for a reply forever.
close()
closeDescriptor(fd)
}

private func route(_ reply: SQLiteAgentReply) {
Expand Down Expand Up @@ -530,8 +605,11 @@ final class SQLiteAgentConnection: @unchecked Sendable {
// MARK: - Heartbeat

private func startHeartbeat() {
stateLock.lock()
defer { stateLock.unlock() }
guard !closed, heartbeatTimer == nil else { return }
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer.schedule(deadline: .now() + Self.heartbeatInterval, repeating: Self.heartbeatInterval)
timer.schedule(deadline: .now() + heartbeatInterval, repeating: heartbeatInterval)
timer.setEventHandler { @Sendable [weak self] in
self?.sendHeartbeat()
}
Expand Down
5 changes: 3 additions & 2 deletions apple/Plugins/SQLiteDriverPlugin/SQLiteAgentProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ struct SQLiteAgentFrameReader {
private var readIndex = 0
private var expectsLauncherNotice = true

// A read rarely ends on a frame boundary, so waiting for an empty buffer kept every byte of a long result.
mutating func append(_ data: Data) {
if readIndex > 0, readIndex == buffer.count {
buffer.removeAll(keepingCapacity: true)
if readIndex > 0, readIndex >= buffer.count - readIndex {
buffer.removeSubrange(0..<readIndex)
readIndex = 0
}
buffer.append(contentsOf: data)
Expand Down
15 changes: 7 additions & 8 deletions apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable {
sqliteQuoteIdentifier(name)
}

init(config: DriverConnectionConfig) {
convenience init(config: DriverConnectionConfig) {
self.init(config: config, backend: Self.makeBackend(config: config))
}

init(config: DriverConnectionConfig, backend: any SQLiteExecutionBackend) {
self.config = config
self.backend = Self.makeBackend(config: config)
self.backend = backend
}

/// A file-backed connection runs on the app's own SQLite; one whose transport marks it a remote
Expand Down Expand Up @@ -712,14 +716,9 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable {
stream(query, rules: SQLiteStatementRules(context))
}

// Over the SSH agent the stream stays unbounded: a reader waiting for its consumer stops the tunnel relaying the
// heartbeats, and the agent's idle watchdog ends the session.
private func stream(_ query: String, rules: SQLiteStatementRules) -> AsyncThrowingStream<PluginStreamElement, Error> {
let queryToRun = String(query)
let policy: AsyncThrowingStream<PluginStreamElement, Error>.Continuation.BufferingPolicy = backend is SQLiteLocalBackend
? .bufferingOldest(PluginRowStream.boundedBufferBatches)
: .unbounded
return AsyncThrowingStream(bufferingPolicy: policy) { continuation in
return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches)) { continuation in
let streamTask = Task {
do {
try await self.backend.streamQuery(queryToRun, rules: rules, continuation: continuation)
Expand Down
Loading
Loading