From 39ea45b080e7517f5ecf91e3627070cd05bcf1ad Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 00:10:52 +0700 Subject: [PATCH 1/2] fix(ios): stop MySQL timeouts and Stop leaving statements running, and keep synced passwords --- .../TableProDatabase/ConnectionManager.swift | 22 +- .../Protocols/DriverFactory+SignIn.swift | 9 + .../Protocols/DriverFactory.swift | 9 + .../ConnectionManagerSignInTests.swift | 62 ++- .../CBridges/CMariaDB/CMariaDB.h | 12 + .../Coordinators/ConnectionCoordinator.swift | 38 +- .../TableProMobile/Drivers/MySQLDriver.swift | 241 ++++++++-- .../Drivers/MySQLStatementKiller.swift | 339 ++++++++++++++ .../Drivers/PostgreSQLConnectionString.swift | 2 +- .../Drivers/PostgreSQLDriver.swift | 4 + .../Drivers/SessionSocketKeepalive.swift | 32 ++ .../TableProMobile/Localizable.xcstrings | 9 + .../Platform/IOSDriverFactory.swift | 21 +- .../Platform/KeychainSecureStore.swift | 151 +++++-- .../Platform/SessionTransactionMonitor.swift | 37 +- .../Security/CertificateMaterialStore.swift | 38 +- ...ConnectionCoordinatorBackgroundTests.swift | 134 ++++++ .../Drivers/FakeMySQLServer.swift | 165 ++++++- .../Drivers/FakePostgreSQLServer.swift | 176 ++++++++ .../Drivers/MySQLKillSignInTests.swift | 124 ++++++ .../Drivers/MySQLKillTokenTests.swift | 78 ++++ .../Drivers/MySQLQueryTimeoutTests.swift | 92 ++++ .../Drivers/MySQLSessionSocketTests.swift | 53 +++ .../Drivers/MySQLStopTests.swift | 418 ++++++++++++++++++ .../PostgreSQLConnectionStringTests.swift | 10 + .../PostgreSQLSessionSocketTests.swift | 26 ++ .../Drivers/SessionSocketProbe.swift | 48 ++ .../Mocks/InMemoryKeychainItems.swift | 171 +++++++ .../Platform/KeychainSecureStoreTests.swift | 125 ++++++ .../SessionTransactionMonitorTests.swift | 33 ++ 30 files changed, 2556 insertions(+), 123 deletions(-) create mode 100644 apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift create mode 100644 apple/TableProMobile/TableProMobile/Drivers/SessionSocketKeepalive.swift create mode 100644 apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorBackgroundTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillSignInTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillTokenTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLQueryTimeoutTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLSessionSocketTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLStopTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLSessionSocketTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/SessionSocketProbe.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift index fd4cf096d7..317be54e12 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/ConnectionManager.swift @@ -99,7 +99,10 @@ public final class ConnectionManager: @unchecked Sendable { let driver = try driverFactory.createDriver( for: effectiveConnection, password: password, - preTunnelHost: preTunnelHost + preTunnelHost: preTunnelHost, + refreshPassword: Self.passwordRefresh( + for: connection, stored: stored, factory: driverFactory, secureStore: secureStore + ) ) try await driver.connect() @@ -407,6 +410,16 @@ public final class ConnectionManager: @unchecked Sendable { return catalogs[connectionId] === connection } + // Resolves for the connection as saved: behind a tunnel the driver's own connection names the loopback. + private static func passwordRefresh( + for saved: DatabaseConnection, + stored: String?, + factory: DriverFactory, + secureStore: SecureStore + ) -> @Sendable () async throws -> String? { + { try await factory.resolvePassword(for: saved, stored: stored, secureStore: secureStore) } + } + private static func openCatalogDriver( dial: SessionDial, database: String, @@ -420,7 +433,12 @@ public final class ConnectionManager: @unchecked Sendable { // An IAM token is minted for the saved host and expires, so each catalog connection gets its own. let stored = try secureStore.retrieve(forKey: passwordKey(for: dial.saved.id)) let password = try await factory.resolvePassword(for: dial.saved, stored: stored, secureStore: secureStore) - let driver = try factory.createDriver(for: target, password: password, preTunnelHost: dial.preTunnelHost) + let driver = try factory.createDriver( + for: target, + password: password, + preTunnelHost: dial.preTunnelHost, + refreshPassword: passwordRefresh(for: dial.saved, stored: stored, factory: factory, secureStore: secureStore) + ) try await driver.connect() do { try await factory.prepareCatalogDriver(driver, for: target) diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory+SignIn.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory+SignIn.swift index 0546d3e960..516dd94728 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory+SignIn.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory+SignIn.swift @@ -10,6 +10,15 @@ public extension DriverFactory { try createDriver(for: connection, password: password) } + func createDriver( + for connection: DatabaseConnection, + password: String?, + preTunnelHost: String?, + refreshPassword: (@Sendable () async throws -> String?)? + ) throws -> any DatabaseDriver { + try createDriver(for: connection, password: password, preTunnelHost: preTunnelHost) + } + func resolvePassword( for connection: DatabaseConnection, stored: String?, diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift index d31bd189e2..e2ef856f52 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/Protocols/DriverFactory.swift @@ -9,6 +9,15 @@ public protocol DriverFactory: Sendable { password: String?, preTunnelHost: String? ) throws -> any DatabaseDriver + // `refreshPassword` resolves the password again for the connection as saved, whose endpoint a tunnel hides from + // `connection`. A driver that signs in a second time later, such as to stop a statement, calls it once a minted + // token may have expired. + func createDriver( + for connection: DatabaseConnection, + password: String?, + preTunnelHost: String?, + refreshPassword: (@Sendable () async throws -> String?)? + ) throws -> any DatabaseDriver // Runs before the tunnel, on the connection as saved, so a sign-in failure comes before any SSH prompt. func resolvePassword( for connection: DatabaseConnection, diff --git a/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift b/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift index 3cd4e5309d..fe26cf08e1 100644 --- a/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProDatabaseTests/ConnectionManagerSignInTests.swift @@ -19,10 +19,12 @@ private struct DriverRequest: Sendable { let port: Int let password: String? let preTunnelHost: String? + let refreshPassword: (@Sendable () async throws -> String?)? } private final class SigningDriverFactory: DriverFactory, @unchecked Sendable { var mint: @Sendable (String?) async throws -> String? = { stored in stored } + var opensCatalogs = false private(set) var mintRequests: [MintRequest] = [] private(set) var driverRequests: [DriverRequest] = [] @@ -43,17 +45,29 @@ private final class SigningDriverFactory: DriverFactory, @unchecked Sendable { for connection: DatabaseConnection, password: String?, preTunnelHost: String? + ) throws -> any DatabaseDriver { + try createDriver(for: connection, password: password, preTunnelHost: preTunnelHost, refreshPassword: nil) + } + + func createDriver( + for connection: DatabaseConnection, + password: String?, + preTunnelHost: String?, + refreshPassword: (@Sendable () async throws -> String?)? ) throws -> any DatabaseDriver { driverRequests.append(DriverRequest( host: connection.host, port: connection.port, password: password, - preTunnelHost: preTunnelHost + preTunnelHost: preTunnelHost, + refreshPassword: refreshPassword )) return MockDatabaseDriver() } func supportedTypes() -> [DatabaseType] { [mockType] } + + func opensCatalogConnections(for connection: DatabaseConnection) -> Bool { opensCatalogs } } private final class PasswordRecordingFactory: DriverFactory, @unchecked Sendable { @@ -179,6 +193,52 @@ struct ConnectionManagerSignInTests { #expect(driver.preTunnelHost == connection.host) } + @Test("Through a tunnel, a driver signing in again later mints for the saved endpoint, not the tunnel's") + func tunneledRefreshMintsForTheSavedEndpoint() async throws { + let factory = SigningDriverFactory() + let manager = ConnectionManager( + driverFactory: factory, + secureStore: MockSecureStore(), + sshProvider: MockSSHProvider() + ) + let connection = rdsConnection(tunneled: true) + _ = try await manager.connect(connection) + let refresh = try #require(factory.driverRequests.first?.refreshPassword) + factory.mint = { _ in "fresh-token" } + + let token = try await refresh() + + let mint = try #require(factory.mintRequests.last) + #expect(token == "fresh-token") + #expect(factory.mintRequests.count == 2) + #expect(mint.host == connection.host) + #expect(mint.port == connection.port) + } + + @Test("A catalog connection behind a tunnel mints again for the saved endpoint too") + func tunneledCatalogRefreshMintsForTheSavedEndpoint() async throws { + let factory = SigningDriverFactory() + factory.opensCatalogs = true + let manager = ConnectionManager( + driverFactory: factory, + secureStore: MockSecureStore(), + sshProvider: MockSSHProvider() + ) + let connection = rdsConnection(tunneled: true) + _ = try await manager.connect(connection) + _ = try #require(await manager.catalogDriver(for: connection.id, database: "")) + let catalog = try #require(factory.driverRequests.last) + let refresh = try #require(catalog.refreshPassword) + + _ = try await refresh() + + let mint = try #require(factory.mintRequests.last) + #expect(factory.driverRequests.count == 2) + #expect(catalog.host == "127.0.0.1") + #expect(mint.host == connection.host) + #expect(mint.port == connection.port) + } + @Test("Without a tunnel, the driver dials the saved host and gets no preTunnelHost") func directConnectHasNoPreTunnelHost() async throws { let factory = SigningDriverFactory() diff --git a/apple/TableProMobile/TableProMobile/CBridges/CMariaDB/CMariaDB.h b/apple/TableProMobile/TableProMobile/CBridges/CMariaDB/CMariaDB.h index 87a9358fa2..efd5733b7d 100644 --- a/apple/TableProMobile/TableProMobile/CBridges/CMariaDB/CMariaDB.h +++ b/apple/TableProMobile/TableProMobile/CBridges/CMariaDB/CMariaDB.h @@ -8,6 +8,18 @@ #undef MARIADB_TLS #include +// MYSQL_OPT_READ_TIMEOUT is read once at connect, so a live session's socket timeout is set on its pvio. 0 is none. +static inline my_bool tablepro_mysql_set_io_timeout(MYSQL *mysql, unsigned int seconds) { + if (mysql == NULL || mysql->net.pvio == NULL) { + return 1; + } + mysql->options.read_timeout = seconds; + mysql->options.write_timeout = seconds; + my_bool read_result = ma_pvio_set_timeout(mysql->net.pvio, PVIO_READ_TIMEOUT, (int)seconds); + my_bool write_result = ma_pvio_set_timeout(mysql->net.pvio, PVIO_WRITE_TIMEOUT, (int)seconds); + return read_result || write_result; +} + // The server name a tunneled connection's certificate is checked against, kept as handle user data. static const char *const tablepro_mysql_server_name_key = "com.TablePro.tls.serverName"; diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift index 0e4f7ae3ce..8555d70ad1 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift @@ -360,6 +360,10 @@ final class ConnectionCoordinator { case duringWork // The transaction is gone either way, which is what was asked for. case duringRollBack + // The app closed a database file as it went to the background, which rolled the transaction back. + case inBackground + // The app closed the file once a running statement ended, and that statement may have ended the transaction. + case inBackgroundAfterStatement } // True when the redial dropped a held transaction and the outcome is already settled for the user. @@ -405,7 +409,10 @@ final class ConnectionCoordinator { } workWasRunning = workWasRunning || appState.sessionTransactions.isRunningStatements(connection.id) - let heldTransaction = transactionHold != nil + let ended = appState.sessionTransactions.endedHold(for: connection.id, on: held?.driver) + workWasRunning = workWasRunning || ended?.duringWork == true + let heldTransaction = transactionHold != nil || ended != nil + let released = ended.flatMap { releasedLoss($0, on: held, asked: loss) } isReconnecting = true defer { isReconnecting = false } do { @@ -419,12 +426,12 @@ final class ConnectionCoordinator { guard attemptToken == token else { return false } await takeSession(outcome.session, token: token, dialedHere: outcome.dialedHere) guard attemptToken == token else { return false } - let dropped = heldTransaction ? settledLoss(loss, workWasRunning: workWasRunning) : unsettledLoss + let dropped = heldTransaction ? released ?? settledLoss(loss, workWasRunning: workWasRunning) : unsettledLoss return reportDroppedTransaction(dropped) } catch { guard attemptToken == token else { return false } if heldTransaction { - unsettledLoss = settledLoss(loss, workWasRunning: workWasRunning) + unsettledLoss = released ?? settledLoss(loss, workWasRunning: workWasRunning) } phase = .lost(classify(error, operation: "reconnect")) self.session = nil @@ -436,6 +443,17 @@ final class ConnectionCoordinator { workWasRunning || statementFailedDuringCheck ? .duringWork : loss } + // The release closed the file once its running statement ended, so a later failure on the closed file decides nothing. + private func releasedLoss( + _ ended: SessionTransactionMonitor.EndedHold, + on held: ConnectionSession?, + asked loss: SessionLoss + ) -> SessionLoss? { + guard held?.driver.holdsSuspensionBlockingResource == true else { return nil } + if ended.duringWork { return .inBackgroundAfterStatement } + return loss == .duringRollBack ? .duringRollBack : .inBackground + } + // True when the transaction a redial ended is settled for the user. @discardableResult private func reportDroppedTransaction(_ loss: SessionLoss?) -> Bool { @@ -685,6 +703,20 @@ final class ConnectionCoordinator { ) case .duringRollBack: nil + case .inBackground: + String( + format: String( + localized: "TablePro closed %@ when it went to the background, which rolled back its open transaction. It has been reopened; run the statements again, and commit before you leave the app." + ), + connectionName + ) + case .inBackgroundAfterStatement: + String( + format: String( + localized: "TablePro closed %@ when it went to the background while a statement was running, so its open transaction may have been committed or rolled back. It has been reopened; check which of its changes were saved before running them again." + ), + connectionName + ) } } diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift index 04d1f8e7c4..db2d147650 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift @@ -21,6 +21,13 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { let databaseType: DatabaseType private let connectionEncoding: MySQLConnectionEncoding let signsInWithToken: Bool + let queryTimeoutSeconds: Int? + // None without a query timeout: Connector/C reports its own read timeout as a lost connection while the server + // runs the statement to the end, so it only backs up a limit the server enforces. The keepalive fails a dead path. + let socketTimeoutSeconds: UInt32 + // A kill signs in anew, which an IAM token minted more than 15 minutes ago cannot, as on the Mac. + private let refreshPassword: (@Sendable () async throws -> String?)? + private let clock: any Clock var supportsSchemas: Bool { false } var currentSchema: String? { nil } @@ -56,7 +63,10 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { ssl: DriverSSLConfiguration = .disabled, databaseType: DatabaseType = .mysql, connectionEncoding: MySQLConnectionEncoding = .utf8, - signsInWithToken: Bool = false + signsInWithToken: Bool = false, + queryTimeoutSeconds: Int? = nil, + refreshPassword: (@Sendable () async throws -> String?)? = nil, + clock: any Clock = ContinuousClock() ) { self.host = host self.port = port @@ -67,13 +77,25 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { self.databaseType = databaseType self.connectionEncoding = connectionEncoding self.signsInWithToken = signsInWithToken + self.queryTimeoutSeconds = queryTimeoutSeconds + self.socketTimeoutSeconds = mysqlSocketTimeoutSeconds(forQueryTimeout: queryTimeoutSeconds ?? 0) + self.refreshPassword = refreshPassword + self.clock = clock } // MARK: - Connection private static let logger = Logger(subsystem: "com.TablePro", category: "MySQLDriver") - static func sessionSetupStatements(for databaseType: DatabaseType) -> [String] { + // OceanBase stops a statement after 10 s by default, so a session without a query timeout lifts that limit. + static func sessionSetupStatements( + for databaseType: DatabaseType, + banner: String? = nil, + queryTimeoutSeconds: Int? = nil + ) -> [String] { + if let queryTimeoutSeconds, queryTimeoutSeconds > 0 { + return serverFlavor(for: databaseType, banner: banner).queryTimeoutStatements(seconds: queryTimeoutSeconds) + } guard databaseType == .oceanbase else { return [] } return MySQLServerFlavor.oceanbase(version: nil).queryTimeoutStatements(seconds: 0) } @@ -115,10 +137,15 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { try await actor.connect( host: host, port: port, user: user, password: password, database: database, ssl: ssl, encoding: connectionEncoding, databaseType: databaseType, - signsInWithToken: signsInWithToken + signsInWithToken: signsInWithToken, + signIn: MySQLKillSignIn(password: password, refresh: refreshPassword, clock: clock) ) serverVersion = await actor.serverVersion() - for statement in Self.sessionSetupStatements(for: databaseType) { + let setup = Self.sessionSetupStatements( + for: databaseType, banner: serverVersion, queryTimeoutSeconds: queryTimeoutSeconds + ) + // A server that refuses the limit still runs queries, as on the Mac. + for statement in setup { do { _ = try await actor.execute(statement) } catch { @@ -128,6 +155,7 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { break } } + try await actor.completeConnect(socketTimeoutSeconds: socketTimeoutSeconds) await settleLexicalFacts() } @@ -165,18 +193,23 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { ) } + // Runs without the actor, which stays blocked in Connector/C for as long as the statement runs. The editor's Stop + // leaves its stream first, so this stops only a statement whose reader is gone. func cancelCurrentQuery() async throws { - // MySQL C API does not support async cancel without a second connection. - // No-op for mobile. + actor.killer.stop() } + // The statement is reserved before the producer starts, so a reader that leaves names this stream's statement and + // never another surface's. func executeStreaming(query: String, options: StreamOptions) -> AsyncThrowingStream { let actor = self.actor + let killer = actor.killer + let statement = killer.reserve() return AsyncThrowingStream { continuation in let task = Task { var openStream: MySQLStreamID? do { - switch try await actor.beginStream(query: query) { + switch try await actor.beginStream(query: query, statement: statement) { case .noResult(let affectedRows): if affectedRows != 0 { continuation.yield(.rowsAffected(affectedRows)) @@ -214,7 +247,12 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } - continuation.onTermination = { _ in task.cancel() } + continuation.onTermination = { reason in + task.cancel() + if case .cancelled = reason { + killer.readerLeft(statement) + } + } } } @@ -449,6 +487,46 @@ nonisolated enum MySQLTLSPolicy: Equatable, Sendable { } } +// What every connection to the server is opened with, so the one that sends a kill signs in the way the session did. +nonisolated enum MySQLDialOptions { + static func apply( + to handle: UnsafeMutablePointer, + policy: MySQLTLSPolicy, + ssl: DriverSSLConfiguration, + serverName: MySQLTLSServerName?, + signsInWithToken: Bool, + connectSeconds: UInt32, + socketSeconds: UInt32 + ) { + mysql_options(handle, MYSQL_SET_CHARSET_NAME, MySQLConnectionEncoding.sessionCharacterSetName) + + var connectTimeout = connectSeconds + mysql_options(handle, MYSQL_OPT_CONNECT_TIMEOUT, &connectTimeout) + var readTimeout = socketSeconds + mysql_options(handle, MYSQL_OPT_READ_TIMEOUT, &readTimeout) + var writeTimeout = socketSeconds + mysql_options(handle, MYSQL_OPT_WRITE_TIMEOUT, &writeTimeout) + + var reconnect: my_bool = 0 + mysql_options(handle, MYSQL_OPT_RECONNECT, &reconnect) + + var allowLocalInfile: UInt32 = 0 + mysql_options(handle, MYSQL_OPT_LOCAL_INFILE, &allowLocalInfile) + + policy.apply(to: handle, ssl: ssl, serverName: serverName) + if policy == .disabled || policy == .preferred { + _ = MySQLPlaintextPassword.methodsWithoutTLS.withCString { + mysql_options(handle, MARIADB_OPT_RESTRICTED_AUTH, $0) + } + } + if signsInWithToken { + // A no-op in Connector/C 3.4, which answers a cleartext request anyway; set to match the Mac. + var enableCleartext: my_bool = 1 + mysql_options(handle, MYSQL_ENABLE_CLEARTEXT_PLUGIN, &enableCleartext) + } + } +} + // The name a tunneled connection's certificate must carry, as a C string the TLS callback reads // during the handshake. nonisolated final class MySQLTLSServerName: @unchecked Sendable { @@ -566,14 +644,23 @@ nonisolated enum MySQLSessionTransaction { // MARK: - MySQL Actor (thread-safe C API access) private actor MySQLActor { + nonisolated let killer = MySQLStatementKiller() private var mysql: UnsafeMutablePointer? private static let connectSeconds = 15 + // Bounds the sign-in and the setup statements after it; `completeConnect` gives the session its own. + private static let signInSocketSeconds: UInt32 = 30 + private static let connectionLossCodes: Set = [ + UInt32(CR_SERVER_GONE_ERROR), UInt32(CR_SERVER_LOST), UInt32(CR_SERVER_LOST_EXTENDED) + ] private var encoding: MySQLConnectionEncoding = .utf8 private var errorLanguage: String.Encoding? private var showTextIsBinary = false private var tlsServerName: MySQLTLSServerName? + private var flavor: MySQLServerFlavor = .mysql + // The read timeout the socket has now: the sign-in's until `completeConnect`. + private var socketTimeoutSeconds: UInt32 = 0 private struct AttemptFailure: Error { let code: UInt32 @@ -584,12 +671,13 @@ private actor MySQLActor { func connect( host: String, port: Int, user: String, password: String, database: String, ssl: DriverSSLConfiguration, encoding: MySQLConnectionEncoding, databaseType: DatabaseType, - signsInWithToken: Bool + signsInWithToken: Bool, signIn: MySQLKillSignIn ) async throws { self.encoding = encoding errorLanguage = nil showTextIsBinary = false close() + socketTimeoutSeconds = Self.signInSocketSeconds guard let portU32 = UInt32(exactly: port), (1...65_535).contains(port) else { throw MySQLError.connectionFailed( @@ -608,12 +696,14 @@ private actor MySQLActor { // authentication with the typed password, so Preferred asks for TLS outright and falls back without it. let firstPolicy: MySQLTLSPolicy = policy == .preferred ? .required : policy let handle: UnsafeMutablePointer + var dialedPolicy = firstPolicy do { do { handle = try await open(endpoint, ssl: ssl, policy: firstPolicy, serverName: serverName, deadline: deadline) } catch let failure as AttemptFailure where policy == .preferred && MariaDBSSLClassifier.sslOnlyErrorCodes.contains(failure.code) { // Preferred retries in plaintext after a failed handshake, as the Mac does. + dialedPolicy = .disabled handle = try await open(endpoint, ssl: ssl, policy: .disabled, serverName: nil, deadline: deadline) } } catch let failure as AttemptFailure where MySQLPlaintextPassword.isRefusal(code: failure.code) { @@ -637,10 +727,52 @@ private actor MySQLActor { } let banner = mysql_get_server_info(handle).map { String(cString: $0) } + let sessionFlavor = MySQLDriver.serverFlavor(for: databaseType, banner: banner) + // TiDB's handshake carries only the low 32 bits of its connection id. + let tidbConnectionId = sessionFlavor.isTiDB ? connectionIdentifier(on: handle) : nil + showTextIsBinary = MySQLDriver.labelsShowTextAsBinary(for: databaseType, banner: banner) errorLanguage = MariaDBCharacterSet.errorLanguage(on: handle) tlsServerName = serverName + flavor = sessionFlavor self.mysql = handle + + let dial = MySQLKillDial( + host: endpoint.host, port: endpoint.port, user: endpoint.user, signIn: signIn, + ssl: ssl, policy: dialedPolicy, serverName: dialedPolicy == .disabled ? nil : serverName, + signsInWithToken: signsInWithToken + ) + killer.attach(MySQLKillStatement.text( + flavor: sessionFlavor, banner: banner, threadId: mysql_thread_id(handle), tidbConnectionId: tidbConnectionId + ).map { statement in + MySQLSessionKill( + statement: statement, + absorbsLatchedKill: MySQLKillStatement.absorbsLatchedKill(flavor: sessionFlavor), + killsWhenReaderLeaves: !sessionFlavor.dropsIdleSessionOnKillQuery, + dial: dial + ) + }) + } + + // Last, as the Mac's completeConnect: the sign-in's reads and the setup statements after it keep its bound, so a + // server that stops answering right after the sign-in still fails the connect. + func completeConnect(socketTimeoutSeconds sessionTimeout: UInt32) throws { + guard let mysql else { throw MySQLError.notConnected } + guard tablepro_mysql_set_io_timeout(mysql, sessionTimeout) == 0, + SessionSocketKeepalive.enable(on: mysql_get_socket(mysql)) else { + close() + throw MySQLError.connectionFailed(String(localized: "Failed to configure the connection timeout")) + } + socketTimeoutSeconds = sessionTimeout + } + + private func connectionIdentifier(on handle: UnsafeMutablePointer) -> UInt64? { + let probe = MySQLFlavorResolution.connectionIdentifierProbe + guard mysql_real_query(handle, probe, UInt(probe.utf8.count)) == 0, + let result = mysql_store_result(handle) else { return nil } + defer { mysql_free_result(result) } + guard let row = mysql_fetch_row(result), let value = row[0] else { return nil } + return UInt64(String(cString: value)) } private struct Endpoint: Sendable { @@ -667,32 +799,15 @@ private actor MySQLActor { throw MySQLError.connectionFailed("Failed to initialize MySQL client") } - mysql_options(handle, MYSQL_SET_CHARSET_NAME, MySQLConnectionEncoding.sessionCharacterSetName) - - var timeout: UInt32 = 10 - mysql_options(handle, MYSQL_OPT_CONNECT_TIMEOUT, &timeout) - var readTimeout: UInt32 = 30 - mysql_options(handle, MYSQL_OPT_READ_TIMEOUT, &readTimeout) - var writeTimeout: UInt32 = 30 - mysql_options(handle, MYSQL_OPT_WRITE_TIMEOUT, &writeTimeout) - - var reconnect: my_bool = 0 - mysql_options(handle, MYSQL_OPT_RECONNECT, &reconnect) - - var allowLocalInfile: UInt32 = 0 - mysql_options(handle, MYSQL_OPT_LOCAL_INFILE, &allowLocalInfile) - - policy.apply(to: handle, ssl: ssl, serverName: serverName) - if policy == .disabled || policy == .preferred { - _ = MySQLPlaintextPassword.methodsWithoutTLS.withCString { - mysql_options(handle, MARIADB_OPT_RESTRICTED_AUTH, $0) - } - } - if endpoint.signsInWithToken { - // A no-op in Connector/C 3.4, which answers a cleartext request anyway; set to match the Mac. - var enableCleartext: my_bool = 1 - mysql_options(handle, MYSQL_ENABLE_CLEARTEXT_PLUGIN, &enableCleartext) - } + MySQLDialOptions.apply( + to: handle, + policy: policy, + ssl: ssl, + serverName: serverName, + signsInWithToken: endpoint.signsInWithToken, + connectSeconds: 10, + socketSeconds: Self.signInSocketSeconds + ) // A late call closes the handle it was still using, rather than the caller closing it. nonisolated(unsafe) let unsafeHandle = handle @@ -732,6 +847,7 @@ private actor MySQLActor { } func close() { + killer.detach() guard let mysql else { return } if let result = streamingResult { // An open result points at the handle `mysql_close` frees, so it goes first; shutting the socket @@ -814,6 +930,8 @@ private actor MySQLActor { func execute(_ query: String) throws -> RawMySQLResult { guard let mysql else { throw MySQLError.notConnected } endLeftoverStream(of: mysql) + let sent = send(killer.reserve(), on: mysql) + defer { finish(sent, on: mysql) } let start = Date() @@ -882,18 +1000,28 @@ private actor MySQLActor { private var streamGeneration: UInt64 = 0 private var streamingColumns: [ColumnInfo] = [] private var streamingDecoding = MySQLResultColumns() + private var streamingStatement: SentStatement? - func beginStream(query: String) throws -> MySQLBeginStreamResult { + func beginStream(query: String, statement: UInt64) throws -> MySQLBeginStreamResult { guard let mysql else { throw MySQLError.notConnected } endLeftoverStream(of: mysql) streamGeneration &+= 1 let stream = MySQLStreamID(generation: streamGeneration) + let sent = send(statement, on: mysql) + // A reader that left while this waited for the session stops it here; one that leaves later finds it in + // flight and kills it. + guard !Task.isCancelled else { + finish(sent, on: mysql) + throw CancellationError() + } guard mysql_real_query(mysql, query, UInt(query.utf8.count)) == 0 else { + finish(sent, on: mysql) throw MySQLError.queryFailed(message(from: mysql)) } guard let result = mysql_use_result(mysql) else { + defer { finish(sent, on: mysql) } if mysql_field_count(mysql) != 0 { throw MySQLError.queryFailed(message(from: mysql)) } @@ -904,6 +1032,7 @@ private actor MySQLActor { } streamingResult = result + streamingStatement = sent let described = MariaDBCharacterSet.describeColumns( of: mysql_fetch_fields(result), count: Int(mysql_num_fields(result)), encoding: encoding, @@ -983,17 +1112,57 @@ private actor MySQLActor { private func releaseStream(of mysql: UnsafeMutablePointer) throws { guard let result = streamingResult else { return } + let sent = streamingStatement clearStream() + defer { if let sent { finish(sent, on: mysql) } } try drain(result, of: mysql) try consumeTrailingResults(of: mysql) } private func clearStream() { streamingResult = nil + streamingStatement = nil streamingColumns = [] streamingDecoding = MySQLResultColumns() } + // MARK: - Stopping a statement + + nonisolated private struct SentStatement: Sendable { + let id: UInt64 + let sentAt: ContinuousClock.Instant + } + + private func send(_ statement: UInt64, on mysql: UnsafeMutablePointer) -> SentStatement { + if killer.collectLatchedKill() { + absorbLatchedKill(on: mysql) + } + killer.sent(statement) + return SentStatement(id: statement, sentAt: .now) + } + + // A throwaway statement collects the flag, failing with 1317 where the server kept it, so the user's runs as sent. + // It costs the session its ROW_COUNT() and FOUND_ROWS(), as on the Mac. + private func absorbLatchedKill(on mysql: UnsafeMutablePointer) { + let probe = "SELECT 1" + guard mysql_real_query(mysql, probe, UInt(probe.utf8.count)) == 0 else { return } + if let result = mysql_store_result(mysql) { + mysql_free_result(result) + } + } + + // Connector/C reports its own read timeout as the same lost connection a server drop gives, and only that one + // leaves the statement running, so only a wait that long kills it. + private func finish(_ sent: SentStatement, on mysql: UnsafeMutablePointer) { + let code = mysql_errno(mysql) + let interrupted = code != 0 && flavor.isInterruptedByKill(errno: code, message: message(from: mysql)) + killer.finished(sent.id, interrupted: interrupted) + guard Self.connectionLossCodes.contains(code), + mysqlWaitCouldOutlastSocketTimeout(sent.sentAt.duration(to: .now), socketTimeoutSeconds: socketTimeoutSeconds) + else { return } + killer.killOrphan() + } + /// The first result set is the statement's answer, as before. A later one, such as a `CALL` that selects /// twice, is read and dropped so the session can take the next command, and an error in one fails the statement. private func consumeTrailingResults(of mysql: UnsafeMutablePointer) throws { diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift new file mode 100644 index 0000000000..e6159c3d2f --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift @@ -0,0 +1,339 @@ +import CMariaDB +import Foundation +import os +import TableProMySQLCore + +// Which statement a kill is for. A session runs one statement at a time, so a kill is taken only for the statement +// in flight; one asked for before its statement is sent, or after it ends, is dropped and never reaches the next one. +nonisolated struct MySQLKillRequests: Equatable, Sendable { + private var lastStatement: UInt64 = 0 + private(set) var inFlight: UInt64? + private var killRequested: UInt64? + private var killDelivered: UInt64? + private var killCollected: UInt64? + private var lastLeft: UInt64? + + mutating func reserve() -> UInt64 { + lastStatement &+= 1 + return lastStatement + } + + mutating func sent(_ statement: UInt64) { + inFlight = statement + } + + mutating func finished(_ statement: UInt64, interrupted: Bool) { + if interrupted { killCollected = statement } + guard inFlight == statement else { return } + inFlight = nil + } + + mutating func detach() { + inFlight = nil + killDelivered = nil + killCollected = nil + } + + // Once per statement: a second kill could land after the first stopped it and stop the next statement instead. + mutating func kill(_ statement: UInt64) -> Bool { + guard inFlight == statement, killRequested != statement else { return false } + killRequested = statement + return true + } + + mutating func readerLeft(_ statement: UInt64) { + lastLeft = statement + } + + // Stop names no statement, so it takes the one in flight only when that is the one whose reader left last: the + // editor's own, never a statement another surface is still reading. + mutating func killLeftInFlight() -> UInt64? { + guard let statement = inFlight, lastLeft == statement, kill(statement) else { return nil } + return statement + } + + func isInFlight(_ statement: UInt64) -> Bool { + inFlight == statement + } + + mutating func delivered(_ statement: UInt64) { + killDelivered = statement + } + + // Order-independent: the kill is recorded from its own queue while its statement reads the interruption on the + // actor. Clears the record either way. + mutating func takeLatchedKill() -> Bool { + defer { + killDelivered = nil + killCollected = nil + } + guard let statement = killDelivered else { return false } + return killCollected != statement + } +} + +// The kill statement for a session, as the Mac plugin's MySQLKillTarget spells it. Nil where only `KILL `, which +// ends the session, would stop the statement, and for Databend, whose session id this driver never reads. +nonisolated enum MySQLKillStatement { + static func text( + flavor: MySQLServerFlavor, + banner: String?, + threadId: UInt, + tidbConnectionId: UInt64? + ) -> String? { + guard MySQLServerVersion.canStopStatementAlone(banner: banner, flavor: flavor) else { return nil } + switch flavor { + case .tidb: + if let tidbConnectionId { return "KILL TIDB QUERY \(tidbConnectionId)" } + return threadId > 0 ? "KILL QUERY \(threadId)" : nil + case .databend: + return nil + case .mysql, .mariadb, .oceanbase, .starrocks: + return threadId > 0 ? "KILL QUERY \(threadId)" : nil + } + } + + // Measured on the Mac: MySQL 5.5 and 5.6 and MariaDB 5.5 keep a kill that lands on an idle session and fail the + // next statement with 1317. The other engines are unmeasured, so none gets a statement inserted on a guess. + static func absorbsLatchedKill(flavor: MySQLServerFlavor) -> Bool { + switch flavor { + case .mysql, .mariadb: + return true + case .tidb, .databend, .oceanbase, .starrocks: + return false + } + } +} + +// The token the session signed in with serves a kill while it is young, because a mint can be an HTTPS round trip the +// next statement waits behind. Past ten minutes, five short of an RDS token's fifteen, a kill mints its own and later +// kills reuse that one the same way. The age runs from the connect, which comes after the tunnel and any prompts it +// raised, so a reused token can be older than it looks; the server's refusal of one is what settles it. +nonisolated final class MySQLKillSignIn: @unchecked Sendable { + static let reuseWindow: Duration = .seconds(600) + private static let mintTimeout: DispatchTimeInterval = .seconds(5) + + nonisolated struct Credential: Equatable, Sendable { + let password: String + // Handed out on its age alone, so a refusal means a mint could still sign the kill in. + let isReused: Bool + } + + nonisolated private struct NoToken: Error {} + + private let lock = NSLock() + private let stopwatch: @Sendable () -> @Sendable () -> Duration + private let refresh: (@Sendable () async throws -> String?)? + private var token: String + private var age: @Sendable () -> Duration + + init(password: String, refresh: (@Sendable () async throws -> String?)?, clock: any Clock) { + stopwatch = Self.stopwatches(on: clock) + self.refresh = refresh + token = password + age = stopwatch() + } + + // On the kill queue, never a Swift concurrency thread, so it may wait for the mint. + func credential() -> Credential { + let (current, currentAge) = lock.withLock { (token, age()) } + guard refresh != nil else { return Credential(password: current, isReused: false) } + guard currentAge >= Self.reuseWindow else { return Credential(password: current, isReused: true) } + return Credential(password: mint() ?? current, isReused: false) + } + + // The server refused `refused`, a token handed out on its age. One mint replaces it, unless another kill already did. + func passwordAfterRefusal(of refused: String) -> String? { + let current = lock.withLock { token } + guard current == refused else { return current } + return mint() + } + + private func mint() -> String? { + guard let refresh else { return nil } + let mintStarted = stopwatch() + let minted = MySQLBlockingPasswordRefresh.wait( + for: { + guard let fresh = try await refresh() else { throw NoToken() } + return fresh + }, + timeout: Self.mintTimeout + ) + guard let minted else { return nil } + lock.withLock { + token = minted + age = mintStarted + } + return minted + } + + // Each call starts a new one: the time since that call. + private static func stopwatches>(on clock: C) -> @Sendable () -> @Sendable () -> Duration { + { + let start = clock.now + return { start.duration(to: clock.now) } + } + } +} + +// The way back to a server a statement is running on, repeating the transport and TLS the session actually got. +nonisolated struct MySQLKillDial: Sendable { + let host: String + let port: UInt32 + let user: String + let signIn: MySQLKillSignIn + let ssl: DriverSSLConfiguration + let policy: MySQLTLSPolicy + let serverName: MySQLTLSServerName? + let signsInWithToken: Bool + + private static let logger = Logger(subsystem: "com.TablePro", category: "MySQLStatementKiller") + private static let timeoutSeconds: UInt32 = 5 + private static let accessDeniedCode: UInt32 = 1_045 + + nonisolated private enum SignInAttempt { + case connected(UnsafeMutablePointer) + case refused(code: UInt32, message: String) + } + + func send(_ statement: String) -> Bool { + let credential = signIn.credential() + var attempt = open(signingInWith: credential.password) + if case .refused(Self.accessDeniedCode, _) = attempt, credential.isReused, + let fresh = signIn.passwordAfterRefusal(of: credential.password) { + attempt = open(signingInWith: fresh) + } + let handle: UnsafeMutablePointer + switch attempt { + case .connected(let connected): + handle = connected + case .refused(_, let message): + Self.logger.warning("\(statement, privacy: .private) could not connect: \(message, privacy: .private)") + return false + } + defer { mysql_close(handle) } + guard mysql_real_query(handle, statement, UInt(statement.utf8.count)) == 0 else { + Self.logger.warning("\(statement, privacy: .private) was refused: \(Self.failure(of: handle), privacy: .private)") + return false + } + return true + } + + // A refused sign-in leaves its handle closed, so a second try opens its own. + private func open(signingInWith password: String) -> SignInAttempt { + guard let handle = mysql_init(nil) else { return .refused(code: 0, message: "mysql_init failed") } + MySQLDialOptions.apply( + to: handle, + policy: policy, + ssl: ssl, + serverName: serverName, + signsInWithToken: signsInWithToken, + connectSeconds: Self.timeoutSeconds, + socketSeconds: Self.timeoutSeconds + ) + let connected = withExtendedLifetime(serverName) { + mysql_real_connect(handle, host, user, password, nil, port, nil, 0) != nil + } + guard connected else { + let attempt = SignInAttempt.refused(code: mysql_errno(handle), message: Self.failure(of: handle)) + mysql_close(handle) + return attempt + } + return .connected(handle) + } + + private static func failure(of handle: UnsafeMutablePointer) -> String { + let code = mysql_errno(handle) + guard let text = mysql_error(handle) else { return "error \(code)" } + return "\(code) \(String(cString: text))" + } +} + +nonisolated struct MySQLSessionKill: Sendable { + let statement: String + let absorbsLatchedKill: Bool + // TiDB drops a session a kill lands on once the statement has ended, so only Stop kills there, as on the Mac. + let killsWhenReaderLeaves: Bool + let dial: MySQLKillDial +} + +// The actor sits blocked in Connector/C for as long as a statement runs, so Stop reaches the server from here, over a +// second connection. The lock is never held across a network call: opening that connection is a TCP connect, a TLS +// handshake and a sign-in. +nonisolated final class MySQLStatementKiller: @unchecked Sendable { + private typealias Accepted = (statement: UInt64, target: MySQLSessionKill) + + private let queue = DispatchQueue(label: "com.TablePro.mobile.mysql.kill", qos: .userInitiated) + private let lock = NSLock() + private let pending = DispatchGroup() + private var target: MySQLSessionKill? + private var requests = MySQLKillRequests() + + func attach(_ target: MySQLSessionKill?) { + lock.withLock { self.target = target } + } + + func detach() { + lock.withLock { + target = nil + requests.detach() + } + } + + func reserve() -> UInt64 { + lock.withLock { requests.reserve() } + } + + // A kill still on its way could stop the statement about to go out, so it is waited for first. + func collectLatchedKill() -> Bool { + pending.wait() + return lock.withLock { requests.takeLatchedKill() && target?.absorbsLatchedKill == true } + } + + func sent(_ statement: UInt64) { + lock.withLock { requests.sent(statement) } + } + + func finished(_ statement: UInt64, interrupted: Bool) { + lock.withLock { requests.finished(statement, interrupted: interrupted) } + } + + // A page change, a closed screen or Stop: the reader of the stream left while its statement may still run. + func readerLeft(_ statement: UInt64) { + deliver(lock.withLock { () -> Accepted? in + requests.readerLeft(statement) + guard target?.killsWhenReaderLeaves == true else { return nil } + return accepted(requests.kill(statement) ? statement : nil) + }) + } + + func stop() { + deliver(lock.withLock { accepted(requests.killLeftInFlight()) }) + } + + // The socket timeout gave up on a statement the server is still running, and the handle can no longer reach it. + func killOrphan() { + guard let target = lock.withLock({ self.target }) else { return } + queue.async { + _ = target.dial.send(target.statement) + } + } + + // Entered in the same critical section that takes the kill, so a statement that ends right after still finds the + // kill pending and the next one waits for it. + private func accepted(_ statement: UInt64?) -> Accepted? { + guard let statement, let target = self.target else { return nil } + pending.enter() + return (statement, target) + } + + private func deliver(_ accepted: Accepted?) { + guard let accepted else { return } + queue.async { [self] in + defer { pending.leave() } + guard lock.withLock({ requests.isInFlight(accepted.statement) }) else { return } + guard accepted.target.dial.send(accepted.target.statement) else { return } + lock.withLock { requests.delivered(accepted.statement) } + } + } +} diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift index 763d965657..55018c81c9 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift @@ -21,7 +21,7 @@ nonisolated enum PostgreSQLConnectionString { ("connect_timeout", String(connectTimeoutSeconds)), ("sslmode", ssl.postgresSSLMode), ("client_encoding", clientEncoding), - ] + ] + SessionSocketKeepalive.libpqParameters if let caPath = ssl.existingCACertificatePath { parameters.append(("sslrootcert", caPath)) diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index 0539067889..eb5153fcd1 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -382,6 +382,10 @@ private actor PostgreSQLActor { } try await pollUntilConnected(connection) + // This libpq is built without `__darwin__`, so it takes keepalives_idle and never sets it on the socket. + guard SessionSocketKeepalive.enable(on: PQsocket(connection)) else { + throw PostgreSQLError.connectionFailed(String(localized: "Failed to configure the connection timeout")) + } self.conn = connection adopted = true diff --git a/apple/TableProMobile/TableProMobile/Drivers/SessionSocketKeepalive.swift b/apple/TableProMobile/TableProMobile/Drivers/SessionSocketKeepalive.swift new file mode 100644 index 0000000000..4af0230aa4 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Drivers/SessionSocketKeepalive.swift @@ -0,0 +1,32 @@ +import Foundation + +// Left to the kernel, Darwin sends the first keepalive after 7,200 s of idle, while an AWS Network Load Balancer drops +// an idle flow after 350 s, so a reply lost with the flow, or on a path that died, blocked a session waiting on it for +// hours. These keep the flow open and fail a dead peer about 90 s after the last traffic, the values the Mac's +// PostgreSQL connection uses. A slow statement on a live server still completes: its kernel answers the probes. +nonisolated enum SessionSocketKeepalive { + static let idleSeconds: Int32 = 60 + static let intervalSeconds: Int32 = 10 + static let probeCount: Int32 = 3 + + // libpq applies the interval and count while it connects; the iOS build drops the idle time, which the driver sets + // with `enable` once the connect completes. + static let libpqParameters: [(String, String)] = [ + ("keepalives", "1"), + ("keepalives_idle", String(idleSeconds)), + ("keepalives_interval", String(intervalSeconds)), + ("keepalives_count", String(probeCount)), + ] + + static func enable(on socket: Int32) -> Bool { + set(socket, SOL_SOCKET, SO_KEEPALIVE, 1) + && set(socket, IPPROTO_TCP, TCP_KEEPALIVE, idleSeconds) + && set(socket, IPPROTO_TCP, TCP_KEEPINTVL, intervalSeconds) + && set(socket, IPPROTO_TCP, TCP_KEEPCNT, probeCount) + } + + private static func set(_ socket: Int32, _ level: Int32, _ name: Int32, _ value: Int32) -> Bool { + var value = value + return setsockopt(socket, level, name, &value, socklen_t(MemoryLayout.size)) == 0 + } +} diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 35d9ee1a79..869b707d46 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -9873,6 +9873,9 @@ } } } + }, + "Failed to configure the connection timeout" : { + }, "Failed to decode the SSO portal response for profile \"%@\"." : { "extractionState" : "manual", @@ -23233,6 +23236,12 @@ }, "TablePro cannot upload changes until there is space in iCloud. Changes on this device are kept here and upload once you free up space or add storage." : { + }, + "TablePro closed %@ when it went to the background while a statement was running, so its open transaction may have been committed or rolled back. It has been reopened; check which of its changes were saved before running them again." : { + + }, + "TablePro closed %@ when it went to the background, which rolled back its open transaction. It has been reopened; run the statements again, and commit before you leave the app." : { + }, "TablePro could not read that file." : { "localizations" : { diff --git a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift index 3e93e7444d..d2d895b1bb 100644 --- a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift +++ b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift @@ -16,6 +16,8 @@ nonisolated final class IOSDriverFactory: DriverFactory { private let secureStore: any SecureStore // The session configuration of an engine reached over HTTPS, which a test points at its own URLProtocol. private let apiSessionConfiguration: @Sendable () -> URLSessionConfiguration + // Times how long a MySQL kill may reuse the token its session signed in with. + private let clock: any Clock init( bookmarkStore: FileBookmarkStore = FileBookmarkStore(), @@ -23,7 +25,8 @@ nonisolated final class IOSDriverFactory: DriverFactory { localFiles: LocalDatabaseFileLocator = .live, mint: IAMPasswordMint = IAMPasswordMint(), secureStore: any SecureStore = KeychainSecureStore(), - apiSessionConfiguration: @escaping @Sendable () -> URLSessionConfiguration = { .default } + apiSessionConfiguration: @escaping @Sendable () -> URLSessionConfiguration = { .default }, + clock: any Clock = ContinuousClock() ) { self.bookmarkStore = bookmarkStore self.materializer = materializer @@ -31,6 +34,7 @@ nonisolated final class IOSDriverFactory: DriverFactory { self.mint = mint self.secureStore = secureStore self.apiSessionConfiguration = apiSessionConfiguration + self.clock = clock } private func sqliteSource(for connection: DatabaseConnection) throws -> LocalDatabaseFileSource { @@ -119,6 +123,15 @@ nonisolated final class IOSDriverFactory: DriverFactory { for connection: DatabaseConnection, password: String?, preTunnelHost: String? + ) throws -> any DatabaseDriver { + try createDriver(for: connection, password: password, preTunnelHost: preTunnelHost, refreshPassword: nil) + } + + func createDriver( + for connection: DatabaseConnection, + password: String?, + preTunnelHost: String?, + refreshPassword: (@Sendable () async throws -> String?)? ) throws -> any DatabaseDriver { switch connection.type { case .sqlite where connection.isSample: @@ -140,7 +153,11 @@ nonisolated final class IOSDriverFactory: DriverFactory { ssl: try ssl(for: connection, serverName: preTunnelHost), databaseType: connection.type, connectionEncoding: MySQLConnectionEncoding(additionalFields: connection.additionalFields), - signsInWithToken: connection.iamSignIn != nil + signsInWithToken: connection.iamSignIn != nil, + queryTimeoutSeconds: connection.queryTimeoutSeconds, + // Only a token expires; a password session signs in again with the password it has. + refreshPassword: connection.iamSignIn == nil ? nil : refreshPassword, + clock: clock ) case .postgresql, .redshift: guard connection.macSocketPath == nil else { diff --git a/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift b/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift index a5d685650d..4ae63bc82c 100644 --- a/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift +++ b/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift @@ -3,11 +3,56 @@ import os import Security import TableProDatabase +nonisolated struct KeychainCopiedItem: Sendable { + let status: OSStatus + let data: Data? + let modified: Date? +} + +nonisolated protocol KeychainItemStore: Sendable { + func add(_ attributes: [String: Any]) -> OSStatus + func update(_ query: [String: Any], attributes: [String: Any]) -> OSStatus + // The first item the query matches, with its data and modification date. + func copyItem(_ query: [String: Any]) -> KeychainCopiedItem + func delete(_ query: [String: Any]) -> OSStatus +} + +nonisolated struct SystemKeychainItemStore: KeychainItemStore { + func add(_ attributes: [String: Any]) -> OSStatus { + SecItemAdd(attributes as CFDictionary, nil) + } + + func update(_ query: [String: Any], attributes: [String: Any]) -> OSStatus { + SecItemUpdate(query as CFDictionary, attributes as CFDictionary) + } + + func copyItem(_ query: [String: Any]) -> KeychainCopiedItem { + var query = query + query[kSecReturnData as String] = true + query[kSecReturnAttributes as String] = true + query[kSecMatchLimit as String] = kSecMatchLimitOne + var result: AnyObject? + let status = SecItemCopyMatching(query as CFDictionary, &result) + let item = result as? [String: Any] + return KeychainCopiedItem( + status: status, + data: item?[kSecValueData as String] as? Data, + modified: item?[kSecAttrModificationDate as String] as? Date + ) + } + + func delete(_ query: [String: Any]) -> OSStatus { + SecItemDelete(query as CFDictionary) + } +} + nonisolated final class KeychainSecureStore: SecureStore { private static let logger = Logger(subsystem: "com.TablePro", category: "KeychainSecureStore") private static let serviceName = "com.TablePro" private let accessGroup: String? + private let syncsPasswords: @Sendable () -> Bool + private let items: any KeychainItemStore private static let cachedAccessGroup = OSAllocatedUnfairLock(initialState: nil) @@ -29,8 +74,13 @@ nonisolated final class KeychainSecureStore: SecureStore { return group } - init() { + init( + syncsPasswords: @escaping @Sendable () -> Bool = { AppPreferences.syncsPasswords }, + items: any KeychainItemStore = SystemKeychainItemStore() + ) { self.accessGroup = Self.resolveAccessGroup() + self.syncsPasswords = syncsPasswords + self.items = items } private func applyingAccessGroup(_ query: [String: Any]) -> [String: Any] { @@ -54,72 +104,83 @@ nonisolated final class KeychainSecureStore: SecureStore { syncsPasswords && !ConnectionSecretKind.isDeviceOnly(account: key) } + // Synchronizable is part of an item's identity, and deleting a synced item deletes it on every device. func store(_ value: String, forKey key: String) throws { guard let data = value.data(using: .utf8) else { return } - let synchronizable = Self.synchronizes(key, syncsPasswords: AppPreferences.syncsPasswords) + let synchronizable = Self.synchronizes(key, syncsPasswords: syncsPasswords()) + let accessible = Self.accessibility(forSync: synchronizable) + let target = itemQuery(forKey: key, synchronizable: synchronizable) - let deleteQuery: [String: Any] = [ - kSecClass as String: kSecClassGenericPassword, - kSecAttrService as String: Self.serviceName, - kSecAttrAccount as String: key, - kSecAttrSynchronizable as String: kSecAttrSynchronizableAny, - kSecUseDataProtectionKeychain as String: true, - ] - SecItemDelete(applyingAccessGroup(deleteQuery) as CFDictionary) - - var addQuery: [String: Any] = [ - kSecClass as String: kSecClassGenericPassword, - kSecAttrService as String: Self.serviceName, - kSecAttrAccount as String: key, + var status = items.update(target, attributes: [ kSecValueData as String: data, - kSecAttrAccessible as String: Self.accessibility(forSync: synchronizable), - kSecUseDataProtectionKeychain as String: true, - ] - if synchronizable { - addQuery[kSecAttrSynchronizable as String] = true + kSecAttrAccessible as String: accessible, + ]) + if status == errSecItemNotFound { + var attributes = target + attributes[kSecValueData as String] = data + attributes[kSecAttrAccessible as String] = accessible + status = items.add(attributes) } - let status = SecItemAdd(applyingAccessGroup(addQuery) as CFDictionary, nil) - if status != errSecSuccess { + guard status == errSecSuccess else { throw KeychainError.storeFailed(status) } + + guard synchronizable else { return } + let stale = items.delete(itemQuery(forKey: key, synchronizable: false)) + if stale != errSecSuccess, stale != errSecItemNotFound { + Self.logger.error("Removing the device-only copy of '\(key, privacy: .public)' failed: \(stale)") + } } + // With sync on, a newer device-only copy is a save made while sync was off; with sync off, this device's copy wins. func retrieve(forKey key: String) throws -> String? { - let query: [String: Any] = [ - kSecClass as String: kSecClassGenericPassword, - kSecAttrService as String: Self.serviceName, - kSecAttrAccount as String: key, - kSecReturnData as String: true, - kSecMatchLimit as String: kSecMatchLimitOne, - kSecAttrSynchronizable as String: kSecAttrSynchronizableAny, - kSecUseDataProtectionKeychain as String: true, - ] - - var result: AnyObject? - let status = SecItemCopyMatching(applyingAccessGroup(query) as CFDictionary, &result) + let preferred = Self.synchronizes(key, syncsPasswords: syncsPasswords()) + var found = items.copyItem(itemQuery(forKey: key, synchronizable: preferred)) + if found.status == errSecItemNotFound { + found = items.copyItem(itemQuery(forKey: key, synchronizable: !preferred)) + } else if found.status == errSecSuccess, preferred { + let other = items.copyItem(itemQuery(forKey: key, synchronizable: !preferred)) + if Self.isNewer(other, than: found) { found = other } + } - if status == errSecItemNotFound { return nil } - if status != errSecSuccess { - throw KeychainError.retrieveFailed(status) + if found.status == errSecItemNotFound { return nil } + if found.status != errSecSuccess { + throw KeychainError.retrieveFailed(found.status) } - guard let data = result as? Data else { return nil } + guard let data = found.data else { return nil } return String(data: data, encoding: .utf8) } + private static func isNewer(_ candidate: KeychainCopiedItem, than current: KeychainCopiedItem) -> Bool { + guard candidate.status == errSecSuccess, let candidateDate = candidate.modified, + let currentDate = current.modified else { return false } + return candidateDate > currentDate + } + func delete(forKey key: String) throws { - let query: [String: Any] = [ + var query = baseQuery(forKey: key) + query[kSecAttrSynchronizable as String] = kSecAttrSynchronizableAny + let status = items.delete(query) + if status != errSecSuccess && status != errSecItemNotFound { + throw KeychainError.deleteFailed(status) + } + } + + private func baseQuery(forKey key: String) -> [String: Any] { + applyingAccessGroup([ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: Self.serviceName, kSecAttrAccount as String: key, - kSecAttrSynchronizable as String: kSecAttrSynchronizableAny, kSecUseDataProtectionKeychain as String: true, - ] - let status = SecItemDelete(applyingAccessGroup(query) as CFDictionary) - if status != errSecSuccess && status != errSecItemNotFound { - throw KeychainError.deleteFailed(status) - } + ]) + } + + private func itemQuery(forKey key: String, synchronizable: Bool) -> [String: Any] { + var query = baseQuery(forKey: key) + query[kSecAttrSynchronizable as String] = synchronizable + return query } // Device-local items only: deleting a synced one here would remove it from the Mac that wrote it. diff --git a/apple/TableProMobile/TableProMobile/Platform/SessionTransactionMonitor.swift b/apple/TableProMobile/TableProMobile/Platform/SessionTransactionMonitor.swift index eceeace247..b74ed25335 100644 --- a/apple/TableProMobile/TableProMobile/Platform/SessionTransactionMonitor.swift +++ b/apple/TableProMobile/TableProMobile/Platform/SessionTransactionMonitor.swift @@ -16,14 +16,27 @@ final class SessionTransactionMonitor { let workGeneration: Int } + // What a session held when it ended. `duringWork`: a statement was running, so it may have ended the transaction. + nonisolated struct EndedHold: Sendable, Equatable { + let hold: Hold + let duringWork: Bool + } + private struct Entry { let driver: any DatabaseDriver var hold: Hold } + private struct EndedEntry { + let driver: any DatabaseDriver + let ended: EndedHold + } + private static let logger = Logger(subsystem: "com.TablePro", category: "SessionTransactionMonitor") private var entries: [UUID: Entry] = [:] + // Kept for a window still on the ended session, which says the transaction is gone once it redials. + @ObservationIgnored private var endedEntries: [UUID: EndedEntry] = [:] // Sessions whose last answer was idle, apart from those that could not say. @ObservationIgnored private var idleDrivers: [UUID: ObjectIdentifier] = [:] private var workCounts: [UUID: Int] = [:] @@ -47,6 +60,11 @@ final class SessionTransactionMonitor { return entry.hold } + func endedHold(for connectionId: UUID, on driver: (any DatabaseDriver)?) -> EndedHold? { + guard let driver, let entry = endedEntries[connectionId], entry.driver === driver else { return nil } + return entry.ended + } + func token(for connectionId: UUID, on driver: (any DatabaseDriver)?) -> HoldToken? { guard let driver, hold(for: connectionId, on: driver) != nil else { return nil } return HoldToken(driver: driver, workGeneration: workGenerations[connectionId, default: 0]) @@ -89,7 +107,7 @@ final class SessionTransactionMonitor { func refresh(_ connectionId: UUID) async { guard let driver = liveDriver(connectionId) else { - entries[connectionId] = nil + retireEntry(connectionId) return } let generation = refreshGenerations[connectionId, default: 0] + 1 @@ -133,13 +151,24 @@ final class SessionTransactionMonitor { func sessionEnded(_ connectionId: UUID, driver: any DatabaseDriver) { guard let entry = entries[connectionId], entry.driver === driver else { return } - entries[connectionId] = nil + retireEntry(connectionId) + } + + // Only for a session that is gone: its entry's driver can never be live again. + private func retireEntry(_ connectionId: UUID) { + guard let entry = entries.removeValue(forKey: connectionId) else { return } + endedEntries[connectionId] = EndedEntry( + driver: entry.driver, + ended: EndedHold(hold: entry.hold, duringWork: isRunningStatements(connectionId)) + ) } private func actionDriver(for connectionId: UUID, token: HoldToken) throws -> any DatabaseDriver { let live = liveDriver(connectionId) guard let live, live === token.driver, let entry = entries[connectionId], entry.driver === live else { - if let entry = entries[connectionId], entry.driver !== live { + if live == nil { + retireEntry(connectionId) + } else if let entry = entries[connectionId], entry.driver !== live { entries[connectionId] = nil } Self.logger.info("Refused a transaction action: the session that held it is gone") @@ -153,6 +182,8 @@ final class SessionTransactionMonitor { } private func apply(_ state: DriverTransactionState, from driver: any DatabaseDriver, to connectionId: UUID) { + // A live session answered, so the window that redialed it has already read what the ended one held. + endedEntries[connectionId] = nil idleDrivers[connectionId] = state == .idle ? ObjectIdentifier(driver) : nil switch state { case .explicitTransaction, .implicitTransaction: diff --git a/apple/TableProMobile/TableProMobile/Security/CertificateMaterialStore.swift b/apple/TableProMobile/TableProMobile/Security/CertificateMaterialStore.swift index 9f7051213f..a0a077f844 100644 --- a/apple/TableProMobile/TableProMobile/Security/CertificateMaterialStore.swift +++ b/apple/TableProMobile/TableProMobile/Security/CertificateMaterialStore.swift @@ -28,6 +28,11 @@ nonisolated protocol CertificateMaterialStoring: Sendable { nonisolated final class CertificateMaterialStore: CertificateMaterialStoring { private static let logger = Logger(subsystem: "com.TablePro", category: "CertificateMaterialStore") private let serviceName = "com.TablePro.clientCertificates" + private let items: any KeychainItemStore + + init(items: any KeychainItemStore = SystemKeychainItemStore()) { + self.items = items + } private func account(_ role: CertificateRole, _ connectionId: UUID) -> String { "\(connectionId.uuidString).\(role.rawValue)" @@ -38,21 +43,27 @@ nonisolated final class CertificateMaterialStore: CertificateMaterialStoring { kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: serviceName, kSecAttrAccount as String: account(role, connectionId), + kSecAttrSynchronizable as String: false, kSecUseDataProtectionKeychain as String: true, ] } + // Updated in place: a delete then a failed add would lose the certificate the connection already uses. func store(_ pem: String, role: CertificateRole, for connectionId: UUID) throws { guard let data = pem.data(using: .utf8) else { return } - SecItemDelete(baseQuery(role, connectionId) as CFDictionary) - - var addQuery = baseQuery(role, connectionId) - addQuery[kSecValueData as String] = data - addQuery[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly - addQuery[kSecAttrSynchronizable as String] = false - - let status = SecItemAdd(addQuery as CFDictionary, nil) + let target = baseQuery(role, connectionId) + let accessible = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly + var status = items.update(target, attributes: [ + kSecValueData as String: data, + kSecAttrAccessible as String: accessible, + ]) + if status == errSecItemNotFound { + var attributes = target + attributes[kSecValueData as String] = data + attributes[kSecAttrAccessible as String] = accessible + status = items.add(attributes) + } guard status == errSecSuccess else { Self.logger.error("Storing \(role.rawValue, privacy: .public) failed with \(status)") throw CertificateStoreError.writeFailed(status) @@ -60,18 +71,13 @@ nonisolated final class CertificateMaterialStore: CertificateMaterialStoring { } func pem(role: CertificateRole, for connectionId: UUID) -> String? { - var query = baseQuery(role, connectionId) - query[kSecReturnData as String] = true - query[kSecMatchLimit as String] = kSecMatchLimitOne - - var result: AnyObject? - guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, - let data = result as? Data else { return nil } + let found = items.copyItem(baseQuery(role, connectionId)) + guard found.status == errSecSuccess, let data = found.data else { return nil } return String(data: data, encoding: .utf8) } func delete(role: CertificateRole, for connectionId: UUID) { - SecItemDelete(baseQuery(role, connectionId) as CFDictionary) + _ = items.delete(baseQuery(role, connectionId)) } func deleteAll(for connectionId: UUID) { diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorBackgroundTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorBackgroundTests.swift new file mode 100644 index 0000000000..3011a5fb98 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorBackgroundTests.swift @@ -0,0 +1,134 @@ +import Foundation +import TableProDatabase +@testable import TableProMobile +import TableProModels +import Testing + +@MainActor +@Suite("Connection coordinator after the background", .timeLimit(.minutes(1))) +struct ConnectionCoordinatorBackgroundTests { + private let fixture: AppStateFixture + private let factory = StubDriverFactory() + private let appState: AppState + private let connection = DatabaseConnection(name: "Ledger", type: .duckdb, host: "", port: 0, database: "ledger.duckdb") + + init() throws { + fixture = try AppStateFixture() + appState = fixture.makeState(syncEnabled: false, driverFactory: factory) + } + + private var monitor: SessionTransactionMonitor { appState.sessionTransactions } + + private func fileDriver(_ state: DriverTransactionState) -> MockDatabaseDriver { + let driver = MockDatabaseDriver() + driver.scriptedTables = [TableInfo(name: "entries")] + driver.scriptedTransactionState = state + driver.holdsSuspensionBlockingResource = true + return driver + } + + private func connectedCoordinator(on driver: MockDatabaseDriver) async throws -> ConnectionCoordinator { + factory.queue(driver) + _ = try await appState.connectionManager.connect(connection) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + await coordinator.connect() + return coordinator + } + + // What the app runs at `.background`, then the session's end reaching the monitor. + private func releaseForTheBackground() async { + let released = await appState.connectionManager.releaseSuspensionBlockingResources() + #expect(released == [connection.id]) + await ObservedCondition.wait { monitor.hold(for: connection.id) == nil } + } + + @Test("A transaction open when the app went to the background is reported once the file reopens") + func backgroundCloseReportsTheTransaction() async throws { + let coordinator = try await connectedCoordinator(on: fileDriver(.explicitTransaction)) + #expect(coordinator.transactionHold == .open) + + await releaseForTheBackground() + let reopened = fileDriver(.idle) + factory.queue(reopened) + await coordinator.reconnectIfNeeded() + + #expect(coordinator.session?.driver === reopened) + #expect(coordinator.showFailureAlert) + #expect( + coordinator.failureAlertMessage + == ConnectionCoordinator.lostTransactionMessage(.inBackground, connectionName: "Ledger") + ) + #expect(monitor.rollBackCount(for: connection.id) == 1) + } + + @Test("A file session with nothing open reopens after the background without an alert") + func idleBackgroundCloseIsQuiet() async throws { + let coordinator = try await connectedCoordinator(on: fileDriver(.idle)) + + await releaseForTheBackground() + let reopened = fileDriver(.idle) + factory.queue(reopened) + await coordinator.reconnectIfNeeded() + + #expect(coordinator.session?.driver === reopened) + #expect(!coordinator.showFailureAlert) + #expect(monitor.rollBackCount(for: connection.id) == 0) + } + + @Test("A statement still running when the app went to the background leaves the outcome unknown, in a file's terms") + func backgroundCloseDuringWorkIsAmbiguous() async throws { + let coordinator = try await connectedCoordinator(on: fileDriver(.explicitTransaction)) + let gate = TestGate() + let work = Task { await coordinator.track { await gate.enter() } } + await gate.waitUntilEntered() + + await releaseForTheBackground() + await gate.open() + await work.value + factory.queue(fileDriver(.idle)) + await coordinator.reconnectIfNeeded() + + let message = try #require(coordinator.failureAlertMessage) + #expect(message == ConnectionCoordinator.lostTransactionMessage(.inBackgroundAfterStatement, connectionName: "Ledger")) + #expect(message != ConnectionCoordinator.lostTransactionMessage(.duringWork, connectionName: "Ledger")) + } + + @Test("A statement on the closed file while the window checks the session leaves the roll back certain") + func statementAfterTheReturnKeepsTheRollBackCertain() async throws { + let coordinator = try await connectedCoordinator(on: fileDriver(.explicitTransaction)) + await releaseForTheBackground() + + let gate = TestGate() + let work = Task { await coordinator.track { await gate.enter() } } + await gate.waitUntilEntered() + factory.queue(fileDriver(.idle)) + await coordinator.reconnectIfNeeded() + await gate.open() + await work.value + + #expect( + coordinator.failureAlertMessage + == ConnectionCoordinator.lostTransactionMessage(.inBackground, connectionName: "Ledger") + ) + } + + @Test("A reopen that fails keeps the loss and reports it when a later reopen lands") + func failedReopenReportsLater() async throws { + let coordinator = try await connectedCoordinator(on: fileDriver(.explicitTransaction)) + + await releaseForTheBackground() + let refused = fileDriver(.idle) + refused.connectError = MockDatabaseDriver.MockError.scripted + factory.queue(refused) + await coordinator.reconnectIfNeeded() + #expect(!coordinator.showFailureAlert) + + factory.queue(fileDriver(.idle)) + await coordinator.reconnectIfNeeded() + + #expect( + coordinator.failureAlertMessage + == ConnectionCoordinator.lostTransactionMessage(.inBackground, connectionName: "Ledger") + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift index 25070de66c..82c0fd9fc6 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift @@ -1,13 +1,17 @@ import CRedis import Darwin import Foundation +import os import TableProTLSTestFixtures // Loopback sessions that accept any login. With a certificate it upgrades after the client's SSL // request; with `asksForCleartext` it answers the login with a switch to mysql_clear_password, as RDS // does; with `asksForFullSHA2Auth` it switches to caching_sha2_password and asks for full authentication, // which a client sends as the typed password once it thinks the session is secure; with `breaksHandshake` -// it answers the SSL request with bytes that are not TLS. +// it answers the SSL request with bytes that are not TLS. A query starting with `holdsQueriesStarting` waits +// up to `holdSeconds` for `KILL QUERY ` from another session, then fails with 1317 as a killed +// statement does, or with `killedQueriesComplete` succeeds as one that finished before the kill landed. Unkilled, +// or after `release`, it succeeds. A cleartext password passed to `refuse` is answered with 1045, as an expired token is. final class FakeMySQLServer: @unchecked Sendable { struct SetupFailure: Error {} @@ -29,6 +33,7 @@ final class FakeMySQLServer: @unchecked Sendable { var loggedIn = false var queries: [String] = [] var quit = false + var connectionId: UInt32 = 0 func sawInPlaintext(_ secret: String) -> Bool { let needle = Array(secret.utf8) @@ -63,25 +68,53 @@ final class FakeMySQLServer: @unchecked Sendable { private static let switchToSHA2: [UInt8] = [0xFE] + Array("caching_sha2_password".utf8) + [0] + Array("abcdefghijklmnopqrst".utf8) + [0] private static let performFullAuthentication: [UInt8] = [0x01, 0x04] + // ER_QUERY_INTERRUPTED (1317), SQLSTATE 70100. + private static let interruptedPacket: [UInt8] = [0xFF, 0x25, 0x05] + Array("#70100".utf8) + + Array("Query execution was interrupted".utf8) + // ER_ACCESS_DENIED_ERROR (1045), SQLSTATE 28000. + private static let accessDeniedPacket: [UInt8] = [0xFF, 0x15, 0x04] + Array("#28000".utf8) + + Array("Access denied for user (using password: YES)".utf8) let port: Int private let listener: Int32 private let asksForCleartext: Bool private let asksForFullSHA2Auth: Bool private let breaksHandshake: Bool + private let heldPrefix: String? + private let holdSeconds: TimeInterval + private let killedQueriesComplete: Bool private let fixtures: URL? + private let kills = KillBoard() + private let refusedPasswords = OSAllocatedUnfairLock>(initialState: []) var offersTLS: Bool { fixtures != nil || breaksHandshake } + var heldQueries: Int { kills.heldQueries } + + // Lets a held query on that session answer as completed. + func release(_ connectionId: UInt32) { + kills.release(connectionId) + } + + func refuse(_ password: String) { + refusedPasswords.withLock { _ = $0.insert(password) } + } + init( identity: Identity?, asksForCleartext: Bool = false, asksForFullSHA2Auth: Bool = false, - breaksHandshake: Bool = false + breaksHandshake: Bool = false, + holdsQueriesStarting heldPrefix: String? = nil, + holdSeconds: TimeInterval = 10, + killedQueriesComplete: Bool = false ) throws { self.asksForCleartext = asksForCleartext self.asksForFullSHA2Auth = asksForFullSHA2Auth self.breaksHandshake = breaksHandshake + self.heldPrefix = heldPrefix + self.holdSeconds = holdSeconds + self.killedQueriesComplete = killedQueriesComplete if let identity { let directory = FileManager.default.temporaryDirectory .appendingPathComponent("FakeMySQLServer-\(UUID().uuidString)", isDirectory: true) @@ -143,14 +176,38 @@ final class FakeMySQLServer: @unchecked Sendable { func serve(sessions count: Int) async -> [Session] { await withCheckedContinuation { continuation in DispatchQueue(label: "com.TablePro.tests.fake-mysql").async { - continuation.resume(returning: (0.. [Session] { + await withCheckedContinuation { continuation in + DispatchQueue(label: "com.TablePro.tests.fake-mysql.accept").async { + let served = SessionList(count: count) + let group = DispatchGroup() + for index in 0.. Session { - var session = Session() - guard let client = acceptClient() else { return session } + private func runSession(on client: Int32, connectionId: UInt32) -> Session { + var session = Session(connectionId: connectionId) let channel = ServerChannel(socket: client) serve(on: channel, into: &session) session.plaintext = channel.plaintext @@ -159,7 +216,8 @@ final class FakeMySQLServer: @unchecked Sendable { } private func serve(on channel: ServerChannel, into session: inout Session) { - guard channel.send(handshake, sequence: 0), var login = channel.readPacket() else { return } + guard channel.send(handshake(connectionId: session.connectionId), sequence: 0), + var login = channel.readPacket() else { return } if offersTLS, login.payload.count == Self.sslRequestLength { session.askedForTLS = true @@ -193,8 +251,13 @@ final class FakeMySQLServer: @unchecked Sendable { guard channel.send(Self.switchToCleartext, sequence: sequence &+ 1), let answer = channel.readPacket() else { return } - session.cleartextPassword = String(decoding: answer.payload.prefix { $0 != 0 }, as: UTF8.self) + let password = String(decoding: answer.payload.prefix { $0 != 0 }, as: UTF8.self) + session.cleartextPassword = password sequence = answer.sequence + if refusedPasswords.withLock({ $0.contains(password) }) { + _ = channel.send(Self.accessDeniedPacket, sequence: sequence &+ 1) + return + } } guard channel.send(Self.okPacket, sequence: sequence &+ 1) else { return } session.loggedIn = true @@ -204,14 +267,28 @@ final class FakeMySQLServer: @unchecked Sendable { session.quit = true return } + var reply = Self.okPacket if command.payload.first == Self.comQuery { - session.queries.append(String(decoding: command.payload.dropFirst(), as: UTF8.self)) + let query = String(decoding: command.payload.dropFirst(), as: UTF8.self) + session.queries.append(query) + if let killed = Self.killedSession(in: query) { + kills.kill(killed) + } else if let heldPrefix, query.hasPrefix(heldPrefix), + kills.hold(session.connectionId, seconds: holdSeconds) == .killed, !killedQueriesComplete { + reply = Self.interruptedPacket + } } - guard channel.send(Self.okPacket, sequence: command.sequence &+ 1) else { return } + guard channel.send(reply, sequence: command.sequence &+ 1) else { return } } } - private var handshake: [UInt8] { + private static func killedSession(in query: String) -> UInt32? { + let words = query.split(separator: " ") + guard words.count == 3, words[0] == "KILL", words[1] == "QUERY" else { return nil } + return UInt32(words[2]) + } + + private func handshake(connectionId: UInt32) -> [UInt8] { var capabilities = Capability.longPassword | Capability.connectWithDB | Capability.protocol41 | Capability.transactions | Capability.secureConnection | Capability.multiResults | Capability.pluginAuth if offersTLS { @@ -219,7 +296,7 @@ final class FakeMySQLServer: @unchecked Sendable { } var payload: [UInt8] = [10] payload += Array("8.0.36".utf8) + [0] - payload += [1, 0, 0, 0] + payload += (0..<4).map { UInt8((connectionId >> ($0 * 8)) & 0xFF) } payload += Array("abcdefgh".utf8) + [0] payload += [UInt8(capabilities & 0xFF), UInt8((capabilities >> 8) & 0xFF)] payload += [45, 0x02, 0x00] @@ -230,9 +307,9 @@ final class FakeMySQLServer: @unchecked Sendable { return payload } - private func acceptClient() -> Int32? { + private func acceptClient(within milliseconds: Int32 = ServerChannel.pollMilliseconds) -> Int32? { var pollFD = pollfd(fd: listener, events: Int16(POLLIN), revents: 0) - guard poll(&pollFD, 1, ServerChannel.pollMilliseconds) > 0 else { return nil } + guard poll(&pollFD, 1, milliseconds) > 0 else { return nil } let client = accept(listener, nil, nil) guard client >= 0 else { return nil } var noSignal: Int32 = 1 @@ -243,6 +320,66 @@ final class FakeMySQLServer: @unchecked Sendable { return client } + // Where a held statement and the kill from another session meet. + private final class KillBoard: @unchecked Sendable { + enum HoldEnd { + case killed + case released + case timedOut + } + + private let condition = NSCondition() + private var ends: [UInt32: HoldEnd] = [:] + private var holds = 0 + + var heldQueries: Int { + condition.lock() + defer { condition.unlock() } + return holds + } + + func kill(_ connectionId: UInt32) { + end(connectionId, as: .killed) + } + + func release(_ connectionId: UInt32) { + end(connectionId, as: .released) + } + + func hold(_ connectionId: UInt32, seconds: TimeInterval) -> HoldEnd { + condition.lock() + defer { condition.unlock() } + holds += 1 + let deadline = Date(timeIntervalSinceNow: seconds) + while ends[connectionId] == nil, condition.wait(until: deadline) {} + return ends.removeValue(forKey: connectionId) ?? .timedOut + } + + private func end(_ connectionId: UInt32, as end: HoldEnd) { + condition.lock() + ends[connectionId] = end + condition.broadcast() + condition.unlock() + } + } + + private final class SessionList: @unchecked Sendable { + private let lock = NSLock() + private var list: [Session] + + init(count: Int) { + list = Array(repeating: Session(), count: count) + } + + var sessions: [Session] { + lock.withLock { list } + } + + func set(_ session: Session, at index: Int) { + lock.withLock { list[index] = session } + } + } + // Reads and writes MySQL packets on the socket, or inside TLS once `startTLS` succeeds. private final class ServerChannel { static let pollMilliseconds: Int32 = 10_000 diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift new file mode 100644 index 0000000000..4f1d047357 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift @@ -0,0 +1,176 @@ +import Darwin +import Foundation + +// A loopback PostgreSQL server for one plaintext session: it refuses SSL and GSS encryption, accepts any startup without +// a password, and answers every simple query with an empty completion. +final class FakePostgreSQLServer: @unchecked Sendable { + struct SetupFailure: Error {} + + struct Session: Sendable { + var startedUp = false + var queries: [String] = [] + var terminated = false + } + + private static let protocolVersion: UInt32 = 196_608 + private static let sslRequest: UInt32 = 80_877_103 + private static let gssEncryptionRequest: UInt32 = 80_877_104 + private static let pollMilliseconds: Int32 = 10_000 + private static let parameters: [(String, String)] = [ + ("server_version", "16.4"), + ("server_encoding", "UTF8"), + ("client_encoding", "UTF8"), + ("standard_conforming_strings", "on"), + ("DateStyle", "ISO, MDY"), + ("integer_datetimes", "on"), + ("TimeZone", "UTC"), + ] + + let port: Int + private let listener: Int32 + + init() throws { + let socketFD = socket(AF_INET, SOCK_STREAM, 0) + guard socketFD >= 0 else { throw SetupFailure() } + var option: Int32 = 1 + setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &option, socklen_t(MemoryLayout.size)) + + var address = sockaddr_in() + address.sin_family = sa_family_t(AF_INET) + address.sin_port = 0 + address.sin_addr.s_addr = inet_addr("127.0.0.1") + let bound = withUnsafePointer(to: &address) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { + bind(socketFD, $0, socklen_t(MemoryLayout.size)) + } + } + guard bound == 0, Darwin.listen(socketFD, 4) == 0 else { + Darwin.close(socketFD) + throw SetupFailure() + } + + var boundAddress = sockaddr_in() + var length = socklen_t(MemoryLayout.size) + let named = withUnsafeMutablePointer(to: &boundAddress) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { getsockname(socketFD, $0, &length) } + } + guard named == 0 else { + Darwin.close(socketFD) + throw SetupFailure() + } + listener = socketFD + port = Int(boundAddress.sin_port.bigEndian) + } + + func close() { + Darwin.close(listener) + } + + func serveOneSession() async -> Session { + await withCheckedContinuation { continuation in + DispatchQueue(label: "com.TablePro.tests.fake-postgresql").async { + continuation.resume(returning: self.runSession()) + } + } + } + + private func runSession() -> Session { + var session = Session() + guard let client = acceptClient() else { return session } + defer { Darwin.close(client) } + guard startUp(client) else { return session } + session.startedUp = true + + while let type = receive(client, 1)?.first, let body = receiveBody(client) { + switch type { + case UInt8(ascii: "Q"): + session.queries.append(String(decoding: body.prefix { $0 != 0 }, as: UTF8.self)) + guard send(client, Self.message("C", Array("SELECT 0".utf8) + [0])), + send(client, Self.readyForQuery) else { return session } + case UInt8(ascii: "X"): + session.terminated = true + return session + default: + return session + } + } + return session + } + + // The client may ask for SSL or GSS encryption first; each gets "N" and the real startup follows. + private func startUp(_ client: Int32) -> Bool { + while let packet = receiveBody(client), packet.count >= 4 { + let code = Self.uint32(packet.prefix(4)) + if code == Self.sslRequest || code == Self.gssEncryptionRequest { + guard send(client, [UInt8(ascii: "N")]) else { return false } + continue + } + guard code == Self.protocolVersion else { return false } + var reply = Self.message("R", Self.bytes(UInt32(0))) + for (name, value) in Self.parameters { + reply += Self.message("S", Array(name.utf8) + [0] + Array(value.utf8) + [0]) + } + reply += Self.message("K", Self.bytes(UInt32(4_242)) + Self.bytes(UInt32(7_777))) + reply += Self.readyForQuery + return send(client, reply) + } + return false + } + + private static var readyForQuery: [UInt8] { + message("Z", [UInt8(ascii: "I")]) + } + + private static func message(_ type: Character, _ body: [UInt8]) -> [UInt8] { + [type.asciiValue ?? 0] + bytes(UInt32(body.count + 4)) + body + } + + private static func bytes(_ value: UInt32) -> [UInt8] { + [UInt8((value >> 24) & 0xFF), UInt8((value >> 16) & 0xFF), UInt8((value >> 8) & 0xFF), UInt8(value & 0xFF)] + } + + private static func uint32(_ bytes: C) -> UInt32 where C.Element == UInt8 { + bytes.reduce(0) { $0 << 8 | UInt32($1) } + } + + // A length that counts its own four bytes, then that many minus four. + private func receiveBody(_ client: Int32) -> [UInt8]? { + guard let header = receive(client, 4) else { return nil } + let length = Int(Self.uint32(header)) + guard length >= 4 else { return nil } + return length == 4 ? [] : receive(client, length - 4) + } + + private func acceptClient() -> Int32? { + var pollFD = pollfd(fd: listener, events: Int16(POLLIN), revents: 0) + guard poll(&pollFD, 1, Self.pollMilliseconds) > 0 else { return nil } + let client = accept(listener, nil, nil) + guard client >= 0 else { return nil } + var noSignal: Int32 = 1 + setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &noSignal, socklen_t(MemoryLayout.size)) + return client + } + + private func receive(_ client: Int32, _ count: Int) -> [UInt8]? { + var bytes: [UInt8] = [] + while bytes.count < count { + var pollFD = pollfd(fd: client, events: Int16(POLLIN), revents: 0) + guard poll(&pollFD, 1, Self.pollMilliseconds) > 0 else { return nil } + var chunk = [UInt8](repeating: 0, count: count - bytes.count) + let received = recv(client, &chunk, chunk.count, 0) + guard received > 0 else { return nil } + bytes += chunk.prefix(received) + } + return bytes + } + + private func send(_ client: Int32, _ bytes: [UInt8]) -> Bool { + var offset = 0 + while offset < bytes.count { + let sent = bytes[offset...].withUnsafeBytes { Darwin.send(client, $0.baseAddress, $0.count, 0) } + guard sent > 0 else { return false } + offset += sent + } + return true + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillSignInTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillSignInTests.swift new file mode 100644 index 0000000000..78d8b3e6eb --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillSignInTests.swift @@ -0,0 +1,124 @@ +import Foundation +import os +@testable import TableProMobile +import TableProTestSupport +import Testing + +struct MySQLKillSignInTests { + @Test("A young session token serves every kill without a mint") + func youngTokenIsReused() async { + let clock = ManualClock() + let mints = OSAllocatedUnfairLock(initialState: 0) + let signIn = MySQLKillSignIn( + password: "connect-token", + refresh: { mints.withLock { $0 += 1 }; return "fresh-token" }, + clock: clock + ) + + clock.advance(by: .seconds(599)) + let first = await Self.credential(signIn) + let second = await Self.credential(signIn) + let minted = mints.withLock { $0 } + + #expect(first == MySQLKillSignIn.Credential(password: "connect-token", isReused: true)) + #expect(second == first) + #expect(minted == 0) + } + + @Test("A reused token the server refuses is replaced by one mint, which later kills reuse") + func refusedTokenIsMintedOnce() async { + let clock = ManualClock() + let mints = OSAllocatedUnfairLock(initialState: 0) + let signIn = MySQLKillSignIn( + password: "connect-token", + refresh: { "fresh-\(mints.withLock { $0 += 1; return $0 })" }, + clock: clock + ) + + let refused = await Self.credential(signIn) + let replacement = await Self.afterRefusal(of: refused.password, signIn) + let staleRefusal = await Self.afterRefusal(of: refused.password, signIn) + let next = await Self.credential(signIn) + + #expect(replacement == "fresh-1") + #expect(staleRefusal == "fresh-1") + #expect(next == MySQLKillSignIn.Credential(password: "fresh-1", isReused: true)) + } + + @Test("A refused token with a mint that fails leaves the kill nothing to try again") + func refusalWithFailedMint() async { + let signIn = MySQLKillSignIn( + password: "connect-token", refresh: { throw MintFailure() }, clock: ManualClock() + ) + + let replacement = await Self.afterRefusal(of: "connect-token", signIn) + + #expect(replacement == nil) + } + + private struct MintFailure: Error {} + + @Test("Past ten minutes a kill mints once, and later kills reuse that token for ten more") + func oldTokenIsMintedOnceThenReused() async { + let clock = ManualClock() + let mints = OSAllocatedUnfairLock(initialState: 0) + let signIn = MySQLKillSignIn( + password: "connect-token", + refresh: { "fresh-\(mints.withLock { $0 += 1; return $0 })" }, + clock: clock + ) + + clock.advance(by: .seconds(600)) + let pastWindow = await Self.credential(signIn) + clock.advance(by: .seconds(300)) + let reused = await Self.credential(signIn) + clock.advance(by: .seconds(300)) + let mintedAgain = await Self.credential(signIn) + + #expect(pastWindow == MySQLKillSignIn.Credential(password: "fresh-1", isReused: false)) + #expect(reused == MySQLKillSignIn.Credential(password: "fresh-1", isReused: true)) + #expect(mintedAgain == MySQLKillSignIn.Credential(password: "fresh-2", isReused: false)) + } + + @Test("A mint that yields nothing keeps the session token, and the next kill tries again") + func emptyMintRetries() async { + let clock = ManualClock() + let mints = OSAllocatedUnfairLock(initialState: 0) + let signIn = MySQLKillSignIn( + password: "connect-token", + refresh: { mints.withLock { $0 += 1; return $0 } == 1 ? nil : "fresh-token" }, + clock: clock + ) + + clock.advance(by: .seconds(601)) + let afterEmpty = await Self.credential(signIn) + let afterRetry = await Self.credential(signIn) + + #expect(afterEmpty == MySQLKillSignIn.Credential(password: "connect-token", isReused: false)) + #expect(afterRetry.password == "fresh-token") + } + + @Test("A password session signs every kill in with its password") + func passwordSessionNeverMints() async { + let clock = ManualClock() + let signIn = MySQLKillSignIn(password: "app-password", refresh: nil, clock: clock) + + clock.advance(by: .seconds(3_600)) + let credential = await Self.credential(signIn) + + #expect(credential == MySQLKillSignIn.Credential(password: "app-password", isReused: false)) + } + + // Off the Swift concurrency threads, as the kill queue calls them. + private static func credential(_ signIn: MySQLKillSignIn) async -> MySQLKillSignIn.Credential { + await withCheckedContinuation { continuation in + DispatchQueue.global().async { continuation.resume(returning: signIn.credential()) } + } + } + + private static func afterRefusal(of password: String, _ signIn: MySQLKillSignIn) async -> String? { + await withCheckedContinuation { continuation in + DispatchQueue.global().async { continuation.resume(returning: signIn.passwordAfterRefusal(of: password)) } + } + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillTokenTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillTokenTests.swift new file mode 100644 index 0000000000..486e0366e2 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLKillTokenTests.swift @@ -0,0 +1,78 @@ +import Foundation +import TableProDatabase +@testable import TableProMobile +import TableProModels +import TableProTestSupport +import Testing + +// The factory gets the session's refresh from ConnectionManager, which resolves it for the connection as saved, so a +// session behind a tunnel mints for the real endpoint too. Against the TLS fake, which records each cleartext sign-in. +// Each session is aged past the ten minutes its own token serves kills for. +@Suite(.timeLimit(.minutes(1))) +struct MySQLKillTokenTests { + private static let held = "UPDATE orders SET archived = 1 WHERE placed_at < '2020-01-01'" + + @Test("An IAM session's kill signs in with the token the manager's refresh mints") + func iamKillUsesTheRefresh() async throws { + let served = try await killSessions( + fields: [IAMSignIn.Field.method: IAMSignIn.awsAccessKey.pickerValue], + preTunnelHost: nil + ) + + #expect(served[0].cleartextPassword == "connect-token") + #expect(served[1].cleartextPassword == "kill-token") + #expect(served[1].queries == ["KILL QUERY 1"]) + } + + @Test("Behind a tunnel the kill still signs in with the refreshed token") + func tunneledKillUsesTheRefresh() async throws { + let served = try await killSessions( + fields: [IAMSignIn.Field.method: IAMSignIn.awsAccessKey.pickerValue], + preTunnelHost: "mydb.abc123.us-east-1.rds.amazonaws.com" + ) + + #expect(served[1].cleartextPassword == "kill-token") + } + + @Test("A password session's kill signs in with its password, without calling the refresh") + func passwordKillKeepsThePassword() async throws { + let served = try await killSessions(fields: [:], preTunnelHost: nil) + + #expect(served[1].cleartextPassword == "connect-token") + #expect(served[1].queries == ["KILL QUERY 1"]) + } + + private func killSessions(fields: [String: String], preTunnelHost: String?) async throws -> [FakeMySQLServer.Session] { + let server = try FakeMySQLServer(identity: .localhost, asksForCleartext: true, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let connection = DatabaseConnection( + type: .mysql, + host: "127.0.0.1", + port: server.port, + username: "iam_user", + additionalFields: fields, + sslEnabled: true + ) + let clock = ManualClock() + let created = try IOSDriverFactory(clock: clock).createDriver( + for: connection, + password: "connect-token", + preTunnelHost: preTunnelHost, + refreshPassword: { "kill-token" } + ) + let driver = try #require(created as? MySQLDriver) + try await driver.connect() + clock.advance(by: MySQLKillSignIn.reuseWindow) + + let reader = Task { + for try await _ in driver.executeStreaming(query: Self.held, options: StreamOptions(maxRows: 100)) {} + } + try await waitUntil { server.heldQueries == 1 } + reader.cancel() + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + return await sessions + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLQueryTimeoutTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLQueryTimeoutTests.swift new file mode 100644 index 0000000000..702f1e1891 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLQueryTimeoutTests.swift @@ -0,0 +1,92 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct MySQLQueryTimeoutTests { + @Test("Without a query timeout a statement has no socket read timeout to fail on") + func noQueryTimeoutNoSocketTimeout() { + #expect(Self.driver(queryTimeoutSeconds: nil).socketTimeoutSeconds == 0) + #expect(Self.driver(queryTimeoutSeconds: 0).socketTimeoutSeconds == 0) + } + + @Test("A query timeout keeps the socket open past the server's own limit") + func socketTimeoutOutlastsQueryTimeout() { + #expect(Self.driver(queryTimeoutSeconds: 45).socketTimeoutSeconds == 75) + } + + @Test( + "The factory hands the connection's query timeout to the MySQL driver", + arguments: [DatabaseType.mysql, .mariadb, .tidb, .oceanbase] + ) + func factoryPassesQueryTimeout(type: DatabaseType) throws { + let timed = DatabaseConnection(name: "Shop", type: type, host: "db.example.com", queryTimeoutSeconds: 45) + let untimed = DatabaseConnection(name: "Shop", type: type, host: "db.example.com") + + let timedDriver = try #require(try IOSDriverFactory().createDriver(for: timed, password: nil) as? MySQLDriver) + let untimedDriver = try #require(try IOSDriverFactory().createDriver(for: untimed, password: nil) as? MySQLDriver) + + #expect(timedDriver.queryTimeoutSeconds == 45) + #expect(timedDriver.socketTimeoutSeconds == 75) + #expect(untimedDriver.socketTimeoutSeconds == 0) + } + + @Test("A query timeout reaches the server as the session's statement limit") + func queryTimeoutReachesServer() async throws { + let server = try FakeMySQLServer(identity: nil) + defer { server.close() } + async let session = server.serveOneSession() + + let driver = Self.driver(queryTimeoutSeconds: 45, port: server.port) + try await driver.connect() + try await driver.disconnect() + + let served = await session + #expect(served.queries.contains("SET SESSION max_execution_time = 45000")) + } + + @Test("Without a query timeout the session keeps the server's own limit") + func noQueryTimeoutSendsNoLimit() async throws { + let server = try FakeMySQLServer(identity: nil) + defer { server.close() } + async let session = server.serveOneSession() + + let driver = Self.driver(queryTimeoutSeconds: nil, port: server.port) + try await driver.connect() + try await driver.disconnect() + + let served = await session + let setsLimit = served.queries.contains { $0.contains("max_execution_time") } + #expect(served.loggedIn) + #expect(!setsLimit) + } + + @Test("Each engine gets its own statement limit") + func statementLimitPerEngine() { + #expect( + MySQLDriver.sessionSetupStatements(for: .mysql, banner: "8.4.11", queryTimeoutSeconds: 45) + == ["SET SESSION max_execution_time = 45000"] + ) + #expect( + MySQLDriver.sessionSetupStatements(for: .mariadb, banner: "10.6.28-MariaDB", queryTimeoutSeconds: 45) + == ["SET SESSION max_statement_time = 45"] + ) + #expect( + MySQLDriver.sessionSetupStatements(for: .tidb, banner: "8.0.11-TiDB-v8.5.0", queryTimeoutSeconds: 45) + == ["SET SESSION max_execution_time = 45000"] + ) + #expect( + MySQLDriver.sessionSetupStatements(for: .oceanbase, banner: nil, queryTimeoutSeconds: 45) + == ["SET SESSION ob_query_timeout = 45000000", "SET SESSION max_execution_time = 0"] + ) + #expect(MySQLDriver.sessionSetupStatements(for: .mysql, banner: "8.4.11", queryTimeoutSeconds: 0).isEmpty) + } + + private static func driver(queryTimeoutSeconds: Int?, port: Int = 3_306) -> MySQLDriver { + MySQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "", + queryTimeoutSeconds: queryTimeoutSeconds + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLSessionSocketTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLSessionSocketTests.swift new file mode 100644 index 0000000000..c6c05de8b3 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLSessionSocketTests.swift @@ -0,0 +1,53 @@ +import Foundation +@testable import TableProMobile +import TableProTestSupport +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct MySQLSessionSocketTests { + @Test("A session without a query timeout has no read timeout, and its keepalive fails a dead path in about 90 s") + func deadPathFailsOnTheKeepalive() async throws { + let server = try FakeMySQLServer(identity: nil) + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port, queryTimeoutSeconds: nil) + try await driver.connect() + + let socket = try #require(SessionSocketProbe.clientSocket(connectedTo: server.port)) + let readTimeout = SessionSocketProbe.receiveTimeout(socket) + let keepalive = SessionSocketProbe.keepalive(socket) + try await driver.disconnect() + _ = await session + + #expect(readTimeout == 0) + #expect(keepalive == SessionSocketProbe.expected) + } + + @Test("The setup statements keep the sign-in's 30 s bound, and the session takes its own after them") + func setupKeepsTheSignInBound() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: "SET SESSION max_execution_time") + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port, queryTimeoutSeconds: 45) + + let connecting = Task { try await driver.connect() } + try await waitUntil { server.heldQueries == 1 } + let socket = try #require(SessionSocketProbe.clientSocket(connectedTo: server.port)) + let duringSetup = SessionSocketProbe.receiveTimeout(socket) + server.release(1) + try await connecting.value + let afterSetup = SessionSocketProbe.receiveTimeout(socket) + try await driver.disconnect() + _ = await session + + #expect(duringSetup == 30) + #expect(afterSetup == 75) + } + + private static func driver(port: Int, queryTimeoutSeconds: Int?) -> MySQLDriver { + MySQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "", + queryTimeoutSeconds: queryTimeoutSeconds + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLStopTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLStopTests.swift new file mode 100644 index 0000000000..f900e8766e --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLStopTests.swift @@ -0,0 +1,418 @@ +import Foundation +import os +import TableProDatabase +@testable import TableProMobile +import TableProModels +import TableProTestSupport +import Testing + +// The fake server runs each session on its own thread: the first holds the statement, the second is the connection +// the kill arrives on. Its handshake gives the first session thread id 1. Stop is driven as the editor does it: the +// reader leaves its stream, then the driver is asked to cancel. +@Suite(.timeLimit(.minutes(1))) +struct MySQLStopTests { + private static let held = "UPDATE orders SET archived = 1 WHERE placed_at < '2020-01-01'" + private static let queuedDelete = "DELETE FROM orders WHERE archived = 1" + private static let options = StreamOptions(maxRows: 100) + + @Test("Stop sends KILL QUERY for the session's thread over a second connection") + func stopKillsOnServer() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let reader = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + try await Self.stop(reader, on: driver) + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(Array(served[0].queries.suffix(2)) == [Self.held, "SELECT 2"]) + } + + @Test("A reader that stops kills its own statement on the server") + func stoppedReaderKills() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let reader = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + reader.cancel() + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(Array(served[0].queries.suffix(2)) == [Self.held, "SELECT 2"]) + } + + @Test("Stop never kills a statement another surface is still reading") + func stopLeavesAnotherReadersStatement() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held, holdSeconds: 2) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2, acceptingWithin: 5_000) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let browsing = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + let editor = Task { try await Self.read(driver.executeStreaming(query: Self.queuedDelete, options: Self.options)) } + try await Self.stop(editor, on: driver) + + try await browsing.value + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries.isEmpty) + #expect(Array(served[0].queries.suffix(2)) == [Self.held, "SELECT 2"]) + } + + @Test("On TiDB a reader that leaves sends no kill, which would drop the session once the statement ended") + func tidbReaderExitSendsNoKill() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held, holdSeconds: 2) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2, acceptingWithin: 5_000) + let driver = Self.driver(port: server.port, databaseType: .tidb) + try await driver.connect() + + let page = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + page.cancel() + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries.isEmpty) + #expect(Array(served[0].queries.suffix(2)) == [Self.held, "SELECT 2"]) + } + + @Test("On TiDB Stop still kills the statement") + func tidbStopKills() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port, databaseType: .tidb) + try await driver.connect() + + let reader = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + try await Self.stop(reader, on: driver) + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(Array(served[0].queries.suffix(2)) == [Self.held, "SELECT 2"]) + } + + @Test("A kill that lands after its statement finished is taken by a throwaway statement, not the next one") + func latchedKillIsAbsorbed() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held, killedQueriesComplete: true) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let reader = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + try await Self.stop(reader, on: driver) + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(Array(served[0].queries.suffix(3)) == [Self.held, "SELECT 1", "SELECT 2"]) + } + + @Test("A statement whose reader left while it waited for the session is never sent") + func abandonedQueuedStatementIsNeverSent() async throws { + let server = try FakeMySQLServer(identity: nil, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let running = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + let queued = Task { try await Self.read(driver.executeStreaming(query: Self.queuedDelete, options: Self.options)) } + queued.cancel() + _ = await queued.result + running.cancel() + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(!served[0].queries.contains(Self.queuedDelete)) + #expect(served[0].queries.last == "SELECT 2") + } + + @Test("A young session token signs the kill in without waiting on a mint") + func youngSessionTokenServesTheKill() async throws { + let mints = OSAllocatedUnfairLock(initialState: 0) + let served = try await Self.killTokenSessions(sessionAge: .seconds(60)) { + mints.withLock { $0 += 1 } + return "kill-token" + } + let minted = mints.withLock { $0 } + + #expect(served[1].cleartextPassword == "connect-token") + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(minted == 0) + } + + @Test("Past ten minutes a kill signs in with a token minted for it, not the one the session signed in with") + func killMintsAFreshToken() async throws { + let served = try await Self.killTokenSessions(sessionAge: .seconds(601)) { "kill-token" } + + #expect(served[0].cleartextPassword == "connect-token") + #expect(served[1].cleartextPassword == "kill-token") + #expect(served[1].encrypted) + #expect(served[1].queries == ["KILL QUERY 1"]) + } + + @Test("A mint that fails leaves the kill the token the session signed in with") + func failedMintKeepsSessionToken() async throws { + let served = try await Self.killTokenSessions(sessionAge: .seconds(601)) { throw MintFailure() } + + #expect(served[1].cleartextPassword == "connect-token") + #expect(served[1].queries == ["KILL QUERY 1"]) + } + + // The tunnel and its prompts come before the connect the reuse is timed from, so a young-looking token can be + // expired; the server's 1045 sends the kill back for one mint. + @Test("A reused token the server refuses is minted once and the kill signs in again") + func refusedReusedTokenIsMintedOnce() async throws { + let mints = OSAllocatedUnfairLock(initialState: 0) + let served = try await Self.killTokenSessions( + sessionAge: .seconds(60), refusing: "connect-token", sessions: 3 + ) { + mints.withLock { $0 += 1 } + return "kill-token" + } + let minted = mints.withLock { $0 } + + #expect(served[1].cleartextPassword == "connect-token") + #expect(!served[1].loggedIn) + #expect(served[2].cleartextPassword == "kill-token") + #expect(served[2].queries == ["KILL QUERY 1"]) + #expect(minted == 1) + } + + @Test("A token minted for the kill that the server refuses is not tried again") + func refusedMintedTokenIsNotRetried() async throws { + let mints = OSAllocatedUnfairLock(initialState: 0) + let served = try await Self.killTokenSessions( + sessionAge: .seconds(601), refusing: "kill-token", sessions: 3, acceptingWithin: 5_000 + ) { + mints.withLock { $0 += 1 } + return "kill-token" + } + let minted = mints.withLock { $0 } + + #expect(served[1].cleartextPassword == "kill-token") + #expect(!served[1].loggedIn) + #expect(!served[2].sawLogin) + #expect(minted == 1) + } + + private struct MintFailure: Error {} + + private static func killTokenSessions( + sessionAge: Duration, + refusing refusedPassword: String? = nil, + sessions count: Int = 2, + acceptingWithin milliseconds: Int32? = nil, + refresh: @escaping @Sendable () async throws -> String? + ) async throws -> [FakeMySQLServer.Session] { + let server = try FakeMySQLServer(identity: .localhost, asksForCleartext: true, holdsQueriesStarting: Self.held) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: count, acceptingWithin: milliseconds) + let clock = ManualClock() + let driver = MySQLDriver( + host: "127.0.0.1", port: server.port, user: "iam_user", password: "connect-token", database: "", + ssl: DriverSSLConfiguration(mode: .require), signsInWithToken: true, refreshPassword: refresh, + clock: clock + ) + try await driver.connect() + if let refusedPassword { + server.refuse(refusedPassword) + } + clock.advance(by: sessionAge) + + let reader = Task { try await Self.read(driver.executeStreaming(query: Self.held, options: Self.options)) } + try await waitUntil { server.heldQueries == 1 } + reader.cancel() + + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + return await sessions + } + + private static func driver(port: Int, databaseType: DatabaseType = .mysql) -> MySQLDriver { + MySQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "", + databaseType: databaseType + ) + } + + private static func stop(_ reader: Task, on driver: MySQLDriver) async throws { + reader.cancel() + _ = await reader.result + try await driver.cancelCurrentQuery() + } + + private static func read(_ stream: AsyncThrowingStream) async throws { + for try await _ in stream {} + } +} + +struct MySQLKillRequestsTests { + @Test("A kill is taken only for the statement in flight, and once") + func killOnlyInFlight() { + var requests = MySQLKillRequests() + let first = requests.reserve() + let second = requests.reserve() + let beforeSending = requests.kill(first) + + requests.sent(first) + let otherStatement = requests.kill(second) + let inFlight = requests.kill(first) + let again = requests.kill(first) + requests.readerLeft(first) + let stopAgain = requests.killLeftInFlight() + + #expect(!beforeSending) + #expect(!otherStatement) + #expect(inFlight) + #expect(!again) + #expect(stopAgain == nil) + } + + @Test("Stop takes only the statement in flight whose reader left last") + func stopTakesOnlyTheLeftStatement() { + var requests = MySQLKillRequests() + let browsing = requests.reserve() + let editor = requests.reserve() + requests.sent(browsing) + let whileRead = requests.killLeftInFlight() + + requests.readerLeft(editor) + let editorQueued = requests.killLeftInFlight() + + requests.readerLeft(browsing) + let browsingLeft = requests.killLeftInFlight() + + #expect(whileRead == nil) + #expect(editorQueued == nil) + #expect(browsingLeft == browsing) + } + + @Test("A statement that ended takes no kill, so a late Stop never reaches the next one") + func endedStatementTakesNoKill() { + var requests = MySQLKillRequests() + let first = requests.reserve() + requests.sent(first) + requests.readerLeft(first) + requests.finished(first, interrupted: false) + let stop = requests.killLeftInFlight() + let streamEnd = requests.kill(first) + + let second = requests.reserve() + requests.sent(second) + requests.finished(first, interrupted: false) + + #expect(stop == nil) + #expect(!streamEnd) + #expect(requests.isInFlight(second)) + } + + @Test("Only a delivered kill its statement did not collect is left for the next statement") + func latchedKill() { + var requests = MySQLKillRequests() + let first = requests.reserve() + requests.sent(first) + requests.delivered(first) + requests.finished(first, interrupted: false) + let latched = requests.takeLatchedKill() + let latchedAgain = requests.takeLatchedKill() + + let second = requests.reserve() + requests.sent(second) + requests.finished(second, interrupted: true) + requests.delivered(second) + let collected = requests.takeLatchedKill() + + let third = requests.reserve() + requests.sent(third) + requests.finished(third, interrupted: false) + let neverKilled = requests.takeLatchedKill() + + #expect(latched) + #expect(!latchedAgain) + #expect(!collected) + #expect(!neverKilled) + } + + @Test("A closed session forgets its kills") + func detachForgets() { + var requests = MySQLKillRequests() + let first = requests.reserve() + requests.sent(first) + requests.delivered(first) + requests.detach() + let latched = requests.takeLatchedKill() + + #expect(!requests.isInFlight(first)) + #expect(!latched) + } + + @Test("Each engine gets the kill that stops only the statement") + func killStatements() { + #expect(Self.kill(.mysql, banner: "8.4.11", thread: 42) == "KILL QUERY 42") + #expect(Self.kill(.mariadb, banner: "10.6.28-MariaDB", thread: 42) == "KILL QUERY 42") + #expect(Self.kill(.mysql, banner: "5.0.96", thread: 42) == "KILL QUERY 42") + #expect(Self.kill(.oceanbase, banner: "5.7.25-OceanBase_CE-v4.2.1.0", thread: 42) == "KILL QUERY 42") + #expect(Self.kill(.tidb, banner: "8.0.11-TiDB-v8.5.0", thread: 42, tidb: 2_199_023_255_553) + == "KILL TIDB QUERY 2199023255553") + #expect(Self.kill(.mysql, banner: "8.4.11", thread: 0) == nil) + } + + @Test("A server that can only end the session, and Databend, get no kill") + func noKill() { + #expect(Self.kill(.mysql, banner: "4.1.22", thread: 42) == nil) + #expect(Self.kill(.mysql, banner: "8.0.17-v1.2.615-nightly", thread: 42) == nil) + } + + @Test("Only MySQL and MariaDB pay a round trip to collect a kill that landed late") + func absorbingEngines() { + #expect(MySQLKillStatement.absorbsLatchedKill(flavor: .mysql)) + #expect(MySQLKillStatement.absorbsLatchedKill(flavor: .mariadb)) + #expect(!MySQLKillStatement.absorbsLatchedKill(flavor: .tidb(version: nil))) + #expect(!MySQLKillStatement.absorbsLatchedKill(flavor: .oceanbase(version: nil))) + #expect(!MySQLKillStatement.absorbsLatchedKill(flavor: .databend)) + } + + private static func kill(_ type: DatabaseType, banner: String, thread: UInt, tidb: UInt64? = nil) -> String? { + MySQLKillStatement.text( + flavor: MySQLDriver.serverFlavor(for: type, banner: banner), + banner: banner, + threadId: thread, + tidbConnectionId: tidb + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLConnectionStringTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLConnectionStringTests.swift index 061be5235f..16a00f2550 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLConnectionStringTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLConnectionStringTests.swift @@ -34,6 +34,16 @@ struct PostgreSQLConnectionStringTests { #expect(connStr.contains("sslmode='disable'")) } + @Test("libpq is asked for the keepalive while it connects; the idle time it drops is set on the socket after") + func asksLibpqForTheKeepalive() throws { + let connStr = try build(ssl: .disabled) + + #expect(connStr.contains("keepalives='1'")) + #expect(connStr.contains("keepalives_idle='60'")) + #expect(connStr.contains("keepalives_interval='10'")) + #expect(connStr.contains("keepalives_count='3'")) + } + @Test("the session is pinned to UTF8 in the startup packet, whatever the database's encoding") func pinsClientEncoding() throws { #expect(try build(ssl: .disabled).contains("client_encoding='UTF8'")) diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLSessionSocketTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLSessionSocketTests.swift new file mode 100644 index 0000000000..eaa30276c9 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLSessionSocketTests.swift @@ -0,0 +1,26 @@ +import Foundation +@testable import TableProMobile +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct PostgreSQLSessionSocketTests { + // libpq here drops keepalives_idle without a word, so only the socket shows whether the idle time took. + @Test("A PostgreSQL session's socket fails a dead path in about 90 s, idle time included") + func keepaliveIsOnTheSocket() async throws { + let server = try FakePostgreSQLServer() + defer { server.close() } + async let session = server.serveOneSession() + let driver = PostgreSQLDriver( + host: "127.0.0.1", port: server.port, user: "app", password: "app-password", database: "app" + ) + try await driver.connect() + + let socket = try #require(SessionSocketProbe.clientSocket(connectedTo: server.port)) + let keepalive = SessionSocketProbe.keepalive(socket) + try await driver.disconnect() + let served = await session + + #expect(served.startedUp) + #expect(keepalive == SessionSocketProbe.expected) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/SessionSocketProbe.swift b/apple/TableProMobile/TableProMobileTests/Drivers/SessionSocketProbe.swift new file mode 100644 index 0000000000..09108e222e --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/SessionSocketProbe.swift @@ -0,0 +1,48 @@ +import Darwin +import Foundation + +// The test process holds a driver's end of its connection, so a session socket's options are read off it. +enum SessionSocketProbe { + struct Keepalive: Equatable { + let enabled: Bool + let idleSeconds: Int32 + let intervalSeconds: Int32 + let probeCount: Int32 + } + + static let expected = Keepalive(enabled: true, idleSeconds: 60, intervalSeconds: 10, probeCount: 3) + + static func clientSocket(connectedTo port: Int) -> Int32? { + (0...size) + let named = withUnsafeMutablePointer(to: &address) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { getpeername(descriptor, $0, &length) } + } + return named == 0 && address.sin_family == sa_family_t(AF_INET) && Int(address.sin_port.bigEndian) == port + } + } + + static func keepalive(_ socket: Int32) -> Keepalive { + Keepalive( + enabled: intOption(socket, SOL_SOCKET, SO_KEEPALIVE) != 0, + idleSeconds: intOption(socket, IPPROTO_TCP, TCP_KEEPALIVE), + intervalSeconds: intOption(socket, IPPROTO_TCP, TCP_KEEPINTVL), + probeCount: intOption(socket, IPPROTO_TCP, TCP_KEEPCNT) + ) + } + + static func receiveTimeout(_ socket: Int32) -> Double { + var timeout = timeval() + var length = socklen_t(MemoryLayout.size) + getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, &length) + return Double(timeout.tv_sec) + Double(timeout.tv_usec) / 1_000_000 + } + + private static func intOption(_ socket: Int32, _ level: Int32, _ name: Int32) -> Int32 { + var value: Int32 = 0 + var length = socklen_t(MemoryLayout.size) + getsockopt(socket, level, name, &value, &length) + return value + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift b/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift new file mode 100644 index 0000000000..037016add4 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift @@ -0,0 +1,171 @@ +import Foundation +import Security +@testable import TableProMobile + +// As Keychain Services documents: no synchronizable key or false finds device-only items, true synced ones, Any both. +final class InMemoryKeychainItems: KeychainItemStore, @unchecked Sendable { + private struct Item { + let service: String + let account: String + let synchronizable: Bool + var data: Data + var accessible: String + var modified: Date + } + + private enum Scope { + case deviceOnly + case synced + case both + } + + private static let deviceOnlyClasses: Set = [ + kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly as String, + kSecAttrAccessibleWhenUnlockedThisDeviceOnly as String, + kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly as String, + ] + + private let lock = NSLock() + private var items: [Item] = [] + private var refusedAdd: OSStatus? + private var refusedUpdate: OSStatus? + private var writes = 0 + + // Each write is one tick later than the one before, as a later save on any device would be. + private func nextDate() -> Date { + writes += 1 + return Date(timeIntervalSinceReferenceDate: TimeInterval(writes)) + } + + var failedAdd: OSStatus? { + get { lock.withLock { refusedAdd } } + set { lock.withLock { refusedAdd = newValue } } + } + + var failedUpdate: OSStatus? { + get { lock.withLock { refusedUpdate } } + set { lock.withLock { refusedUpdate = newValue } } + } + + func seed(_ value: String, service: String = "com.TablePro", account: String, synchronizable: Bool) { + let accessible = synchronizable + ? kSecAttrAccessibleAfterFirstUnlock as String + : kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly as String + lock.withLock { + items.removeAll { $0.service == service && $0.account == account && $0.synchronizable == synchronizable } + items.append(Item( + service: service, + account: account, + synchronizable: synchronizable, + data: Data(value.utf8), + accessible: accessible, + modified: nextDate() + )) + } + } + + func value(service: String = "com.TablePro", account: String, synchronizable: Bool) -> String? { + lock.withLock { () -> String? in + guard let item = stored(service: service, account: account, synchronizable: synchronizable) else { return nil } + return String(decoding: item.data, as: UTF8.self) + } + } + + func accessibility(service: String = "com.TablePro", account: String, synchronizable: Bool) -> String? { + lock.withLock { () -> String? in + stored(service: service, account: account, synchronizable: synchronizable)?.accessible + } + } + + func add(_ attributes: [String: Any]) -> OSStatus { + lock.withLock { () -> OSStatus in + if let refusedAdd { return refusedAdd } + guard let service = attributes[kSecAttrService as String] as? String, + let account = attributes[kSecAttrAccount as String] as? String, + let data = attributes[kSecValueData as String] as? Data, + let accessible = attributes[kSecAttrAccessible as String] as? String else { return errSecParam } + let synchronizable: Bool + switch Self.scope(of: attributes) { + case .deviceOnly: synchronizable = false + case .synced: synchronizable = true + case .both: return errSecParam + } + if synchronizable, Self.deviceOnlyClasses.contains(accessible) { return errSecParam } + guard stored(service: service, account: account, synchronizable: synchronizable) == nil else { + return errSecDuplicateItem + } + items.append(Item( + service: service, + account: account, + synchronizable: synchronizable, + data: data, + accessible: accessible, + modified: nextDate() + )) + return errSecSuccess + } + } + + func update(_ query: [String: Any], attributes: [String: Any]) -> OSStatus { + lock.withLock { () -> OSStatus in + if let refusedUpdate { return refusedUpdate } + let matched = matches(query) + guard !matched.isEmpty else { return errSecItemNotFound } + let accessible = attributes[kSecAttrAccessible as String] as? String + if let accessible, Self.deviceOnlyClasses.contains(accessible), + matched.contains(where: { items[$0].synchronizable }) { + return errSecParam + } + for index in matched { + if let data = attributes[kSecValueData as String] as? Data { items[index].data = data } + if let accessible { items[index].accessible = accessible } + items[index].modified = nextDate() + } + return errSecSuccess + } + } + + func copyItem(_ query: [String: Any]) -> KeychainCopiedItem { + lock.withLock { () -> KeychainCopiedItem in + guard let index = matches(query).first else { + return KeychainCopiedItem(status: errSecItemNotFound, data: nil, modified: nil) + } + return KeychainCopiedItem(status: errSecSuccess, data: items[index].data, modified: items[index].modified) + } + } + + func delete(_ query: [String: Any]) -> OSStatus { + lock.withLock { () -> OSStatus in + let matched = Set(matches(query)) + guard !matched.isEmpty else { return errSecItemNotFound } + items = items.enumerated().filter { !matched.contains($0.offset) }.map { $0.element } + return errSecSuccess + } + } + + private func stored(service: String, account: String, synchronizable: Bool) -> Item? { + items.first { $0.service == service && $0.account == account && $0.synchronizable == synchronizable } + } + + private func matches(_ query: [String: Any]) -> [Int] { + let service = query[kSecAttrService as String] as? String + let account = query[kSecAttrAccount as String] as? String + let scope = Self.scope(of: query) + return items.indices.filter { index in + let item = items[index] + guard service == nil || item.service == service, account == nil || item.account == account else { return false } + switch scope { + case .deviceOnly: return !item.synchronizable + case .synced: return item.synchronizable + case .both: return true + } + } + } + + private static func scope(of query: [String: Any]) -> Scope { + guard let value = query[kSecAttrSynchronizable as String] else { return .deviceOnly } + if let flag = value as? Bool { return flag ? .synced : .deviceOnly } + if let text = value as? String, text == kSecAttrSynchronizableAny as String { return .both } + return .deviceOnly + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift b/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift new file mode 100644 index 0000000000..c608f163b7 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift @@ -0,0 +1,125 @@ +import Foundation +import Security +@testable import TableProMobile +import Testing + +struct KeychainSecureStoreTests { + private let items = InMemoryKeychainItems() + private let account = ConnectionSecretKind.password.account(for: UUID()) + + private func secureStore(syncsPasswords: Bool) -> KeychainSecureStore { + KeychainSecureStore(syncsPasswords: { syncsPasswords }, items: items) + } + + @Test("Saving with Sync Passwords off keeps the synced copy another device reads") + func deviceOnlySaveKeepsTheSyncedCopy() throws { + items.seed("from-mac", account: account, synchronizable: true) + + try secureStore(syncsPasswords: false).store("from-phone", forKey: account) + try secureStore(syncsPasswords: false).store("from-phone-again", forKey: account) + + #expect(items.value(account: account, synchronizable: true) == "from-mac") + #expect(items.value(account: account, synchronizable: false) == "from-phone-again") + #expect(try secureStore(syncsPasswords: false).retrieve(forKey: account) == "from-phone-again") + } + + @Test("Saving with Sync Passwords on updates the synced copy in place and drops the device-only one") + func syncedSaveUpdatesInPlace() throws { + items.seed("old", account: account, synchronizable: true) + items.seed("local", account: account, synchronizable: false) + + try secureStore(syncsPasswords: true).store("new", forKey: account) + + #expect(items.value(account: account, synchronizable: true) == "new") + #expect(items.value(account: account, synchronizable: false) == nil) + #expect(items.accessibility(account: account, synchronizable: true) == kSecAttrAccessibleAfterFirstUnlock as String) + } + + @Test("A save the Keychain refuses leaves every stored copy as it was") + func refusedSaveLosesNothing() throws { + items.seed("from-mac", account: account, synchronizable: true) + items.failedAdd = errSecInteractionNotAllowed + + #expect(throws: KeychainError.self) { + try secureStore(syncsPasswords: false).store("from-phone", forKey: account) + } + #expect(items.value(account: account, synchronizable: true) == "from-mac") + #expect(try secureStore(syncsPasswords: false).retrieve(forKey: account) == "from-mac") + + items.failedAdd = nil + items.seed("local", account: account, synchronizable: false) + items.failedUpdate = errSecInteractionNotAllowed + + #expect(throws: KeychainError.self) { + try secureStore(syncsPasswords: true).store("new", forKey: account) + } + #expect(items.value(account: account, synchronizable: true) == "from-mac") + #expect(items.value(account: account, synchronizable: false) == "local") + } + + @Test("A read finds the copy the setting does not select when it is the only one") + func readFallsBackToTheOtherCopy() throws { + items.seed("from-mac", account: account, synchronizable: true) + #expect(try secureStore(syncsPasswords: false).retrieve(forKey: account) == "from-mac") + } + + @Test("With Sync Passwords on the newer copy is read; with it off this device's own copy is") + func readReturnsTheNewerCopy() throws { + items.seed("from-mac", account: account, synchronizable: true) + items.seed("from-phone", account: account, synchronizable: false) + + #expect(try secureStore(syncsPasswords: false).retrieve(forKey: account) == "from-phone") + #expect(try secureStore(syncsPasswords: true).retrieve(forKey: account) == "from-phone") + + items.seed("changed-on-mac", account: account, synchronizable: true) + + #expect(try secureStore(syncsPasswords: false).retrieve(forKey: account) == "from-phone") + #expect(try secureStore(syncsPasswords: true).retrieve(forKey: account) == "changed-on-mac") + } + + @Test("A password saved with Sync Passwords off is the one read once it is back on") + func deviceOnlySaveSurvivesTurningSyncBackOn() throws { + items.seed("from-mac", account: account, synchronizable: true) + + try secureStore(syncsPasswords: false).store("from-phone", forKey: account) + + #expect(try secureStore(syncsPasswords: true).retrieve(forKey: account) == "from-phone") + #expect(items.value(account: account, synchronizable: true) == "from-mac") + } + + @Test("A device-only sign-in key reads this device's copy over a newer synced one") + func deviceOnlyKeyReadsItsOwnCopy() throws { + let deviceKey = ConnectionSecretKind.deviceIAM.account(for: UUID()) + items.seed("own", account: deviceKey, synchronizable: false) + items.seed("synced", account: deviceKey, synchronizable: true) + + #expect(try secureStore(syncsPasswords: true).retrieve(forKey: deviceKey) == "own") + } + + @Test("A device-only sign-in key is written to this device alone with Sync Passwords on") + func deviceOnlyKeyStaysOnTheDevice() throws { + let deviceKey = ConnectionSecretKind.deviceIAM.account(for: UUID()) + + try secureStore(syncsPasswords: true).store("key", forKey: deviceKey) + + #expect(items.value(account: deviceKey, synchronizable: false) == "key") + #expect(items.value(account: deviceKey, synchronizable: true) == nil) + } + + @Test("A certificate save the Keychain refuses keeps the certificate already stored") + func refusedCertificateSaveKeepsTheOldOne() throws { + let store = CertificateMaterialStore(items: items) + let connectionId = UUID() + try store.store("old-pem", role: .clientCertificate, for: connectionId) + + items.failedUpdate = errSecInteractionNotAllowed + #expect(throws: CertificateStoreError.self) { + try store.store("new-pem", role: .clientCertificate, for: connectionId) + } + #expect(store.pem(role: .clientCertificate, for: connectionId) == "old-pem") + + items.failedUpdate = nil + try store.store("new-pem", role: .clientCertificate, for: connectionId) + #expect(store.pem(role: .clientCertificate, for: connectionId) == "new-pem") + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Platform/SessionTransactionMonitorTests.swift b/apple/TableProMobile/TableProMobileTests/Platform/SessionTransactionMonitorTests.swift index 9c64c404f0..5815991060 100644 --- a/apple/TableProMobile/TableProMobileTests/Platform/SessionTransactionMonitorTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Platform/SessionTransactionMonitorTests.swift @@ -218,6 +218,39 @@ struct SessionTransactionMonitorTests { #expect(monitor.hold(for: id) == nil) } + @Test("A session that ends with a transaction open keeps what it held for the window still on it") + func endedSessionKeepsItsHold() async { + let driver = attach(.explicitTransaction) + await monitor.refresh(id) + + live.drivers[id] = nil + monitor.sessionEnded(id, driver: driver) + + #expect(monitor.endedHold(for: id, on: driver) == SessionTransactionMonitor.EndedHold(hold: .open, duringWork: false)) + #expect(monitor.endedHold(for: id, on: MockDatabaseDriver()) == nil) + + attach(.idle) + await monitor.refresh(id) + #expect(monitor.endedHold(for: id, on: driver) == nil) + } + + @Test("A session that ends under a running statement is kept as ended during work, whichever notices first") + func sessionEndedUnderAStatement() async { + let driver = attach(.explicitTransaction) + await monitor.refresh(id) + let gate = TestGate() + + let work = Task { await monitor.track(id) { await gate.enter() } } + await gate.waitUntilEntered() + live.drivers[id] = nil + await gate.open() + await work.value + monitor.sessionEnded(id, driver: driver) + + #expect(monitor.hold(for: id) == nil) + #expect(monitor.endedHold(for: id, on: driver) == SessionTransactionMonitor.EndedHold(hold: .open, duringWork: true)) + } + @Test("An unknown answer keeps what the session last reported") func unknownKeepsTheEntry() async { let driver = attach(.explicitTransaction) From 9c9fca89cb899f61b5c902339d1a4a86770b7ace Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 00:11:09 +0700 Subject: [PATCH 2/2] docs(changelog): add the fragments for #4177 --- apple/changelog.d/4177.fixed.1.md | 1 + apple/changelog.d/4177.fixed.2.md | 1 + apple/changelog.d/4177.fixed.3.md | 1 + apple/changelog.d/4177.fixed.4.md | 1 + apple/changelog.d/4177.fixed.5.md | 1 + apple/changelog.d/4177.fixed.md | 1 + 6 files changed, 6 insertions(+) create mode 100644 apple/changelog.d/4177.fixed.1.md create mode 100644 apple/changelog.d/4177.fixed.2.md create mode 100644 apple/changelog.d/4177.fixed.3.md create mode 100644 apple/changelog.d/4177.fixed.4.md create mode 100644 apple/changelog.d/4177.fixed.5.md create mode 100644 apple/changelog.d/4177.fixed.md diff --git a/apple/changelog.d/4177.fixed.1.md b/apple/changelog.d/4177.fixed.1.md new file mode 100644 index 0000000000..d3b2ed3257 --- /dev/null +++ b/apple/changelog.d/4177.fixed.1.md @@ -0,0 +1 @@ +Stop on iOS left a MySQL statement running on the server, which then committed it. diff --git a/apple/changelog.d/4177.fixed.2.md b/apple/changelog.d/4177.fixed.2.md new file mode 100644 index 0000000000..40a3316349 --- /dev/null +++ b/apple/changelog.d/4177.fixed.2.md @@ -0,0 +1 @@ +A PostgreSQL query on iOS hung for about two hours when the network dropped its reply. diff --git a/apple/changelog.d/4177.fixed.3.md b/apple/changelog.d/4177.fixed.3.md new file mode 100644 index 0000000000..94aba709c6 --- /dev/null +++ b/apple/changelog.d/4177.fixed.3.md @@ -0,0 +1 @@ +Switching apps on iOS with a DuckDB transaction open rolled it back without any message. diff --git a/apple/changelog.d/4177.fixed.4.md b/apple/changelog.d/4177.fixed.4.md new file mode 100644 index 0000000000..d343b36ba9 --- /dev/null +++ b/apple/changelog.d/4177.fixed.4.md @@ -0,0 +1 @@ +Saving a password on iOS with Sync Passwords off deleted the iCloud Keychain copy your other devices use. diff --git a/apple/changelog.d/4177.fixed.5.md b/apple/changelog.d/4177.fixed.5.md new file mode 100644 index 0000000000..8b7fc143a4 --- /dev/null +++ b/apple/changelog.d/4177.fixed.5.md @@ -0,0 +1 @@ +A password saved on iOS while Sync Passwords was off went unused once Sync Passwords was turned back on. diff --git a/apple/changelog.d/4177.fixed.md b/apple/changelog.d/4177.fixed.md new file mode 100644 index 0000000000..c3957fa9c2 --- /dev/null +++ b/apple/changelog.d/4177.fixed.md @@ -0,0 +1 @@ +MySQL statements on iOS that ran past 30 seconds failed while the server kept running and committed them.