diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/RelayPollState.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/RelayPollState.swift index bf0f4180de..915b186cbc 100644 --- a/apple/Packages/TableProCore/Sources/TableProSSHTransport/RelayPollState.swift +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/RelayPollState.swift @@ -5,40 +5,10 @@ import Foundation -public enum RelayFDState: Equatable, Sendable { - case idle - case readable - case drainThenStop - case stop -} - -public func relayFDState(_ revents: Int16) -> RelayFDState { - if revents & Int16(POLLERR | POLLNVAL) != 0 { return .stop } - if revents & Int16(POLLHUP) != 0 { return .drainThenStop } - if revents & Int16(POLLIN) != 0 { return .readable } - return .idle -} - -/// Outcome of waiting for a specific direction on the SSH transport. Distinct from -/// `RelayFDState`, which classifies a read-side poll where "no bits set" means idle. -/// Here "no bits set" means the wait expired without the requested direction becoming -/// ready, which must not be mistaken for readiness. -public enum TransportPollOutcome: Equatable, Sendable { - case ready - case timedOut - case hangup -} - -public func transportPollOutcome(revents: Int16, requestedEvents: Int16) -> TransportPollOutcome { - if revents & Int16(POLLERR | POLLNVAL | POLLHUP) != 0 { return .hangup } - if revents & requestedEvents != 0 { return .ready } - return .timedOut -} - -/// Waits for libssh2's requested block directions to become ready on the transport. -/// When libssh2 reports no direction, there is nothing to wait on, so this yields -/// briefly instead of returning immediately, which would let a caller retry in a -/// zero-yield spin. +// Waits for libssh2's requested block directions to become ready on the transport. +// When libssh2 reports no direction, there is nothing to wait on, so this yields +// briefly instead of returning immediately, which would let a caller retry in a +// zero-yield spin. public func pollReady(fd: Int32, directions: RelayDirections, timeoutMs: Int32) -> Bool { var events: Int16 = 0 if directions.contains(.inbound) { events |= Int16(POLLIN) } @@ -54,3 +24,18 @@ public func pollReady(fd: Int32, directions: RelayDirections, timeoutMs: Int32) } private let directionlessYieldMicroseconds: UInt32 = 1_000 + +public enum TransportRoom: Equatable, Sendable { + case room + case full + case gone +} + +// A socket polls writable once its send low-water mark is free (2048 bytes unless set), which holds a small +// packet whole. A reset or closed socket reports a hangup or an error instead. +public func transportRoom(_ descriptor: Int32) -> TransportRoom { + var pollFD = pollfd(fd: descriptor, events: Int16(POLLOUT), revents: 0) + guard poll(&pollFD, 1, 0) == 1 else { return .full } + if pollFD.revents & Int16(POLLERR | POLLHUP | POLLNVAL) != 0 { return .gone } + return pollFD.revents & Int16(POLLOUT) != 0 ? .room : .full +} diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelIO.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelIO.swift new file mode 100644 index 0000000000..c89d65cf16 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelIO.swift @@ -0,0 +1,52 @@ +// +// SSHChannelIO.swift +// TableProSSHTransport +// + +import Foundation + +public struct RelayDirections: OptionSet, Sendable { + public let rawValue: Int32 + + public init(rawValue: Int32) { + self.rawValue = rawValue + } + + public static let inbound = RelayDirections(rawValue: 1 << 0) + public static let outbound = RelayDirections(rawValue: 1 << 1) +} + +public enum ChannelReadResult: Equatable, Sendable { + case bytes(Int) + case wouldBlock + case closed + // The session is gone, not only this channel: the relay ends .transportHangup so the owner marks the tunnel dead now, not at the next keep-alive. + case transportFailed +} + +public enum ChannelWriteResult: Equatable, Sendable { + case bytes(Int) + case wouldBlock + case closed + case transportFailed +} + +// One channel of a session, called only on its `SSHSessionPump`'s queue. +public protocol SSHChannelIO: Sendable { + func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult + func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult + func blockDirections() -> RelayDirections + // Data or an end of file another call on the session already took off the transport for this channel. + func hasQueuedInput() -> Bool + // Room left in the server's window for this channel's data. A `write` that answered `.wouldBlock` with + // room has a packet on its way that only another `write` on this channel finishes; without room it waits + // for the server to open the window. + func hasSendWindow() -> Bool +} + +public enum RelayTermination: Equatable, Sendable { + case localClosed + case transportHangup + case channelClosed + case cancelled +} diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelRelay.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelRelay.swift deleted file mode 100644 index ea3e7cc078..0000000000 --- a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHChannelRelay.swift +++ /dev/null @@ -1,234 +0,0 @@ -// -// SSHChannelRelay.swift -// TableProSSHTransport -// - -import Foundation - -public struct RelayDirections: OptionSet, Sendable { - public let rawValue: Int32 - - public init(rawValue: Int32) { - self.rawValue = rawValue - } - - public static let inbound = RelayDirections(rawValue: 1 << 0) - public static let outbound = RelayDirections(rawValue: 1 << 1) -} - -public enum ChannelReadResult: Equatable, Sendable { - case bytes(Int) - case wouldBlock - case closed - // The session is gone, not only this channel: the relay ends .transportHangup so the owner marks the tunnel dead now, not at the next keep-alive. - case transportFailed -} - -public enum ChannelWriteResult: Equatable, Sendable { - case bytes(Int) - case wouldBlock - case closed - case transportFailed -} - -public protocol SSHChannelIO { - func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult - func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult - func blockDirections() -> RelayDirections -} - -public enum RelayTermination: Equatable, Sendable { - case localClosed - case transportHangup - case channelClosed - case cancelled -} - -// A hangup drains buffered data before the loop ends, so the last bytes reach the client. -public struct SSHChannelRelay { - public let localFD: Int32 - public let transportFD: Int32 - public let channelIO: any SSHChannelIO - public let bufferSize: Int - public let isActive: () -> Bool - - /// Counts what crosses this relay, for the connection activity readout. Absent for a jump hop, - /// whose relay carries the same payload a second time on its way to the next hop: counting both - /// would report every byte twice for a connection that goes through a bastion. - public var byteCounter: (any RelayByteObserver)? - - public init( - localFD: Int32, - transportFD: Int32, - channelIO: any SSHChannelIO, - bufferSize: Int, - isActive: @escaping () -> Bool, - byteCounter: (any RelayByteObserver)? = nil - ) { - self.localFD = localFD - self.transportFD = transportFD - self.channelIO = channelIO - self.bufferSize = bufferSize - self.isActive = isActive - self.byteCounter = byteCounter - } - - private static let pollTimeoutMs: Int32 = 500 - private static let writeWaitTimeoutMs: Int32 = 1_000 - - /// How many channel reads one pump round may make before looking at the local fd again. - /// - /// libssh2 buffers whole SSH packets as it decrypts them, so a channel that has fallen behind - /// holds more than one buffer's worth and the transport reports nothing new until that backlog - /// is drained. Reading one buffer per poll round pays the 500ms timeout for every buffer, which - /// measured 1,504ms for 98,304 queued bytes against a real sshd where draining took 502ms. - /// The cap is what keeps a server that streams without pause from starving local to channel; - /// a round that ends on the cap rather than on EAGAIN polls with no timeout, so the cap costs - /// one round trip through `poll` and never a wait. - private static let maxChannelReadsPerRound = 16 - - public func run() -> RelayTermination { - let buffer = UnsafeMutablePointer.allocate(capacity: bufferSize) - defer { buffer.deallocate() } - var pollTimeoutMs = Self.pollTimeoutMs - - while isActive() { - var pollFDs = [ - pollfd(fd: localFD, events: Int16(POLLIN), revents: 0), - pollfd(fd: transportFD, events: Int16(POLLIN), revents: 0), - ] - - let pollResult = poll(&pollFDs, 2, pollTimeoutMs) - pollTimeoutMs = Self.pollTimeoutMs - if pollResult < 0 { - if errno == EINTR { continue } - return .cancelled - } - - let localState = relayFDState(pollFDs[0].revents) - let transportState = relayFDState(pollFDs[1].revents) - - if transportState == .stop { return .transportHangup } - if localState == .stop { return .localClosed } - - if transportState != .idle || pollResult == 0 { - switch pumpChannelToLocal(buffer, transportReadable: transportState != .idle) { - case .drained: break - case .cappedWithMoreQueued: pollTimeoutMs = 0 - case .channelClosed: return .channelClosed - case .localClosed: return .localClosed - case .transportHangup: return .transportHangup - } - } - - if localState == .readable || localState == .drainThenStop { - switch pumpLocalToChannel(buffer) { - case .drained, .cappedWithMoreQueued: break - case .channelClosed: return .channelClosed - case .localClosed: return .localClosed - case .transportHangup: return .transportHangup - } - } - - if transportState == .drainThenStop { return .transportHangup } - if localState == .drainThenStop { return .localClosed } - } - - return .cancelled - } - - private enum PumpOutcome { - case drained - case cappedWithMoreQueued - case channelClosed - case localClosed - case transportHangup - } - - private enum TransportWait { - case ready - case hangup - } - - private func pumpChannelToLocal(_ buffer: UnsafeMutablePointer, transportReadable: Bool) -> PumpOutcome { - for _ in 0 ..< Self.maxChannelReadsPerRound { - switch channelIO.read(into: buffer, count: bufferSize) { - case .bytes(let count): - byteCounter?.recordReceived(count) - guard sendToLocal(buffer, count: count) else { return .localClosed } - case .wouldBlock: - if transportReadable, transportAtEOF() { return .transportHangup } - return .drained - case .closed: - return .channelClosed - case .transportFailed: - return .transportHangup - } - } - return .cappedWithMoreQueued - } - - private func sendToLocal(_ buffer: UnsafeMutablePointer, count: Int) -> Bool { - var totalSent = 0 - while totalSent < count { - // Per send, not SO_NOSIGPIPE: setsockopt fails with EINVAL once a client has already closed, and the send then raises SIGPIPE. - let sent = send(localFD, buffer.advanced(by: totalSent), count - totalSent, MSG_NOSIGNAL) - if sent <= 0 { return false } - totalSent += sent - } - return true - } - - private func pumpLocalToChannel(_ buffer: UnsafeMutablePointer) -> PumpOutcome { - let localRead = recv(localFD, buffer, bufferSize, 0) - if localRead <= 0 { return .localClosed } - byteCounter?.recordSent(localRead) - - var totalWritten = 0 - while totalWritten < Int(localRead) { - switch channelIO.write(buffer.advanced(by: totalWritten), count: Int(localRead) - totalWritten) { - case .bytes(let count): - totalWritten += count - case .wouldBlock: - switch waitForTransport(channelIO.blockDirections()) { - case .ready: break - case .hangup: return .transportHangup - } - case .closed: - return .channelClosed - case .transportFailed: - return .transportHangup - } - } - return .drained - } - - private func waitForTransport(_ directions: RelayDirections) -> TransportWait { - var events: Int16 = 0 - if directions.contains(.inbound) { events |= Int16(POLLIN) } - if directions.contains(.outbound) { events |= Int16(POLLOUT) } - guard events != 0 else { return .ready } - - while true { - var pollFD = pollfd(fd: transportFD, events: events, revents: 0) - let rc = poll(&pollFD, 1, Self.writeWaitTimeoutMs) - if rc < 0 { - if errno == EINTR { continue } - return .hangup - } - switch transportPollOutcome(revents: pollFD.revents, requestedEvents: events) { - case .hangup: - return .hangup - case .ready: - return .ready - case .timedOut: - guard isActive() else { return .hangup } - } - } - } - - private func transportAtEOF() -> Bool { - var byte: UInt8 = 0 - return recv(transportFD, &byte, 1, MSG_PEEK | MSG_DONTWAIT) == 0 - } -} diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHJumpRelayFence.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHJumpRelayFence.swift index 4a545d1478..c7105bcac0 100644 --- a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHJumpRelayFence.swift +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHJumpRelayFence.swift @@ -6,10 +6,9 @@ import Foundation import os -// Cancelling a Swift task does not cancel the GCD block that runs a hop's `SSHChannelRelay`, so -// teardown wakes its descriptors and waits here for its last libssh2 call before freeing the channel -// or session. The stop action and the relay's final close share one lock, so a late stop cannot -// `shutdown` a descriptor number the kernel already handed to someone else. +// A hop's relay runs on its session's queue, outside any Swift task, so teardown stops it and waits +// here for its last libssh2 call before freeing the channel or session. The stop action and the +// finish share one lock, so a stop that comes after the finish does nothing. public final class SSHJumpRelayFence: @unchecked Sendable { private struct State { var stopAction: (@Sendable () -> Void)? diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHListenerAcceptor.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHListenerAcceptor.swift index 9fff8db54a..ee7959c49e 100644 --- a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHListenerAcceptor.swift +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHListenerAcceptor.swift @@ -92,7 +92,7 @@ public final class SSHListenerAcceptor: @unchecked Sendable { } } let acceptedAt = Date() - // Darwin hands the listener's O_NONBLOCK to the accepted socket, and the relays block on it. + // Darwin hands the listener's O_NONBLOCK to the accepted socket, and the Remote SQLite admission read blocks on it. let flags = fcntl(descriptor, F_GETFL, 0) if flags >= 0 { _ = fcntl(descriptor, F_SETFL, flags & ~O_NONBLOCK) diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHSessionPump.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHSessionPump.swift new file mode 100644 index 0000000000..22edaea70d --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHSessionPump.swift @@ -0,0 +1,784 @@ +// +// SSHSessionPump.swift +// TableProSSHTransport +// + +import Foundation +import os + +// The one reader of an SSH session. libssh2 moves every packet on the transport into the session's own queue +// whenever any channel reads or writes (ssh2_channel_read and ssh2_channel_write loop ssh2_transport_read until +// EAGAIN), so a channel that waits on the transport descriptor alone misses data another call already took off the +// socket. Every libssh2 call on the session runs on `queue`, and after each one the pump serves every channel it +// relays. Readiness comes from dispatch sources on the transport and the local sockets, so an idle session has no +// timer and parks no thread. +public final class SSHSessionPump: @unchecked Sendable { + public let queue: DispatchQueue + + private let transportFD: Int32 + private let maxReadsPerRound: Int + private let callbackQueue = DispatchQueue.global(qos: .utility) + // Entered once for the pump, once per relay and once per transport source; `stop` leaves the first. + private let lifetime = DispatchGroup() + private let outsideSenders = OSAllocatedUnfairLock(initialState: 0) + + // Everything below is touched only on `queue`. + private var relays: [PumpedChannel] = [] + private var transportRead: ReadinessSource? + private var transportWrite: ReadinessSource? + private var isStopped = false + private var isPassScheduled = false + private var passNumber = 0 + private var nextStart = 0 + + // A read on one channel can queue data for a channel served earlier in the same pass. + private static let maxSettleSweeps = 4 + private static let contendedReadsPerRound = 4 + + public init(transportFD: Int32, queue: DispatchQueue, maxReadsPerRound: Int = 16) { + self.transportFD = transportFD + self.queue = queue + self.maxReadsPerRound = maxReadsPerRound + lifetime.enter() + } + + // libdispatch crashes on releasing a source that is suspended or was never activated. + deinit { + let ending = relays + relays.removeAll() + ending.forEach { finish($0, .cancelled) } + transportRead?.cancel() + transportWrite?.cancel() + } + + // Runs a libssh2 call on the session's queue, then relays whatever it left queued. + public func perform(_ body: () throws -> T) rethrows -> T { + dispatchPrecondition(condition: .notOnQueue(queue)) + return try queue.sync { + defer { serve() } + return try body() + } + } + + // The same, for a caller on Swift's cooperative pool, which must not block a thread waiting for the queue. + public func performAsync(_ body: @escaping @Sendable () -> T) async -> T { + await withCheckedContinuation { (continuation: CheckedContinuation) in + queue.async { [self] in + let result = body() + serve() + continuation.resume(returning: result) + } + } + } + + // Takes ownership of `localFD`: the pump closes it once the relay ends, then calls `onEnd` once, off the queue. + public func relay( + localFD: Int32, + channel: any SSHChannelIO, + bufferSize: Int, + byteCounter: (any RelayByteObserver)? = nil, + onEnd: @escaping @Sendable (RelayTermination) -> Void + ) { + queue.async { [self] in + guard !isStopped else { + Darwin.close(localFD) + callbackQueue.async { onEnd(.cancelled) } + return + } + let flags = fcntl(localFD, F_GETFL, 0) + if flags >= 0 { + _ = fcntl(localFD, F_SETFL, flags | O_NONBLOCK) + } + startTransportSources() + + let relay = PumpedChannel( + localFD: localFD, + io: channel, + bufferSize: bufferSize, + byteCounter: byteCounter, + onEnd: onEnd + ) + lifetime.enter() + relay.localRead = ReadinessSource( + DispatchSource.makeReadSource(fileDescriptor: localFD, queue: queue), + group: relay.sources + ) { [weak self, weak relay] in + guard let relay else { return } + relay.localReadable = true + self?.serve(only: relay) + } + relay.localWrite = ReadinessSource( + DispatchSource.makeWriteSource(fileDescriptor: localFD, queue: queue), + group: relay.sources + ) { [weak self, weak relay] in + guard let relay else { return } + self?.serve(only: relay) + } + relays.append(relay) + serve() + } + } + + // Ends every relay as `.cancelled`; a relay added later ends at once. The transport descriptor stays open. + public func stop() { + queue.async { [self] in + guard !isStopped else { return } + isStopped = true + let ending = relays + relays.removeAll() + ending.forEach { finish($0, .cancelled) } + transportRead?.cancel() + transportWrite?.cancel() + lifetime.leave() + } + } + + // After `stop`, once no source can fire, every local descriptor is closed and every `onEnd` has run. + public func whenStopped(_ body: @escaping @Sendable () -> Void) { + lifetime.notify(queue: callbackQueue, execute: body) + } + + public func waitUntilStopped() async { + await withCheckedContinuation { (continuation: CheckedContinuation) in + lifetime.notify(queue: callbackQueue) { + continuation.resume() + } + } + } + + // MARK: - Send turns + + // An outside call that has to send on the session, an open or a free, holds a turn while it waits to. libssh2 + // sends one packet at a time per session and refuses every other send until it is out (transport_send_existing), + // so a relay that always had one pending would leave the call no gap. While a turn is held the relays finish the + // packet they have on its way and start no new one. + public func holdSendTurn() { + outsideSenders.withLock { $0 += 1 } + } + + public func releaseSendTurn() { + outsideSenders.withLock { $0 -= 1 } + queue.async { [weak self] in + self?.serve() + } + } + + // MARK: - Serving + + private struct Pass { + let number: Int + let outsideWaiting: Bool + let readBudget: Int + var hasMore = false + } + + private enum Step { + case proceed + case end(RelayTermination) + } + + private func startTransportSources() { + guard transportRead == nil else { return } + transportRead = ReadinessSource( + DispatchSource.makeReadSource(fileDescriptor: transportFD, queue: queue), + group: lifetime + ) { [weak self] in + self?.transportBecameReadable() + } + transportWrite = ReadinessSource( + DispatchSource.makeWriteSource(fileDescriptor: transportFD, queue: queue), + group: lifetime + ) { [weak self] in + self?.serve() + } + } + + private func transportBecameReadable() { + serve() + guard !isStopped, !relays.isEmpty, Self.transportEnded(transportFD) else { return } + let ending = relays + relays.removeAll() + for relay in ending { + _ = relay.sendToLocal() + finish(relay, .transportHangup) + } + rearmTransport() + } + + // A local socket's event serves its own relay first and the rest in one scheduled pass, so a busy channel + // cannot make every other client's event wait behind its reads. That pass always follows: the relay's own call + // may have taken the others' data or a key exchange reply off the transport. + private func serve(only focus: PumpedChannel) { + guard !isStopped, !focus.isFinished else { return } + var pass = newPass() + if let termination = serve(focus, pass: &pass) { + remove(focus, termination) + } else if isLagging(focus, pass: pass), let termination = serve(focus, pass: &pass) { + remove(focus, termination) + } + if pass.hasMore || relays.contains(where: { $0 !== focus }) { + schedulePass() + } + rearmTransport() + } + + // Each pass starts one relay further on, and serves with a smaller read budget while more than one relay has + // work, so a busy channel cannot hold the queue against a quiet one for long. + private func newPass() -> Pass { + passNumber += 1 + let busy = relays.count(where: \.hasWork) + return Pass( + number: passNumber, + outsideWaiting: outsideSenders.withLock { $0 > 0 }, + readBudget: busy > 1 ? min(maxReadsPerRound, Self.contendedReadsPerRound) : maxReadsPerRound + ) + } + + private func serve() { + guard !isStopped else { return } + var pass = newPass() + let first = relays.isEmpty ? 0 : nextStart % relays.count + let order = Array(relays[first...]) + Array(relays[.. RelayTermination? { + if let ending = relay.endingAfterSend { + return finishSend(relay, ending: ending) + } + if case .end(let termination) = relayChannelToLocal(relay, pass: &pass) { + return end(relay, termination) + } + if case .end(let termination) = relayLocalToChannel(relay, pass: &pass) { + return end(relay, termination) + } + if relay.channelAtEOF, relay.toLocal.isEmpty { + return end(relay, .channelClosed) + } + relay.localRead?.arm(relay.toChannel.isEmpty) + relay.localWrite?.arm(!relay.toLocal.isEmpty) + return nil + } + + private func remove(_ relay: PumpedChannel, _ termination: RelayTermination) { + relays.removeAll { $0 === relay } + finish(relay, termination) + } + + // Freeing a channel with its packet half sent would leave every later send on the session refused, and only + // the call that started the packet can finish it: a write, or a read's window adjust. Such a relay ends once + // that call has gone through. + private func end(_ relay: PumpedChannel, _ termination: RelayTermination) -> RelayTermination? { + guard termination != .transportHangup, relay.pendingSend != nil else { + return termination + } + relay.endingAfterSend = termination + relay.localRead?.arm(false) + relay.localWrite?.arm(false) + return nil + } + + private func finishSend(_ relay: PumpedChannel, ending: RelayTermination) -> RelayTermination? { + switch relay.pendingSend { + case .write: + switch relay.io.write(relay.toChannel.head, count: relay.toChannel.count) { + case .bytes, .closed: + relay.pendingSend = nil + case .wouldBlock: + return nil + case .transportFailed: + return .transportHangup + } + case .read: + // What this read returns has nowhere to go: the relay is ending. + switch relay.io.read(into: relay.toChannel.storage, count: relay.toChannel.capacity) { + case .bytes, .closed: + relay.pendingSend = nil + case .wouldBlock: + guard relay.io.blockDirections().contains(.outbound) else { + relay.pendingSend = nil + return ending + } + return nil + case .transportFailed: + return .transportHangup + } + case nil: + break + } + return ending + } + + private func relayChannelToLocal(_ relay: PumpedChannel, pass: inout Pass) -> Step { + if !relay.toLocal.isEmpty { + switch relay.sendToLocal() { + case .drained: break + case .blocked: return .proceed + case .closed: return .end(.localClosed) + } + } + guard !relay.channelAtEOF else { return .proceed } + + for _ in 0 ..< pass.readBudget { + let owned = relay.pendingSend == .read + let outboundBefore = sessionWaitsToSend + switch relay.io.read(into: relay.toLocal.storage, count: relay.toLocal.capacity) { + case .bytes(let count): + relay.ignoresQueuedHint = false + relay.readWaitsOnSend = false + if owned { relay.pendingSend = nil } + relay.byteCounter?.recordReceived(count) + relay.toLocal.fill(count) + switch relay.sendToLocal() { + case .drained: continue + case .blocked: return .proceed + case .closed: return .end(.localClosed) + } + case .wouldBlock: + let outboundAfter = relay.io.blockDirections().contains(.outbound) + relay.lastReadWasEmpty = true + relay.readWaitsOnSend = outboundAfter + if outboundAfter, owned || !outboundBefore { + relay.pendingSend = .read + } else if owned { + relay.pendingSend = nil + } + return .proceed + case .closed: + relay.readWaitsOnSend = false + if owned { relay.pendingSend = nil } + relay.channelAtEOF = true + return .proceed + case .transportFailed: + return .end(.transportHangup) + } + } + pass.hasMore = true + return .proceed + } + + private func relayLocalToChannel(_ relay: PumpedChannel, pass: inout Pass) -> Step { + var reads = 0 + while true { + while !relay.toChannel.isEmpty { + let owned = relay.pendingSend == .write + if !owned { + guard mayStartPacket(relay, pass: &pass) else { return .proceed } + } + let outboundBefore = sessionWaitsToSend + switch relay.io.write(relay.toChannel.head, count: relay.toChannel.count) { + case .bytes(let count): + // Sending or finishing a packet was this relay's turn. + relay.writeBlocked = false + relay.refusedBehindPacket = false + if owned { relay.pendingSend = nil } + relay.startedPacketInPass = pass.number + relay.toChannel.consume(count) + case .wouldBlock: + // With no window libssh2 built no packet, and the window opens on input. + let hasWindow = relay.io.hasSendWindow() + relay.writeBlocked = true + relay.writeWaitsOnWindow = !hasWindow + let outboundAfter = relay.io.blockDirections().contains(.outbound) + // A write that started its packet owns it until a call returns bytes: libssh2 may clear the + // session's outbound bit under it (a write with no window sets the directions outright). + if owned || (outboundAfter && !outboundBefore) { + relay.pendingSend = .write + relay.refusedBehindPacket = false + relay.startedPacketInPass = pass.number + } else { + relay.refusedBehindPacket = hasWindow && outboundBefore + } + return .proceed + case .closed: + relay.writeBlocked = false + if owned { relay.pendingSend = nil } + return .end(.channelClosed) + case .transportFailed: + return .end(.transportHangup) + } + } + guard relay.localReadable else { return .proceed } + guard reads < maxReadsPerRound else { + pass.hasMore = true + return .proceed + } + reads += 1 + switch relay.receiveFromLocal() { + case .bytes(let count): + relay.byteCounter?.recordSent(count) + case .wouldBlock: + relay.localReadable = false + return .proceed + case .closed: + return .end(.localClosed) + } + } + } + + // Turns: a relay refused behind another's packet goes before any relay that was not, and one that already + // had its turn this pass lets any other that waits go first. While an outside call holds a turn no relay starts + // a packet at all; that call comes back through `perform` or `releaseSendTurn`. A pass is scheduled for the + // relay held back only when no packet is pending, as one that is pending ends through its own owner. + private func mayStartPacket(_ relay: PumpedChannel, pass: inout Pass) -> Bool { + guard !turnAllows(relay, pass: pass) else { return true } + if !pass.outsideWaiting, !packetPending { + pass.hasMore = true + } + return false + } + + private func turnAllows(_ relay: PumpedChannel, pass: Pass) -> Bool { + if pass.outsideWaiting { return false } + var othersWait = false + var othersRefused = false + for other in relays where other !== relay && !other.isFinished { + othersWait = othersWait || other.waitsToSend + othersRefused = othersRefused || other.refusedBehindPacket + } + let hadTurn = relay.startedPacketInPass == pass.number && othersWait + let cedes = !relay.refusedBehindPacket && othersRefused + return !hadTurn && !cedes + } + + // A writer blocked on something that is gone, or a reader that waited on a send, retries in this pass; a + // writer only when its turn allows, or settle would sweep it again and again. + private func isLagging(_ relay: PumpedChannel, pass: Pass) -> Bool { + guard relay.endingAfterSend == nil, !relay.isFinished else { return false } + if relay.writeBlocked, relay.pendingSend == nil { + let obstacleGone = relay.writeWaitsOnWindow ? relay.io.hasSendWindow() : relay.refusedBehindPacket && !packetPending + if obstacleGone, turnAllows(relay, pass: pass) { return true } + } + if relay.readWaitsOnSend, relay.pendingSend == nil, !packetPending, relay.canTakeInput { return true } + return relay.isLaggingOnInput + } + + // libssh2 keeps its one pending packet by the address of the call that started it. A relay that started it + // knows; otherwise the session's outbound bit tells, and stays set until the packet is out. + private var sessionWaitsToSend: Bool { + relays.first?.io.blockDirections().contains(.outbound) ?? false + } + + private var relayOwnsPacket: Bool { + relays.contains { $0.pendingSend != nil } + } + + private var packetPending: Bool { + relayOwnsPacket || sessionWaitsToSend + } + + // Read and write sources fire for as long as the descriptor stays ready. While a packet waits to go out, the + // write source is armed only for a relay that owns it, since nothing else here can finish it, and the read source + // waits too: libssh2 reads nothing that needs a send. A packet an open or a free owns is finished by that call, + // which comes back through `perform`. + private func rearmTransport() { + guard let transportRead, let transportWrite else { return } + if relayOwnsPacket { + transportWrite.arm(true) + transportRead.arm(false) + } else if sessionWaitsToSend { + transportWrite.arm(false) + transportRead.arm(false) + } else { + transportWrite.arm(false) + transportRead.arm(relays.contains(where: \.wantsTransportInput)) + } + } + + private func schedulePass() { + guard !isPassScheduled else { return } + isPassScheduled = true + queue.async { [weak self] in + guard let self else { return } + isPassScheduled = false + serve() + } + } + + private func finish(_ relay: PumpedChannel, _ termination: RelayTermination) { + relay.isFinished = true + relay.cancelSources() + let localFD = relay.localFD + let onEnd = relay.onEnd + let lifetime = self.lifetime + relay.sources.notify(queue: callbackQueue) { + Darwin.close(localFD) + onEnd(termination) + lifetime.leave() + } + } + + private static func transportEnded(_ descriptor: Int32) -> Bool { + var byte: UInt8 = 0 + let peeked = recv(descriptor, &byte, 1, MSG_PEEK | MSG_DONTWAIT) + if peeked == 0 { return true } + return peeked < 0 && errno != EAGAIN && errno != EINTR + } +} + +// One relayed channel, touched only on the pump's queue. +private final class PumpedChannel: @unchecked Sendable { + let localFD: Int32 + let io: any SSHChannelIO + let byteCounter: (any RelayByteObserver)? + let onEnd: @Sendable (RelayTermination) -> Void + let toLocal: RelayBuffer + let toChannel: RelayBuffer + let sources = DispatchGroup() + + enum PendingSend { + case write + case read + } + + var localRead: ReadinessSource? + var localWrite: ReadinessSource? + var localReadable = true + var channelAtEOF = false + var writeBlocked = false + var writeWaitsOnWindow = false + // Its last write was turned away because another call's packet was on its way. + var refusedBehindPacket = false + var readWaitsOnSend = false + // The session's one pending packet, when this relay's own write or window adjust started it. + var pendingSend: PendingSend? + var startedPacketInPass = 0 + var endingAfterSend: RelayTermination? + var isFinished = false + var lastReadWasEmpty = false + // libssh2 counts extended data a plain read never returns, so a hint the read disproved is set aside until data flows. + var ignoresQueuedHint = false + + init( + localFD: Int32, + io: any SSHChannelIO, + bufferSize: Int, + byteCounter: (any RelayByteObserver)?, + onEnd: @escaping @Sendable (RelayTermination) -> Void + ) { + self.localFD = localFD + self.io = io + self.byteCounter = byteCounter + self.onEnd = onEnd + toLocal = RelayBuffer(capacity: bufferSize) + toChannel = RelayBuffer(capacity: bufferSize) + } + + var canTakeInput: Bool { + endingAfterSend == nil && toLocal.isEmpty && !channelAtEOF + } + + var hasWork: Bool { + !toChannel.isEmpty || (canTakeInput && io.hasQueuedInput()) + } + + var waitsToSend: Bool { + guard endingAfterSend == nil else { return false } + if pendingSend != nil || refusedBehindPacket { return true } + return !toChannel.isEmpty && (!writeWaitsOnWindow || io.hasSendWindow()) + } + + var isLaggingOnInput: Bool { + canTakeInput && !ignoresQueuedHint && io.hasQueuedInput() + } + + var wantsTransportInput: Bool { + canTakeInput || !toChannel.isEmpty || endingAfterSend != nil + } + + enum LocalSend { + case drained + case blocked + case closed + } + + enum LocalReceive { + case bytes(Int) + case wouldBlock + case closed + } + + func sendToLocal() -> LocalSend { + while !toLocal.isEmpty { + // Per send, not SO_NOSIGPIPE: setsockopt fails with EINVAL once a client has already closed, and the send then raises SIGPIPE. + let sent = send(localFD, toLocal.head, toLocal.count, MSG_NOSIGNAL) + if sent > 0 { + toLocal.consume(sent) + } else if sent < 0, errno == EINTR { + continue + } else if sent < 0, errno == EAGAIN { + return .blocked + } else { + return .closed + } + } + return .drained + } + + func receiveFromLocal() -> LocalReceive { + while true { + let received = recv(localFD, toChannel.storage, toChannel.capacity, 0) + if received > 0 { + toChannel.fill(received) + return .bytes(received) + } + if received < 0, errno == EINTR { continue } + if received < 0, errno == EAGAIN { return .wouldBlock } + return .closed + } + } + + func cancelSources() { + localRead?.cancel() + localWrite?.cancel() + } +} + +private final class RelayBuffer { + let storage: UnsafeMutablePointer + let capacity: Int + private var start = 0 + private var end = 0 + + init(capacity: Int) { + storage = .allocate(capacity: capacity) + self.capacity = capacity + } + + deinit { + storage.deallocate() + } + + var isEmpty: Bool { start == end } + var head: UnsafeMutablePointer { storage + start } + var count: Int { end - start } + + func fill(_ count: Int) { + start = 0 + end = count + } + + func consume(_ count: Int) { + start = min(end, start + count) + if start == end { + start = 0 + end = 0 + } + } +} + +// A dispatch source the pump suspends while it has no use for the event, since read and write sources fire for as +// long as the descriptor stays ready. Touched only on the pump's queue. +private final class ReadinessSource: @unchecked Sendable { + private enum State { + case inactive + case armed + case disarmed + case cancelled + } + + private let source: any DispatchSourceProtocol + private var state = State.inactive + + init(_ source: any DispatchSourceProtocol, group: DispatchGroup, handler: @escaping @Sendable () -> Void) { + self.source = source + group.enter() + source.setEventHandler(handler: handler) + source.setCancelHandler { @Sendable in + group.leave() + } + } + + func arm(_ wanted: Bool) { + switch (state, wanted) { + case (.inactive, true): + source.activate() + state = .armed + case (.disarmed, true): + source.resume() + state = .armed + case (.armed, false): + source.suspend() + state = .disarmed + default: + break + } + } + + // GCD runs a cancel handler only once the source is active and not suspended. + func cancel() { + let previous = state + guard previous != .cancelled else { return } + state = .cancelled + source.cancel() + switch previous { + case .inactive: source.activate() + case .disarmed: source.resume() + case .armed, .cancelled: break + } + } +} + +// One outside call's turn on a session (`SSHSessionPump.holdSendTurn`): held while the call waits to send, let go +// once it waits only for the server or is done. +public final class SSHSendTurn: @unchecked Sendable { + private let pump: SSHSessionPump + private let isHeld = OSAllocatedUnfairLock(initialState: false) + + public init(pump: SSHSessionPump) { + self.pump = pump + } + + deinit { + end() + } + + public func waitsToSend(_ waits: Bool) { + let changed = isHeld.withLock { held -> Bool in + guard held != waits else { return false } + held = waits + return true + } + guard changed else { return } + if waits { + pump.holdSendTurn() + } else { + pump.releaseSendTurn() + } + } + + public func end() { + waitsToSend(false) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/RelayPollStateTests.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/RelayPollStateTests.swift index 97fa773461..c211586af2 100644 --- a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/RelayPollStateTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/RelayPollStateTests.swift @@ -2,89 +2,11 @@ // RelayPollStateTests.swift // TableProSSHTransportTests // -// Tests for relayFDState, the poll revents classifier that decides whether a -// relay fd is idle, readable, draining before teardown, or fatally errored. -// import Foundation @testable import TableProSSHTransport import Testing -@Suite("RelayPollState") -struct RelayPollStateTests { - @Test("No events is idle") - func idle() { - #expect(relayFDState(0) == .idle) - } - - @Test("POLLIN alone is readable") - func readable() { - #expect(relayFDState(Int16(POLLIN)) == .readable) - } - - @Test("POLLHUP drains then stops") - func hangup() { - #expect(relayFDState(Int16(POLLHUP)) == .drainThenStop) - } - - @Test("POLLIN with POLLHUP drains then stops") - func hangupWithBufferedData() { - #expect(relayFDState(Int16(POLLIN) | Int16(POLLHUP)) == .drainThenStop) - } - - @Test("POLLERR is fatal") - func error() { - #expect(relayFDState(Int16(POLLERR)) == .stop) - } - - @Test("POLLNVAL is fatal") - func invalid() { - #expect(relayFDState(Int16(POLLNVAL)) == .stop) - } - - @Test("Fatal errors win over readable data") - func errorWithData() { - #expect(relayFDState(Int16(POLLIN) | Int16(POLLERR)) == .stop) - } - - @Test("Fatal errors win over hangup") - func errorWithHangup() { - #expect(relayFDState(Int16(POLLHUP) | Int16(POLLERR)) == .stop) - } -} - -@Suite("transportPollOutcome") -struct TransportPollOutcomeTests { - @Test("An expired wait is not readiness") - func timeoutIsNotReady() { - #expect(transportPollOutcome(revents: 0, requestedEvents: Int16(POLLIN)) == .timedOut) - } - - @Test("The requested direction becoming ready is readiness") - func requestedDirectionReady() { - #expect(transportPollOutcome(revents: Int16(POLLIN), requestedEvents: Int16(POLLIN)) == .ready) - #expect(transportPollOutcome(revents: Int16(POLLOUT), requestedEvents: Int16(POLLOUT)) == .ready) - } - - @Test("Another direction becoming ready is not the requested one") - func otherDirectionIsNotReady() { - #expect(transportPollOutcome(revents: Int16(POLLIN), requestedEvents: Int16(POLLOUT)) == .timedOut) - } - - @Test("Hangup and errors end the wait") - func hangupEndsWait() { - #expect(transportPollOutcome(revents: Int16(POLLHUP), requestedEvents: Int16(POLLOUT)) == .hangup) - #expect(transportPollOutcome(revents: Int16(POLLERR), requestedEvents: Int16(POLLOUT)) == .hangup) - #expect(transportPollOutcome(revents: Int16(POLLNVAL), requestedEvents: Int16(POLLOUT)) == .hangup) - } - - @Test("Hangup wins over the requested direction") - func hangupWinsOverReady() { - #expect(transportPollOutcome(revents: Int16(POLLOUT) | Int16(POLLHUP), requestedEvents: Int16(POLLOUT)) == .hangup) - } -} - -@Suite("pollReady") struct PollReadyTests { @Test("A directionless wait yields and retries instead of spinning") func directionlessYields() { @@ -113,3 +35,36 @@ struct PollReadyTests { #expect(pollReady(fd: pair.a, directions: .inbound, timeoutMs: 50) == false) } } + +struct TransportRoomTests { + @Test("A socket with free buffer has room") + func roomWhenEmpty() { + let pair = SocketPair() + defer { pair.close() } + + #expect(transportRoom(pair.a) == .room) + } + + @Test("A socket whose buffer is full has no room") + func fullWhenFilled() { + let pair = SocketPair() + defer { pair.close() } + + let flags = fcntl(pair.a, F_GETFL, 0) + _ = fcntl(pair.a, F_SETFL, flags | O_NONBLOCK) + var chunk = [UInt8](repeating: 0x46, count: 4_096) + while Darwin.send(pair.a, &chunk, chunk.count, 0) > 0 {} + + #expect(transportRoom(pair.a) == .full) + } + + @Test("A socket whose peer closed is gone") + func goneWhenPeerClosed() { + let pair = SocketPair() + defer { pair.close() } + + pair.closeB() + + #expect(transportRoom(pair.a) == .gone) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHChannelRelayTests.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHChannelRelayTests.swift deleted file mode 100644 index 3077f8ca2d..0000000000 --- a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHChannelRelayTests.swift +++ /dev/null @@ -1,598 +0,0 @@ -// -// SSHChannelRelayTests.swift -// TableProSSHTransportTests -// -// Tests for SSHChannelRelay termination behaviour. The relay runs over real -// socketpairs with a scripted channel so the regression case (a closed peer fd -// must terminate the loop instead of busy-spinning) is exercised end to end. -// - -import Foundation -import Testing - -@testable import TableProSSHTransport - -@Suite("SSHChannelRelay") -struct SSHChannelRelayTests { - @Test("Cancellation via isActive stops the relay") - func cancelled() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .wouldBlock), - isActive: { false } - ) - - #expect(result == .cancelled) - } - - @Test("Transport hangup terminates instead of spinning") - func transportHangup() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - transport.closeB() - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .wouldBlock) - ) - - #expect(result == .transportHangup) - } - - @Test("Transport half-close at EOF terminates instead of spinning") - func transportHalfCloseEOF() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - shutdown(transport.b, SHUT_WR) - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .wouldBlock) - ) - - #expect(result == .transportHangup) - } - - @Test("Local hangup terminates instead of spinning") - func localHangup() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - local.closeB() - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .wouldBlock) - ) - - #expect(result == .localClosed) - } - - @Test("Channel close terminates the relay") - func channelClosed() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .closed) - ) - - #expect(result == .channelClosed) - } - - @Test("A transport failure on read ends as a transport hangup, not a channel close") - func readTransportFailureIsHangup() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .transportFailed) - ) - - #expect(result == .transportHangup) - } - - @Test("A transport failure on read after the SSH server hung up ends as a transport hangup") - func readTransportFailureAfterHangupIsHangup() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - transport.closeB() - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .transportFailed) - ) - - #expect(result == .transportHangup) - } - - @Test("A transport failure on write ends as a transport hangup, not a channel close") - func writeTransportFailureIsHangup() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("world".utf8) - payload.withUnsafeBytes { raw in - _ = Darwin.send(local.b, raw.baseAddress, raw.count, 0) - } - - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: FakeChannelIO(fallback: .wouldBlock, writeResult: .transportFailed) - ) - - #expect(result == .transportHangup) - } - - @Test("Channel data for a client that already closed ends the relay as a local close") - func channelDataForClosedClientIsLocalClose() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - local.closeB() - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let payload = Data("rows".utf8) - let io = FakeChannelIO(actions: [.data(payload)], fallback: .wouldBlock) - let result = runRelay(localFD: local.a, transportFD: transport.a, io: io) - - #expect(result == .localClosed) - #expect(io.readCount == 1) - #expect(io.deliveredCount == payload.count) - } - - @Test("Channel data is forwarded to the local socket") - func forwardsChannelData() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("hello".utf8) - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let io = FakeChannelIO(actions: [.data(payload)], fallback: .closed) - let result = runRelay(localFD: local.a, transportFD: transport.a, io: io) - - #expect(result == .channelClosed) - - var received = [UInt8](repeating: 0, count: payload.count) - let count = recv(local.b, &received, received.count, 0) - #expect(count == payload.count) - #expect(Data(received) == payload) - } - - @Test("Local data is forwarded to the channel") - func forwardsLocalData() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("world".utf8) - payload.withUnsafeBytes { raw in - _ = Darwin.send(local.b, raw.baseAddress, raw.count, 0) - } - - let io = FakeChannelIO(fallback: .wouldBlock) - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: io, - isActive: io.activeUntilWritten(payload.count) - ) - - #expect(result == .cancelled) - #expect(io.written == payload) - } - - @Test("Channel data counts as received") - func countsChannelDataAsReceived() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("hello".utf8) - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let counter = RecordingByteObserver() - let io = FakeChannelIO(actions: [.data(payload)], fallback: .closed) - let result = runRelay(localFD: local.a, transportFD: transport.a, io: io, byteCounter: counter) - - #expect(result == .channelClosed) - #expect(counter.received == payload.count) - #expect(counter.sent == 0) - } - - @Test("Local data counts as sent") - func countsLocalDataAsSent() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("world".utf8) - payload.withUnsafeBytes { raw in - _ = Darwin.send(local.b, raw.baseAddress, raw.count, 0) - } - - let counter = RecordingByteObserver() - let io = FakeChannelIO(fallback: .wouldBlock) - let result = runRelay( - localFD: local.a, - transportFD: transport.a, - io: io, - isActive: io.activeUntilWritten(payload.count), - byteCounter: counter - ) - - #expect(result == .cancelled) - #expect(counter.received == 0) - #expect(counter.sent == payload.count) - } - - @Test("A relay with no counter still runs") - func runsWithoutACounter() { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let payload = Data("hello".utf8) - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let io = FakeChannelIO(actions: [.data(payload)], fallback: .closed) - let result = runRelay(localFD: local.a, transportFD: transport.a, io: io) - - #expect(result == .channelClosed) - } - - @Test("Concurrent relays sharing one transport each receive their own channel data") - func concurrentRelaysShareTransport() { - let transport = SocketPair() - let first = SocketPair() - let second = SocketPair() - defer { transport.close(); first.close(); second.close() } - - var dummy: UInt8 = 1 - _ = Darwin.send(transport.b, &dummy, 1, 0) - - let firstPayload = Data("first".utf8) - let secondPayload = Data("second".utf8) - let firstIO = FakeChannelIO(actions: [.data(firstPayload)], fallback: .closed) - let secondIO = FakeChannelIO(actions: [.data(secondPayload)], fallback: .closed) - - let group = DispatchGroup() - let firstBox = ResultBox() - let secondBox = ResultBox() - - for (localFD, io, box) in [(first.a, firstIO, firstBox), (second.a, secondIO, secondBox)] { - group.enter() - runRelayOnDedicatedThread( - localFD: localFD, - transportFD: transport.a, - io: io, - isActive: { true }, - box: box - ) { group.leave() } - } - - #expect(group.wait(timeout: .now() + 5) == .success) - #expect(firstBox.value == .channelClosed) - #expect(secondBox.value == .channelClosed) - #expect(readPayload(from: first.b, count: firstPayload.count) == firstPayload) - #expect(readPayload(from: second.b, count: secondPayload.count) == secondPayload) - } - - private func readPayload(from fd: Int32, count: Int) -> Data { - var buffer = [UInt8](repeating: 0, count: count) - let received = recv(fd, &buffer, count, 0) - guard received > 0 else { return Data() } - return Data(buffer.prefix(received)) - } - - private func runRelay( - localFD: Int32, - transportFD: Int32, - io: FakeChannelIO, - isActive: @escaping @Sendable () -> Bool = { true }, - byteCounter: (any RelayByteObserver)? = nil, - timeout: Double = 3 - ) -> RelayTermination? { - let box = ResultBox() - let semaphore = DispatchSemaphore(value: 0) - runRelayOnDedicatedThread( - localFD: localFD, - transportFD: transportFD, - io: io, - isActive: isActive, - box: box, - byteCounter: byteCounter - ) { semaphore.signal() } - _ = semaphore.wait(timeout: .now() + timeout) - return box.value - } -} - -/// The lost wakeup and the drain that follows it, both against a transport fd that is never -/// readable. libssh2 decrypts whole SSH packets into its own buffer, so a second channel on the -/// session holds bytes the transport has already given up: `poll` reports nothing, forever. -/// -/// Measured against a real sshd with two channels and 98,304 queued bytes: reading only when the -/// transport polls readable delivered 0 bytes in 60,178ms; pumping on the poll timeout too -/// delivered all of it in 1,504ms with one buffer per timeout; draining until the channel answers -/// EAGAIN delivered all of it in 502ms. -@Suite("SSHChannelRelay backlog drain") -struct SSHChannelRelayBacklogTests { - fileprivate static let bufferSize = 32_768 - fileprivate static let queuedBuffers = 3 - - @Test("A backlog reaches the local socket although the transport never polls readable") - func deliversBacklogWithoutATransportWakeup() throws { - let run = try runBacklogRelay() - - #expect(run.delivered == Self.bufferSize * Self.queuedBuffers) - } - - @Test("One poll round drains the whole backlog instead of one buffer per timeout") - func drainsInOnePollRound() throws { - let run = try runBacklogRelay() - - #expect(run.reads == Self.queuedBuffers + 1) - } - - @Test("A backlog past the per-round cap still lands inside one poll interval") - func drainsPastTheReadCapWithoutWaiting() throws { - let run = try runBacklogRelay(buffers: 20) - - #expect(run.delivered == Self.bufferSize * 20) - #expect(run.reads == 21) - #expect(run.elapsed < 1.0) - } - - private struct BacklogRun { - let delivered: Int - let reads: Int - let elapsed: TimeInterval - } - - /// Drains the local socket on a reader thread so a 96KB backlog cannot fill the socket - /// buffer and stall the relay inside `send`. The relay stops once the channel has handed - /// over every queued byte, which is what the two shapes take different numbers of poll - /// rounds to reach. - private func runBacklogRelay(buffers: Int = SSHChannelRelayBacklogTests.queuedBuffers) throws -> BacklogRun { - let local = SocketPair() - let transport = SocketPair() - defer { local.close(); transport.close() } - - let total = Self.bufferSize * buffers - let io = FakeChannelIO( - actions: Array(repeating: .data(Data(repeating: 0x41, count: Self.bufferSize)), count: buffers), - fallback: .wouldBlock - ) - - let drained = ByteTally() - let reader = Thread { - var buffer = [UInt8](repeating: 0, count: Self.bufferSize) - while drained.value < total { - let count = recv(local.b, &buffer, buffer.count, 0) - if count <= 0 { return } - drained.add(count) - } - } - reader.stackSize = 512 * 1_024 - reader.start() - - let box = ResultBox() - let semaphore = DispatchSemaphore(value: 0) - let started = Date() - runRelayOnDedicatedThread( - localFD: local.a, - transportFD: transport.a, - io: io, - isActive: { io.deliveredCount < total }, - box: box - ) { semaphore.signal() } - - #expect(semaphore.wait(timeout: .now() + 10) == .success) - let elapsed = Date().timeIntervalSince(started) - _ = drained.waitUntil(total, timeout: 2) - return BacklogRun(delivered: drained.value, reads: io.readCount, elapsed: elapsed) - } -} - -/// Runs a relay on a thread of its own rather than borrowing from the shared global -/// queue. The relay loop blocks its thread for as long as it runs, and the test suite -/// runs in parallel, so relays sharing the global queue's bounded pool can starve each -/// other and time out on machines under load. -internal func runRelayOnDedicatedThread( - localFD: Int32, - transportFD: Int32, - io: any SSHChannelIO, - isActive: @escaping @Sendable () -> Bool, - box: ResultBox, - byteCounter: (any RelayByteObserver)? = nil, - onFinish: @escaping @Sendable () -> Void -) { - let thread = Thread { - let relay = SSHChannelRelay( - localFD: localFD, - transportFD: transportFD, - channelIO: io, - bufferSize: 32_768, - isActive: isActive, - byteCounter: byteCounter - ) - box.value = relay.run() - onFinish() - } - thread.stackSize = 512 * 1_024 - thread.start() -} - -internal final class ResultBox: @unchecked Sendable { - var value: RelayTermination? -} - -internal final class ByteTally: @unchecked Sendable { - private let lock = NSLock() - private var count = 0 - - var value: Int { - lock.lock() - defer { lock.unlock() } - return count - } - - func add(_ amount: Int) { - lock.lock() - count += amount - lock.unlock() - } - - func waitUntil(_ target: Int, timeout: TimeInterval) -> Bool { - let deadline = Date().addingTimeInterval(timeout) - while value < target { - if Date() >= deadline { return false } - usleep(2_000) - } - return true - } -} - -internal final class RecordingByteObserver: RelayByteObserver, @unchecked Sendable { - private let lock = NSLock() - private var receivedBytes = 0 - private var sentBytes = 0 - - var received: Int { - lock.lock() - defer { lock.unlock() } - return receivedBytes - } - - var sent: Int { - lock.lock() - defer { lock.unlock() } - return sentBytes - } - - func recordReceived(_ count: Int) { - lock.lock() - receivedBytes += count - lock.unlock() - } - - func recordSent(_ count: Int) { - lock.lock() - sentBytes += count - lock.unlock() - } -} - -internal final class FakeChannelIO: SSHChannelIO, @unchecked Sendable { - enum Action { - case data(Data) - case wouldBlock - case closed - case transportFailed - } - - private let lock = NSLock() - private var actions: [Action] - private let fallback: Action - private let writeResult: ChannelWriteResult? - private var writtenBuffer = Data() - private var reads = 0 - private var delivered = 0 - - init(actions: [Action] = [], fallback: Action = .wouldBlock, writeResult: ChannelWriteResult? = nil) { - self.actions = actions - self.fallback = fallback - self.writeResult = writeResult - } - - var written: Data { - lock.lock() - defer { lock.unlock() } - return writtenBuffer - } - - var readCount: Int { - lock.lock() - defer { lock.unlock() } - return reads - } - - var deliveredCount: Int { - lock.lock() - defer { lock.unlock() } - return delivered - } - - func activeUntilWritten(_ target: Int) -> @Sendable () -> Bool { - { [weak self] in (self?.written.count ?? target) < target } - } - - func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult { - lock.lock() - defer { lock.unlock() } - reads += 1 - let action = actions.isEmpty ? fallback : actions.removeFirst() - switch action { - case .data(let data): - let length = min(data.count, count) - buffer.withMemoryRebound(to: UInt8.self, capacity: length) { destination in - _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: destination, count: length)) - } - delivered += length - return .bytes(length) - case .wouldBlock: - return .wouldBlock - case .closed: - return .closed - case .transportFailed: - return .transportFailed - } - } - - func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult { - lock.lock() - defer { lock.unlock() } - if let writeResult { return writeResult } - buffer.withMemoryRebound(to: UInt8.self, capacity: count) { source in - writtenBuffer.append(contentsOf: UnsafeBufferPointer(start: source, count: count)) - } - return .bytes(count) - } - - func blockDirections() -> RelayDirections { [] } -} diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHSessionPumpTests.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHSessionPumpTests.swift new file mode 100644 index 0000000000..aa99d89314 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHSessionPumpTests.swift @@ -0,0 +1,923 @@ +// +// SSHSessionPumpTests.swift +// TableProSSHTransportTests +// +// The pump runs over real socketpairs with scripted channels, so a closed peer, a full socket and +// a quiet transport behave the way a database client and an SSH server would make them behave. +// + +import Foundation +import os +import TableProTestSupport +import Testing + +@testable import TableProSSHTransport + +struct SSHSessionPumpTests { + @Test("Stopping the pump ends its relays as cancelled and closes their sockets") + func stopCancels() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock)) + fixture.pump.stop() + + try await waitUntil { ended.value != nil } + #expect(ended.value == .cancelled) + #expect(readsEndOfFile(local.b)) + } + + @Test("A relay added after stop ends as cancelled and its socket is closed") + func relayAfterStopCancels() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + fixture.pump.stop() + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .cancelled) + #expect(readsEndOfFile(local.b)) + } + + @Test("Once stopped, every relay has ended and closed its socket") + func waitUntilStoppedCoversRelays() async throws { + let fixture = PumpFixture() + let first = SocketPair() + let second = SocketPair() + defer { first.close(); second.close(); fixture.close() } + + let firstEnded = fixture.relay(first, io: FakeChannelIO(fallback: .wouldBlock)) + let secondEnded = fixture.relay(second, io: FakeChannelIO(fallback: .wouldBlock)) + fixture.pump.stop() + await fixture.pump.waitUntilStopped() + + #expect(firstEnded.value == .cancelled) + #expect(secondEnded.value == .cancelled) + #expect(readsEndOfFile(first.b)) + #expect(readsEndOfFile(second.b)) + } + + @Test("Transport hangup ends the relay instead of spinning") + func transportHangup() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + fixture.transport.closeB() + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .transportHangup) + } + + @Test("Transport half-close at EOF ends the relay instead of spinning") + func transportHalfCloseEOF() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + shutdown(fixture.transport.b, SHUT_WR) + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .transportHangup) + } + + @Test("Local hangup ends the relay instead of spinning") + func localHangup() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + local.closeB() + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .localClosed) + } + + @Test("Channel close ends the relay") + func channelClosed() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .closed)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .channelClosed) + } + + @Test("A transport failure on read ends as a transport hangup, not a channel close") + func readTransportFailureIsHangup() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .transportFailed)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .transportHangup) + } + + @Test("A transport failure on read after the SSH server hung up ends as a transport hangup") + func readTransportFailureAfterHangupIsHangup() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + fixture.transport.closeB() + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .transportFailed)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .transportHangup) + } + + @Test("A transport failure on write ends as a transport hangup, not a channel close") + func writeTransportFailureIsHangup() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + send(Data("world".utf8), to: local.b) + let ended = fixture.relay(local, io: FakeChannelIO(fallback: .wouldBlock, writeFallback: .transportFailed)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .transportHangup) + } + + @Test("Channel data for a client that already closed ends the relay as a local close") + func channelDataForClosedClientIsLocalClose() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + local.closeB() + let payload = Data("rows".utf8) + let io = FakeChannelIO(actions: [.data(payload)], fallback: .wouldBlock) + let ended = fixture.relay(local, io: io) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .localClosed) + #expect(io.readCount == 1) + #expect(io.deliveredCount == payload.count) + } + + @Test("Channel data is forwarded to the local socket") + func forwardsChannelData() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let payload = Data("hello".utf8) + let ended = fixture.relay(local, io: FakeChannelIO(actions: [.data(payload)], fallback: .closed)) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .channelClosed) + #expect(receive(from: local.b, count: payload.count) == payload) + } + + @Test("Local data is forwarded to the channel") + func forwardsLocalData() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let payload = Data("world".utf8) + send(payload, to: local.b) + let io = FakeChannelIO(fallback: .wouldBlock) + _ = fixture.relay(local, io: io) + + try await waitUntil { io.written.count >= payload.count } + #expect(io.written == payload) + } + + @Test("Channel data counts as received and local data as sent") + func countsBothDirections() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let incoming = Data("hello".utf8) + let outgoing = Data("world!".utf8) + send(outgoing, to: local.b) + let counter = RecordingByteObserver() + let io = FakeChannelIO(actions: [.data(incoming)], fallback: .wouldBlock) + _ = fixture.relay(local, io: io, counter: counter) + + try await waitUntil { counter.received == incoming.count && counter.sent == outgoing.count } + #expect(io.written == outgoing) + } + + @Test("Relays sharing one session each receive their own channel data") + func relaysShareOneSession() async throws { + let fixture = PumpFixture() + let first = SocketPair() + let second = SocketPair() + defer { first.close(); second.close(); fixture.close() } + + let firstPayload = Data("first".utf8) + let secondPayload = Data("second".utf8) + let firstEnded = fixture.relay(first, io: FakeChannelIO(actions: [.data(firstPayload)], fallback: .closed)) + let secondEnded = fixture.relay(second, io: FakeChannelIO(actions: [.data(secondPayload)], fallback: .closed)) + + try await waitUntil { firstEnded.value != nil && secondEnded.value != nil } + #expect(firstEnded.value == .channelClosed) + #expect(secondEnded.value == .channelClosed) + #expect(receive(from: first.b, count: firstPayload.count) == firstPayload) + #expect(receive(from: second.b, count: secondPayload.count) == secondPayload) + } + + // The lost wakeup: libssh2 drains every packet on the transport into its own queue whenever any + // channel reads, so data for a channel served earlier in a pass sits there with the socket quiet. + @Test("Data another channel's read queued reaches its client without a transport event") + func deliversDataAnotherReadQueued() async throws { + let fixture = PumpFixture() + let early = SocketPair() + let late = SocketPair() + defer { early.close(); late.close(); fixture.close() } + + let earlyIO = FakeChannelIO(fallback: .wouldBlock) + let queuedForEarly = Data("queued".utf8) + let lateIO = FakeChannelIO(actions: [.data(Data("late".utf8))], fallback: .wouldBlock) { reads in + if reads == 1 { earlyIO.enqueue(.data(queuedForEarly)) } + } + _ = fixture.relay(early, io: earlyIO) + try await waitUntil { earlyIO.readCount >= 1 } + _ = fixture.relay(late, io: lateIO) + + try await waitUntil(timeout: .seconds(2)) { earlyIO.deliveredCount == queuedForEarly.count } + #expect(receive(from: early.b, count: queuedForEarly.count) == queuedForEarly) + } + + @Test("An idle relay makes no channel call") + func idleRelayMakesNoCall() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let io = FakeChannelIO(fallback: .wouldBlock) + _ = fixture.relay(local, io: io) + try await waitUntil { io.readCount >= 1 } + let settled = io.readCount + + // The previous relay read the channel on a 500 ms timer, so this window would have caught one. + try await Task.sleep(for: .milliseconds(700)) + #expect(io.readCount == settled) + #expect(io.writeCount == 0) + } + + @Test("A call on the session relays whatever it queued") + func performRelaysWhatItQueued() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let io = FakeChannelIO(fallback: .wouldBlock) + _ = fixture.relay(local, io: io) + try await waitUntil { io.readCount >= 1 } + + let payload = Data("drained by an open".utf8) + fixture.pump.perform { io.enqueue(.data(payload)) } + + #expect(io.deliveredCount == payload.count) + #expect(receive(from: local.b, count: payload.count) == payload) + } + + // The SQLite agent's heartbeats travel client to channel while its rows wait for a slow reader. + @Test("Local data keeps reaching the channel while the client is not reading") + func localDataFlowsWhileClientDoesNotRead() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let chunk = Data(repeating: 0x52, count: 32_768) + let io = FakeChannelIO(actions: Array(repeating: .data(chunk), count: 64), fallback: .wouldBlock) + _ = fixture.relay(local, io: io) + try await waitUntil { io.readCount >= 1 } + + let heartbeat = Data("heartbeat".utf8) + send(heartbeat, to: local.b) + + try await waitUntil(timeout: .seconds(2)) { io.written == heartbeat } + #expect(io.deliveredCount < chunk.count * 64) + } + + @Test("A write waiting on the transport resumes once the transport can take data") + func writeResumesWhenTransportWritable() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + fillSendBuffer(fixture.transport.a) + let payload = Data("query".utf8) + send(payload, to: local.b) + let io = FakeChannelIO(fallback: .wouldBlock, writeResults: [.wouldBlock], writeBlocks: .outbound) + _ = fixture.relay(local, io: io) + try await waitUntil { io.writeCount >= 1 } + #expect(io.written.isEmpty) + + drain(fixture.transport.b) + + try await waitUntil(timeout: .seconds(2)) { io.written == payload } + } + + // Freeing a channel with a packet half sent would leave libssh2 refusing every later send. + @Test("A relay whose write is in flight ends only once that write finishes") + func inFlightWriteFinishesBeforeEnd() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + send(Data("x".utf8), to: local.b) + let io = FakeChannelIO( + actions: [.wouldBlock], + fallback: .closed, + writeResults: [.wouldBlock, .wouldBlock, .bytes(1)], + writeBlocks: .outbound + ) + let ended = fixture.relay(local, io: io) + try await waitUntil { io.writeCount >= 1 } + send(Data([1]), to: fixture.transport.b) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .channelClosed) + #expect(io.writeCount == 3) + } + + // A read can owe its window adjust, which only another read on that channel sends. + @Test("A relay whose read waits to send ends only once that read goes through") + func readOwingASendFinishesBeforeEnd() async throws { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + local.closeB() + let io = FakeChannelIO(actions: [.wouldBlock, .wouldBlock], fallback: .closed, readBlocks: .outbound) + let ended = fixture.relay(local, io: io) + + try await waitUntil { ended.value != nil } + #expect(ended.value == .localClosed) + #expect(io.readCount == 3) + } + + // libssh2 takes a window adjust off the transport whichever channel reads, and the writer waits on no socket. + @Test("A write waiting on the window goes out once another channel's read opens it") + func writeGoesOutWhenAnotherReadOpensItsWindow() async throws { + let fixture = PumpFixture() + let writer = SocketPair() + let reader = SocketPair() + defer { writer.close(); reader.close(); fixture.close() } + + let payload = Data("query".utf8) + send(payload, to: writer.b) + let writerIO = FakeChannelIO(fallback: .wouldBlock, sendWindow: false) + _ = fixture.relay(writer, io: writerIO) + try await waitUntil { writerIO.writeCount >= 1 } + #expect(writerIO.written.isEmpty) + + let readerIO = FakeChannelIO(actions: [.data(Data("rows".utf8))], fallback: .wouldBlock) { reads in + if reads == 1 { writerIO.openWindow() } + } + _ = fixture.relay(reader, io: readerIO) + + try await waitUntil(timeout: .seconds(2)) { writerIO.written == payload } + } + + // libssh2 sends one packet at a time per session, so an upload that always has one on its way would refuse + // every other channel's write until it ends. + @Test("A message gets through beside an upload limited by the socket") + func messageGetsThroughBesideUpload() async throws { + let fixture = PumpFixture() + let uploader = SocketPair() + let messenger = SocketPair() + defer { uploader.close(); messenger.close(); fixture.close() } + + let session = OnePacketSession(transportFD: fixture.transport.a) + let drain = SlowDrain(fixture.transport.b) + defer { drain.stop() } + let uploaderIO = session.channel() + let messengerIO = session.channel() + let upload = Data(repeating: 0x55, count: 16 << 20) + enlargeBuffers(uploader) + let peer = uploader.b + Thread { _ = upload.withUnsafeBytes { Darwin.send(peer, $0.baseAddress, $0.count, MSG_NOSIGNAL) } }.start() + _ = fixture.relay(uploader, io: uploaderIO) + try await waitUntil { uploaderIO.writtenCount > 0 } + + let message = Data("hello".utf8) + send(message, to: messenger.b) + _ = fixture.relay(messenger, io: messengerIO) + + try await waitUntil(timeout: .seconds(2)) { messengerIO.writtenCount == message.count } + #expect(uploaderIO.writtenCount < upload.count) + } + + @Test("An outside call waiting to send gets a gap beside an upload limited by the socket") + func outsideCallGetsAGapBesideUpload() async throws { + let fixture = PumpFixture() + let uploader = SocketPair() + defer { uploader.close(); fixture.close() } + + let session = OnePacketSession(transportFD: fixture.transport.a) + let drain = SlowDrain(fixture.transport.b) + defer { drain.stop() } + let uploaderIO = session.channel() + let upload = Data(repeating: 0x55, count: 16 << 20) + enlargeBuffers(uploader) + let peer = uploader.b + Thread { _ = upload.withUnsafeBytes { Darwin.send(peer, $0.baseAddress, $0.count, MSG_NOSIGNAL) } }.start() + _ = fixture.relay(uploader, io: uploaderIO) + try await waitUntil { session.isSending } + + let turn = SSHSendTurn(pump: fixture.pump) + turn.waitsToSend(true) + defer { turn.end() } + let pump = fixture.pump + try await waitUntil(timeout: .seconds(2)) { pump.perform { !session.isSending } } + #expect(uploaderIO.writtenCount < upload.count) + } + + // A write that finds no window clears libssh2's outbound bit while another channel's packet is on its way. + @Test("An upload keeps going beside a channel whose window is closed") + func uploadKeepsGoingBesideAClosedWindow() async throws { + let fixture = PumpFixture() + let uploader = SocketPair() + let waiter = SocketPair() + defer { uploader.close(); waiter.close(); fixture.close() } + + let session = OnePacketSession(transportFD: fixture.transport.a) + let drain = SlowDrain(fixture.transport.b) + defer { drain.stop() } + let uploaderIO = session.channel() + let waiterIO = session.channel(windowClosed: true) + let upload = Data(repeating: 0x55, count: 16 << 20) + enlargeBuffers(uploader) + let peer = uploader.b + Thread { _ = upload.withUnsafeBytes { Darwin.send(peer, $0.baseAddress, $0.count, MSG_NOSIGNAL) } }.start() + _ = fixture.relay(uploader, io: uploaderIO) + try await waitUntil { session.isSending } + + send(Data("waits".utf8), to: waiter.b) + _ = fixture.relay(waiter, io: waiterIO) + try await waitUntil { waiterIO.writeCount >= 2 } + let drainedBefore = drain.drained + + // Progress through the transport, at whatever pace the drain thread gets on a loaded machine: a stalled + // pump lets through only what already sat in the socket. + try await waitUntil(timeout: .seconds(15)) { drain.drained >= drainedBefore + (256 << 10) } + #expect(uploaderIO.writtenCount < upload.count) + } +} + +// libssh2 decrypts whole SSH packets into its own buffer, so a channel can hold more than one +// buffer's worth while the transport reports nothing new. +// +// Measured against a real sshd with two channels and 98,304 queued bytes: reading only when the +// transport polls readable delivered 0 bytes in 60,178ms; pumping on a 500ms poll timeout too +// delivered all of it in 1,504ms with one buffer per timeout; draining until the channel answers +// EAGAIN delivered all of it in 502ms. +struct SSHSessionPumpBacklogTests { + fileprivate static let bufferSize = 32_768 + fileprivate static let queuedBuffers = 3 + + @Test("A backlog reaches the local socket although the transport never turns readable") + func deliversBacklogWithoutATransportWakeup() async throws { + let run = try await runBacklog() + + #expect(run.delivered == Self.bufferSize * Self.queuedBuffers) + } + + @Test("One pass drains the whole backlog instead of one buffer per wakeup") + func drainsInOnePass() async throws { + let run = try await runBacklog() + + #expect(run.reads == Self.queuedBuffers + 1) + } + + @Test("A backlog past the per-pass cap still lands at once") + func drainsPastTheReadCapWithoutWaiting() async throws { + let run = try await runBacklog(buffers: 20) + + #expect(run.delivered == Self.bufferSize * 20) + // The pass the cap scheduled can run after a local event's pass took the last buffer, and read once more. + #expect((21 ... 22).contains(run.reads)) + #expect(run.elapsed < 1.0) + } + + private struct BacklogRun { + let delivered: Int + let reads: Int + let elapsed: TimeInterval + } + + // Drains the local socket on a reader thread so a backlog bigger than the socket buffer + // cannot stall the relay on a full socket. + private func runBacklog(buffers: Int = SSHSessionPumpBacklogTests.queuedBuffers) async throws -> BacklogRun { + let fixture = PumpFixture() + let local = SocketPair() + defer { local.close(); fixture.close() } + + let total = Self.bufferSize * buffers + let io = FakeChannelIO( + actions: Array(repeating: .data(Data(repeating: 0x41, count: Self.bufferSize)), count: buffers), + fallback: .wouldBlock + ) + + let drained = ByteTally() + let peer = local.b + let reader = Thread { + var buffer = [UInt8](repeating: 0, count: Self.bufferSize) + while drained.value < total { + let count = recv(peer, &buffer, buffer.count, 0) + if count <= 0 { return } + drained.add(count) + } + } + reader.stackSize = 512 * 1_024 + reader.start() + + let started = Date() + _ = fixture.relay(local, io: io) + try await waitUntil { drained.value >= total && io.readCount > buffers } + let elapsed = Date().timeIntervalSince(started) + return BacklogRun(delivered: drained.value, reads: io.readCount, elapsed: elapsed) + } +} + +// MARK: - Support + +internal final class PumpFixture: @unchecked Sendable { + let transport = SocketPair() + let pump: SSHSessionPump + + init() { + pump = SSHSessionPump(transportFD: transport.a, queue: DispatchQueue(label: "com.TablePro.tests.ssh.pump")) + } + + func relay(_ local: SocketPair, io: any SSHChannelIO, counter: (any RelayByteObserver)? = nil) -> EndRecorder { + let ended = EndRecorder() + pump.relay(localFD: local.takeA(), channel: io, bufferSize: 32_768, byteCounter: counter) { termination in + ended.record(termination) + } + return ended + } + + // The transport closes only after no source of the pump can fire on it, without holding a test thread. + func close() { + pump.stop() + pump.whenStopped { [self] in transport.close() } + } +} + +internal final class EndRecorder: @unchecked Sendable { + private let lock = NSLock() + private var termination: RelayTermination? + + var value: RelayTermination? { + lock.withLock { termination } + } + + func record(_ termination: RelayTermination) { + lock.withLock { + if self.termination == nil { self.termination = termination } + } + } +} + +internal final class ByteTally: @unchecked Sendable { + private let lock = NSLock() + private var count = 0 + + var value: Int { + lock.withLock { count } + } + + func add(_ amount: Int) { + lock.withLock { count += amount } + } +} + +internal final class RecordingByteObserver: RelayByteObserver, @unchecked Sendable { + private let lock = NSLock() + private var receivedBytes = 0 + private var sentBytes = 0 + + var received: Int { + lock.withLock { receivedBytes } + } + + var sent: Int { + lock.withLock { sentBytes } + } + + func recordReceived(_ count: Int) { + lock.withLock { receivedBytes += count } + } + + func recordSent(_ count: Int) { + lock.withLock { sentBytes += count } + } +} + +internal final class FakeChannelIO: SSHChannelIO, @unchecked Sendable { + enum Action { + case data(Data) + case wouldBlock + case closed + case transportFailed + } + + private let lock = NSLock() + private var actions: [Action] + private let fallback: Action + private var writeResults: [ChannelWriteResult] + private let writeFallback: ChannelWriteResult? + private let readBlocks: RelayDirections + private let writeBlocks: RelayDirections + private var sendWindow: Bool + private let onRead: (@Sendable (Int) -> Void)? + private var lastBlock: RelayDirections = [] + private var writtenBuffer = Data() + private var reads = 0 + private var writes = 0 + private var delivered = 0 + + // The directions answer for the last call that would block, as libssh2's do. + init( + actions: [Action] = [], + fallback: Action = .wouldBlock, + writeResults: [ChannelWriteResult] = [], + writeFallback: ChannelWriteResult? = nil, + readBlocks: RelayDirections = [], + writeBlocks: RelayDirections = [], + sendWindow: Bool = true, + onRead: (@Sendable (Int) -> Void)? = nil + ) { + self.actions = actions + self.fallback = fallback + self.writeResults = writeResults + self.writeFallback = writeFallback + self.readBlocks = readBlocks + self.writeBlocks = writeBlocks + self.sendWindow = sendWindow + self.onRead = onRead + } + + var written: Data { lock.withLock { writtenBuffer } } + var readCount: Int { lock.withLock { reads } } + var writeCount: Int { lock.withLock { writes } } + var deliveredCount: Int { lock.withLock { delivered } } + + func enqueue(_ action: Action) { + lock.withLock { actions.append(action) } + } + + func openWindow() { + lock.withLock { sendWindow = true } + } + + func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult { + let (action, readNumber) = lock.withLock { () -> (Action, Int) in + reads += 1 + let action = actions.isEmpty ? fallback : actions.removeFirst() + if case .wouldBlock = action { lastBlock = readBlocks } else { lastBlock = [] } + return (action, reads) + } + onRead?(readNumber) + switch action { + case .data(let data): + let length = min(data.count, count) + buffer.withMemoryRebound(to: UInt8.self, capacity: length) { destination in + _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: destination, count: length)) + } + lock.withLock { delivered += length } + return .bytes(length) + case .wouldBlock: + return .wouldBlock + case .closed: + return .closed + case .transportFailed: + return .transportFailed + } + } + + func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult { + lock.withLock { + writes += 1 + guard sendWindow else { + lastBlock = .inbound + return .wouldBlock + } + let result = writeResults.isEmpty ? (writeFallback ?? .bytes(count)) : writeResults.removeFirst() + lastBlock = result == .wouldBlock ? writeBlocks : [] + guard case .bytes(let accepted) = result else { return result } + let length = min(accepted, count) + buffer.withMemoryRebound(to: UInt8.self, capacity: length) { source in + writtenBuffer.append(contentsOf: UnsafeBufferPointer(start: source, count: length)) + } + return .bytes(length) + } + } + + func blockDirections() -> RelayDirections { + lock.withLock { lastBlock } + } + + func hasQueuedInput() -> Bool { + lock.withLock { !actions.isEmpty } + } + + func hasSendWindow() -> Bool { + lock.withLock { sendWindow } + } +} + +private func send(_ data: Data, to descriptor: Int32) { + data.withUnsafeBytes { raw in + _ = Darwin.send(descriptor, raw.baseAddress, raw.count, 0) + } +} + +private func receive(from descriptor: Int32, count: Int) -> Data { + var buffer = [UInt8](repeating: 0, count: count) + var received = 0 + while received < count { + let got = buffer.withUnsafeMutableBytes { raw in + recv(descriptor, raw.baseAddress?.advanced(by: received), count - received, 0) + } + if got <= 0 { break } + received += got + } + return Data(buffer.prefix(received)) +} + +private func readsEndOfFile(_ descriptor: Int32) -> Bool { + var pollFD = pollfd(fd: descriptor, events: Int16(POLLIN), revents: 0) + guard poll(&pollFD, 1, 2_000) > 0 else { return false } + var byte: UInt8 = 0 + return recv(descriptor, &byte, 1, 0) == 0 +} + +// The client side feeds the relay whole 32 KB reads, so the 8 KB transport is what the upload waits on. +private func enlargeBuffers(_ pair: SocketPair) { + var size: Int32 = 1 << 20 + for descriptor in [pair.a, pair.b] { + setsockopt(descriptor, SOL_SOCKET, SO_SNDBUF, &size, socklen_t(MemoryLayout.size)) + setsockopt(descriptor, SOL_SOCKET, SO_RCVBUF, &size, socklen_t(MemoryLayout.size)) + } +} + +private func fillSendBuffer(_ descriptor: Int32) { + let flags = fcntl(descriptor, F_GETFL, 0) + _ = fcntl(descriptor, F_SETFL, flags | O_NONBLOCK) + var chunk = [UInt8](repeating: 0x46, count: 4_096) + while Darwin.send(descriptor, &chunk, chunk.count, 0) > 0 {} +} + +private func drain(_ descriptor: Int32) { + var buffer = [UInt8](repeating: 0, count: 65_536) + while recv(descriptor, &buffer, buffer.count, MSG_DONTWAIT) > 0 {} +} + +// A session that, like libssh2, keeps one packet at a time on its way and refuses every other channel's write +// until the channel that started it sends the rest. Packets go into the real transport socket, so its writability +// is what a write source sees. The outbound bit follows libssh2's: set when a send would block, left alone by a +// partial retry, cleared once the packet is out, and cleared outright by a write that finds no window. +internal final class OnePacketSession: @unchecked Sendable { + private let lock = NSLock() + private let transportFD: Int32 + private var owner: ObjectIdentifier? + private var outbound = false + private var remaining = 0 + private var payload = 0 + private let filler = [UInt8](repeating: 0x50, count: 32_768) + + // Non-blocking as libssh2 leaves its socket: Darwin does not honor MSG_DONTWAIT on a send to a Unix socket. + init(transportFD: Int32) { + self.transportFD = transportFD + let flags = fcntl(transportFD, F_GETFL, 0) + _ = fcntl(transportFD, F_SETFL, flags | O_NONBLOCK) + } + + var isSending: Bool { + lock.withLock { owner != nil } + } + + var outboundBit: Bool { + lock.withLock { outbound } + } + + func channel(windowClosed: Bool = false) -> OnePacketChannel { + OnePacketChannel(session: self, windowClosed: windowClosed) + } + + func writeWithNoWindow() -> ChannelWriteResult { + lock.withLock { + outbound = false + return .wouldBlock + } + } + + func write(from channel: OnePacketChannel, count: Int) -> ChannelWriteResult { + lock.withLock { + let id = ObjectIdentifier(channel) + if let owner { + guard owner == id else { return .wouldBlock } + let sent = push(remaining) + if sent == 0 { outbound = true } + remaining -= sent + guard remaining == 0 else { return .wouldBlock } + self.owner = nil + outbound = false + return .bytes(payload) + } + let packet = min(count, filler.count) + let sent = push(packet) + guard sent == packet else { + owner = id + outbound = true + remaining = packet - sent + payload = packet + return .wouldBlock + } + outbound = false + return .bytes(packet) + } + } + + private func push(_ count: Int) -> Int { + let sent = filler.withUnsafeBytes { Darwin.send(transportFD, $0.baseAddress, count, 0) } + return max(0, sent) + } +} + +internal final class OnePacketChannel: SSHChannelIO, @unchecked Sendable { + private let session: OnePacketSession + private let windowClosed: Bool + private let lock = NSLock() + private var written = 0 + private var writes = 0 + + init(session: OnePacketSession, windowClosed: Bool) { + self.session = session + self.windowClosed = windowClosed + } + + var writtenCount: Int { lock.withLock { written } } + var writeCount: Int { lock.withLock { writes } } + + func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult { .wouldBlock } + + func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult { + lock.withLock { writes += 1 } + if windowClosed { return session.writeWithNoWindow() } + let result = session.write(from: self, count: count) + if case .bytes(let accepted) = result { + lock.withLock { written += accepted } + } + return result + } + + func blockDirections() -> RelayDirections { session.outboundBit ? .outbound : [] } + + func hasQueuedInput() -> Bool { false } + + func hasSendWindow() -> Bool { !windowClosed } +} + +// Reads the far end of the transport at about 4 MB/s, so an upload fills the socket and waits on it. It reads a +// descriptor of its own, so it never reads a number the test has closed and the kernel handed to another test. +internal final class SlowDrain: @unchecked Sendable { + private let stopped = OSAllocatedUnfairLock(initialState: false) + private let tally = ByteTally() + + var drained: Int { tally.value } + + init(_ descriptor: Int32) { + let stopped = self.stopped + let tally = self.tally + let own = dup(descriptor) + let thread = Thread { + defer { Darwin.close(own) } + var buffer = [UInt8](repeating: 0, count: 16_384) + while !stopped.withLock({ $0 }) { + let count = recv(own, &buffer, buffer.count, MSG_DONTWAIT) + if count <= 0 { + usleep(1_000) + continue + } + tally.add(count) + usleep(4_000) + } + } + thread.start() + } + + func stop() { + stopped.withLock { $0 = true } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/TestSupport/SocketPair.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/TestSupport/SocketPair.swift index d9b4f1efef..1956f527fd 100644 --- a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/TestSupport/SocketPair.swift +++ b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/TestSupport/SocketPair.swift @@ -9,9 +9,9 @@ import Foundation -/// A connected pair of local socket fds, used to drive relay and channel-open tests over -/// real sockets rather than fakes, so closing behaviour is observed the way a database -/// client would observe it. +// A connected pair of local socket fds, used to drive relay and channel-open tests over +// real sockets rather than fakes, so closing behaviour is observed the way a database +// client would observe it. internal final class SocketPair { private(set) var a: Int32 private(set) var b: Int32 @@ -29,6 +29,13 @@ internal final class SocketPair { func closeB() { Self.close(&b) } + // Hands `a` to an owner that closes it, so `close()` leaves it alone. + func takeA() -> Int32 { + let descriptor = a + a = -1 + return descriptor + } + func close() { Self.close(&a) Self.close(&b) diff --git a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteExecChannelOpener.swift b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteExecChannelOpener.swift index 13651ee288..74a1690600 100644 --- a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteExecChannelOpener.swift +++ b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteExecChannelOpener.swift @@ -8,16 +8,16 @@ import Foundation import TableProSSHTransport -/// Opens one session channel and starts a command on it, one non-blocking attempt at a time, so -/// `SSHForwardChannelOpenPump` can drive it to a decision within an app-owned deadline exactly as it -/// drives a forwarding open. A forwarding channel opens in a single call; an exec channel is two -/// steps, the channel open and the `exec` request, and either can report `LIBSSH2_ERROR_EAGAIN`, so -/// the phase is remembered between attempts. -/// -/// The opener owns the channel until it hands it back as `.opened`. If the pump times out or is -/// cancelled after the channel was created but before it was handed over, the caller must call -/// `abort()` to free it, because dropping the Swift reference does not free the libssh2 channel and -/// the session stays live. +// Opens one session channel and starts a command on it, one non-blocking attempt at a time, so +// `SSHForwardChannelOpenPump` can drive it to a decision within an app-owned deadline exactly as it +// drives a forwarding open. A forwarding channel opens in a single call; an exec channel is two +// steps, the channel open and the `exec` request, and either can report `LIBSSH2_ERROR_EAGAIN`, so +// the phase is remembered between attempts. +// +// The opener owns the channel until it hands it back as `.opened`. If the pump times out or is +// cancelled after the channel was created but before it was handed over, the caller takes it back +// with `abandon()` and frees it, because dropping the Swift reference does not free the libssh2 +// channel and the session stays live. final class RemoteSQLiteExecChannelOpener: SSHForwardChannelOpening { private enum Phase { case opening @@ -28,18 +28,32 @@ final class RemoteSQLiteExecChannelOpener: SSHForwardChannelOpening { private let session: OpaquePointer private let command: String - private let sessionQueue: DispatchQueue + private let pump: SSHSessionPump + private let turn: SSHSendTurn private var phase: Phase = .opening private var channel: OpaquePointer? - init(session: OpaquePointer, command: String, sessionQueue: DispatchQueue) { + private static let abandonSeconds: TimeInterval = 5 + + init(session: OpaquePointer, command: String, pump: SSHSessionPump) { self.session = session self.command = command - self.sessionQueue = sessionQueue + self.pump = pump + self.turn = SSHSendTurn(pump: pump) } func attemptOpen() -> SSHForwardChannelAttempt { - sessionQueue.sync { + let attempt = attemptOnce() + if case .wouldBlock(let directions) = attempt { + turn.waitsToSend(directions.contains(.outbound)) + } else { + turn.end() + } + return attempt + } + + private func attemptOnce() -> SSHForwardChannelAttempt { + pump.perform { switch phase { case .opening: return openChannel() @@ -53,10 +67,24 @@ final class RemoteSQLiteExecChannelOpener: SSHForwardChannelOpening { } } - /// Frees a channel that was opened but never handed to the relay, for an open the pump timed out - /// or cancelled. A no-op once the channel has been handed over or already freed. - func abort() { - sessionQueue.sync { freeChannel() } + // Hands back a channel that was opened but never handed to the relay, for an open that failed, timed out or + // was cancelled; nil once handed over. A request still half sent sits in the channel's own buffer, and only + // this opener's next attempt finishes it, so the attempts go on first while they wait to send. + func abandon(transportFD: Int32) -> OpaquePointer? { + let deadline = Date().addingTimeInterval(Self.abandonSeconds) + while Date() < deadline { + let attempt = attemptOpen() + if case .opened(let opened) = attempt { return opened } + guard case .wouldBlock(let directions) = attempt, directions.contains(.outbound) else { break } + _ = pollReady(fd: transportFD, directions: .outbound, timeoutMs: LibSSH2ForwardChannel.openPollTimeoutMs) + } + turn.end() + return pump.perform { + let abandoned = channel + channel = nil + phase = .finished + return abandoned + } } private func openChannel() -> SSHForwardChannelAttempt { @@ -78,14 +106,13 @@ final class RemoteSQLiteExecChannelOpener: SSHForwardChannelOpening { if rc == LIBSSH2_ERROR_EAGAIN { return .wouldBlock(RelayDirections(libssh2BlockDirections: libssh2_session_block_directions(session))) } - let message = LibSSH2ForwardChannel.lastErrorMessage(session: session) - freeChannel() - return .failed(code: rc, message: message) + phase = .finished + return .failed(code: rc, message: LibSSH2ForwardChannel.lastErrorMessage(session: session)) } - /// Discards the agent's standard error rather than merging it into the protocol stream. A merge - /// would corrupt the framed replies; leaving it unread would let a large Python traceback fill - /// the channel window and stall the relay, which reads standard output alone. + // Discards the agent's standard error rather than merging it into the protocol stream. A merge + // would corrupt the framed replies; leaving it unread would let a large Python traceback fill + // the channel window and stall the relay, which reads standard output alone. private func configure() -> SSHForwardChannelAttempt { guard let channel else { return .failed(code: 0, message: "no channel to configure") } let rc = libssh2_channel_handle_extended_data2(channel, LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE) @@ -104,12 +131,4 @@ final class RemoteSQLiteExecChannelOpener: SSHForwardChannelOpening { } return .wouldBlock(RelayDirections(libssh2BlockDirections: libssh2_session_block_directions(session))) } - - private func freeChannel() { - if let channel { - libssh2_channel_free(channel) - self.channel = nil - } - phase = .finished - } } diff --git a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift index d44e922406..0d65c420ac 100644 --- a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift +++ b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTransportManager.swift @@ -26,7 +26,6 @@ actor RemoteSQLiteTransportManager: TunnelManaging { private var tunnels: [UUID: RemoteSQLiteTunnel] = [:] private var creations = TunnelCreationGenerations() private static let tunnelRegistry = OSAllocatedUnfairLock(initialState: [UUID: RemoteSQLiteTunnel]()) - private var appNapActivity: NSObjectProtocol? private init() {} @@ -118,7 +117,6 @@ actor RemoteSQLiteTransportManager: TunnelManaging { Self.tunnelRegistry.withLock { $0[connectionId] = tunnel } tunnel.startForwarding() tunnel.startKeepAlive() - updateAppNapState() Self.logger.info("Remote SQLite session ready for \(connectionId) on 127.0.0.1:\(tunnel.localPort)") return RemoteSQLiteEndpoint(port: tunnel.localPort, token: token) @@ -133,7 +131,6 @@ actor RemoteSQLiteTransportManager: TunnelManaging { private func discardTunnel(connectionId: UUID) async throws { guard let tunnel = tunnels.removeValue(forKey: connectionId) else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } - updateAppNapState() tunnel.close() } @@ -165,7 +162,6 @@ actor RemoteSQLiteTransportManager: TunnelManaging { private func handleTunnelDeath(connectionId: UUID) async { guard tunnels.removeValue(forKey: connectionId) != nil else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } - updateAppNapState() Self.logger.warning("Remote SQLite session died for \(connectionId)") await DatabaseManager.shared.handleRemoteSQLiteTunnelDied(connectionId: connectionId) } @@ -197,16 +193,4 @@ actor RemoteSQLiteTransportManager: TunnelManaging { } return listenFD } - - private func updateAppNapState() { - if !tunnels.isEmpty && appNapActivity == nil { - appNapActivity = ProcessInfo.processInfo.beginActivity( - options: .userInitiatedAllowingIdleSystemSleep, - reason: "Remote SQLite session keepalive requires timely execution" - ) - } else if tunnels.isEmpty, let activity = appNapActivity { - ProcessInfo.processInfo.endActivity(activity) - appNapActivity = nil - } - } } diff --git a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTunnel.swift b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTunnel.swift index 4e406585bb..a38d51ac14 100644 --- a/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTunnel.swift +++ b/apple/TablePro/Core/RemoteSQLite/RemoteSQLiteTunnel.swift @@ -37,15 +37,11 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { private var keepAliveTask: Task? private let aliveLatch = TeardownLatch() - /// The relays still running, so teardown frees the session only once none of them can touch - /// it. A group rather than a collection of tasks: a relay leaves it by finishing, which is the - /// one thing a `[Task]` pruned on `isCancelled` never noticed, so a tunnel that had served - /// clients carried every one of them until it closed. It is also all a task was ever worth - /// here, since the relay runs on `relayQueue` outside the task's cancellation scope and stops - /// on `aliveLatch` rather than on `Task.isCancelled`. + // Each accepted client from accept until its channel is freed or given up, so teardown frees the + // session only once none of them can touch it. private let clientRelays = DispatchGroup() - private let sessionQueue: DispatchQueue + private let pump: SSHSessionPump private let relayQueue: DispatchQueue private let acceptQueue: DispatchQueue @@ -54,6 +50,10 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { private let byteCounter = TransportByteCounter() private static let relayBufferSize = 32_768 + + private struct ChannelHandle: @unchecked Sendable { + let pointer: OpaquePointer + } private static let admissionTimeoutSeconds = 5 init( @@ -73,7 +73,10 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { self.token = token self.relayDeadlines = ConnectionRelayDeadlineProvider(initialDeadline: connectionDeadline) self.createdAt = Date() - self.sessionQueue = DispatchQueue(label: "com.TablePro.rsqlite.session.\(connectionId.uuidString)", qos: .utility) + self.pump = SSHSessionPump( + transportFD: chain.socketFD, + queue: DispatchQueue(label: "com.TablePro.rsqlite.session.\(connectionId.uuidString)", qos: .utility) + ) self.relayQueue = DispatchQueue( label: "com.TablePro.rsqlite.relay.\(connectionId.uuidString)", qos: .utility, attributes: .concurrent ) @@ -93,7 +96,7 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { // MARK: - Forwarding func startForwarding() { - sessionQueue.sync { libssh2_session_set_blocking(session, 0) } + pump.perform { libssh2_session_set_blocking(session, 0) } // A handler already running when the latch is claimed may still spawn one relay; teardown // waits for the cancel handler before the relay group, so it waits for that relay too. @@ -110,23 +113,19 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { } func startKeepAlive() { - sessionQueue.sync { libssh2_keepalive_config(session, 1, 30) } + pump.perform { LibSSH2KeepAlive.configure(session: session) } keepAliveTask = Task.detached { [weak self] in guard let self else { return } while !Task.isCancelled && self.isRunning { - let failed = await withCheckedContinuation { (continuation: CheckedContinuation) in - self.sessionQueue.async { - var secondsToNext: Int32 = 0 - let rc = libssh2_keepalive_send(self.session, &secondsToNext) - continuation.resume(returning: sshKeepAliveDidFail(rc)) - } + let outcome = await self.pump.performAsync { + LibSSH2KeepAlive.send(session: self.session, transportFD: self.socketFD) } - if failed { + if outcome.failed { Self.logger.warning("Remote SQLite keepalive failed, marking dead") self.markDead() break } - try? await Task.sleep(for: .seconds(10)) + try? await Task.sleep(for: .seconds(outcome.secondsToNext)) } } } @@ -144,6 +143,7 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { guard aliveLatch.claim() else { return } acceptor?.cancel() keepAliveTask?.cancel() + pump.stop() shutdown(socketFD, SHUT_RDWR) } @@ -153,16 +153,19 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { onDeath?(connectionId) } - // `shutdown` wakes every poll on the transport but keeps the descriptor number, so no other - // socket can take it mid-poll. The listening socket closes only after the acceptor's cancel - // handler ran, which GCD runs once no accept handler is running. The relays are awaited after - // that, because the accept handler is the only thing that starts one: the group can only empty. + // `shutdown` wakes every open or free waiting on the transport but keeps the descriptor number, + // so no other socket can take it mid-poll. The listening socket closes only after the acceptor's + // cancel handler ran, which GCD runs once no accept handler is running. The clients are awaited + // after that, because the accept handler is the only thing that starts one: the group can only + // empty. The pump is awaited last, so no source of it can fire on the descriptors closed below. private func performTeardown() { acceptor?.cancel() keepAliveTask?.cancel() + pump.stop() shutdown(socketFD, SHUT_RDWR) + let pump = self.pump let chain = self.chain let relayQueue = self.relayQueue let clientRelays = self.clientRelays @@ -176,6 +179,7 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { await withCheckedContinuation { (continuation: CheckedContinuation) in clientRelays.notify(queue: relayQueue) { continuation.resume() } } + await pump.waitUntilStopped() Darwin.close(listenFD) LibSSH2TunnelFactory.cleanupChain(chain, reason: "Closing remote SQLite session") @@ -192,19 +196,22 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { let clientRelays = self.clientRelays clientRelays.enter() relayQueue.async { [weak self] in - defer { clientRelays.leave() } guard let self else { Darwin.close(clientFD) + clientRelays.leave() return } - self.admitAndRelay(clientFD: clientFD, budget: budget) + if !self.admitAndRelay(clientFD: clientFD, budget: budget) { + clientRelays.leave() + } } } + // True once the relay owns the client, which then leaves `clientRelays` when its channel is freed. private func admitAndRelay( clientFD: Int32, budget: ConnectionRelayDeadlineProvider.Budget - ) { + ) -> Bool { let connectionDeadline = budget.deadline guard let line = Self.readAdmissionLine(clientFD, deadline: connectionDeadline), RemoteSQLiteAdmission.isAuthorized(line: line, token: token) else { @@ -213,11 +220,11 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { } Self.logger.warning("Remote SQLite client rejected: bad or missing token") Darwin.close(clientFD) - return + return false } - let opener = RemoteSQLiteExecChannelOpener(session: session, command: command, sessionQueue: sessionQueue) - let pump = SSHForwardChannelOpenPump( + let opener = RemoteSQLiteExecChannelOpener(session: session, command: command, pump: pump) + let openPump = SSHForwardChannelOpenPump( opener: opener, isActive: { [weak self] in guard self?.isRunning == true else { return false } @@ -238,7 +245,7 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { } ) - let outcome = pump.run() + let outcome = openPump.run() let initialDeadlineTimedOut: Bool switch outcome { case .timedOut: @@ -253,22 +260,26 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { if case .opened(let channel) = outcome { Darwin.close(clientFD) freeChannel(channel) - return + return false } } switch outcome { case .opened(let channel): - runRelay(clientFD: clientFD, channel: channel) + relayChannel(clientFD: clientFD, channel: channel) + return true case .failed(let code, let message): Self.logger.error("Remote SQLite exec channel failed to open, libssh2 \(code): \(message)") Darwin.close(clientFD) case .timedOut, .cancelled: - /// The channel may have opened before the deadline and be waiting on the exec request. - /// The pump never took ownership, so free it here rather than leak it on the live session. - opener.abort() Darwin.close(clientFD) } + // The channel may have opened and then failed or run out of time on the exec request. The open + // pump never took ownership, so free it here rather than leak it on the live session. + if let channel = opener.abandon(transportFD: socketFD) { + freeAbandonedChannel(channel) + } + return false } private func recordConnectionTimeout(_ deadline: ConnectionDeadline) { @@ -317,26 +328,24 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { return nil } - private func runRelay(clientFD: Int32, channel: OpaquePointer) { - let relay = SSHChannelRelay( + private func relayChannel(clientFD: Int32, channel: OpaquePointer) { + let clientRelays = self.clientRelays + let handle = ChannelHandle(pointer: channel) + pump.relay( localFD: clientFD, - transportFD: socketFD, - channelIO: LibSSH2ChannelIO(channel: channel, session: session, sessionQueue: sessionQueue), + channel: LibSSH2ChannelIO(channel: channel, session: session), bufferSize: Self.relayBufferSize, - isActive: { [weak self] in self?.isRunning ?? false }, byteCounter: byteCounter - ) - - let termination = relay.run() - Darwin.close(clientFD) - - guard isRunning else { return } + ) { [weak self] termination in + defer { clientRelays.leave() } + guard let self, self.isRunning else { return } - freeChannel(channel) + self.freeChannel(handle.pointer) - if termination == .transportHangup { - Self.logger.info("Remote SQLite transport hung up for \(self.connectionId)") - markDead() + if termination == .transportHangup { + Self.logger.info("Remote SQLite transport hung up for \(self.connectionId)") + self.markDead() + } } } @@ -346,11 +355,25 @@ final class RemoteSQLiteTunnel: @unchecked Sendable { _ = LibSSH2ChannelFree.run( channel: channel, session: session, - sessionQueue: sessionQueue, + pump: pump, transportFD: socketFD, - sendEOF: true, deadline: .distantFuture, isActive: { [weak self] in self?.isRunning ?? false } ) } + + // A channel given up on mid-open may hold its exec request half sent, which no call finishes once the + // open stopped, so its free has the keep-alive's limit and a stuck one ends the session. + private func freeAbandonedChannel(_ channel: OpaquePointer) { + let outcome = LibSSH2ChannelFree.run( + channel: channel, + session: session, + pump: pump, + transportFD: socketFD, + isActive: { [weak self] in self?.isRunning ?? false } + ) + guard outcome == .timedOut else { return } + Self.logger.warning("Remote SQLite channel did not close within \(Int(LibSSH2ChannelFree.deadlineSeconds))s, marking dead") + markDead() + } } diff --git a/apple/TablePro/Core/SSH/LibSSH2ChannelFree.swift b/apple/TablePro/Core/SSH/LibSSH2ChannelFree.swift index 22bec799c0..bdcaa5d937 100644 --- a/apple/TablePro/Core/SSH/LibSSH2ChannelFree.swift +++ b/apple/TablePro/Core/SSH/LibSSH2ChannelFree.swift @@ -16,29 +16,43 @@ nonisolated internal enum LibSSH2ChannelFree { // The keep-alive's own limit: a packet still unsent by then holds every other send on the session. static let deadlineSeconds: TimeInterval = 30 - // Another relay can read the CLOSE reply off the shared socket and leave it quiet, so free runs - // again after every wait. An EOF that would block is sent again first: libssh2 keeps it as the - // session's next packet and turns every other send away until the same call sends it. + // Another relay can read the CLOSE reply off the shared socket and leave it quiet, so free runs again after + // every wait. The EOF goes first, on its own: channel_close would send it anyway, from a packet on its stack + // that a retry from another frame cannot finish (stackPacketReadiness), so it waits for room to go whole. A + // wait on another packet is bounded by the poll timeout, as only that packet's own call can finish it. static func run( channel: OpaquePointer, session: OpaquePointer, - sessionQueue: DispatchQueue, + pump: SSHSessionPump, transportFD: Int32, - sendEOF: Bool = false, deadline: Date = Date().addingTimeInterval(deadlineSeconds), isActive: @escaping () -> Bool ) -> SSHChannelFreeOutcome { - var eofPending = sendEOF + var eofSent = false + let turn = SSHSendTurn(pump: pump) + defer { turn.end() } return SSHChannelFreePump( attemptFree: { - sessionQueue.sync { - if eofPending { + let attempt: SSHChannelFreeAttempt = pump.perform { + if !eofSent { + switch stackPacketReadiness(session: session, transportFD: transportFD) { + case .socketFull: return .wouldBlock(.outbound) + case .sendPending: return .wouldBlock(.inbound) + case .ready, .gone: break + } guard libssh2_channel_send_eof(channel) != LIBSSH2_ERROR_EAGAIN else { return blocked(session) } - eofPending = false + eofSent = true } guard libssh2_channel_free(channel) == LIBSSH2_ERROR_EAGAIN else { return .finished } return blocked(session) } + // Past the EOF and the CLOSE the free only waits for the server's reply. + if case .wouldBlock(let directions) = attempt { + turn.waitsToSend(!eofSent || directions.contains(.outbound)) + } else { + turn.end() + } + return attempt }, isActive: isActive, deadline: deadline, diff --git a/apple/TablePro/Core/SSH/LibSSH2ChannelIO.swift b/apple/TablePro/Core/SSH/LibSSH2ChannelIO.swift index 5d6111c364..8d76a048c5 100644 --- a/apple/TablePro/Core/SSH/LibSSH2ChannelIO.swift +++ b/apple/TablePro/Core/SSH/LibSSH2ChannelIO.swift @@ -12,11 +12,10 @@ import Foundation import CLibSSH2 import TableProSSHTransport -// Every call goes through the serial sessionQueue: libssh2 is not thread-safe per session. -nonisolated internal struct LibSSH2ChannelIO: SSHChannelIO { +// libssh2 is not thread-safe per session: the session's SSHSessionPump makes every call on its queue. +nonisolated internal struct LibSSH2ChannelIO: SSHChannelIO, @unchecked Sendable { let channel: OpaquePointer let session: OpaquePointer - let sessionQueue: DispatchQueue // libssh2 hands a transport error (-43 on EOF, -13 on DISCONNECT, -12, a failed re-key) to every channel call; only these end just one channel. private static let channelScopedErrors: Set = [ @@ -45,20 +44,34 @@ nonisolated internal struct LibSSH2ChannelIO: SSHChannelIO { return channelScopedErrors.contains(returnCode) ? .closed : .transportFailed } + // A read after the server's EOF answers EAGAIN only while its window adjust waits to send (channel.c, + // ssh2_channel_read), and freeing the channel with that packet half sent leaves every later send refused. func read(into buffer: UnsafeMutablePointer, count: Int) -> ChannelReadResult { - let result = sessionQueue.sync { Int(tablepro_libssh2_channel_read(channel, buffer, count)) } - if result == Int(LIBSSH2_ERROR_EAGAIN), sessionQueue.sync(execute: { libssh2_channel_eof(channel) }) != 0 { + let result = Int(tablepro_libssh2_channel_read(channel, buffer, count)) + if result == Int(LIBSSH2_ERROR_EAGAIN), libssh2_channel_eof(channel) != 0, !blockDirections().contains(.outbound) { return .closed } return Self.readResult(result) } func write(_ buffer: UnsafePointer, count: Int) -> ChannelWriteResult { - Self.writeResult(sessionQueue.sync { Int(tablepro_libssh2_channel_write(channel, buffer, count)) }) + Self.writeResult(Int(tablepro_libssh2_channel_write(channel, buffer, count))) } func blockDirections() -> RelayDirections { - RelayDirections(libssh2BlockDirections: sessionQueue.sync { libssh2_session_block_directions(session) }) + RelayDirections(libssh2BlockDirections: libssh2_session_block_directions(session)) + } + + // read_avail counts the bytes another call already queued for this channel (channel.c, libssh2_channel_window_read_ex). + func hasQueuedInput() -> Bool { + var queued: UInt = 0 + _ = libssh2_channel_window_read_ex(channel, &queued, nil) + return queued > 0 || libssh2_channel_eof(channel) != 0 + } + + // ssh2_channel_write answers EAGAIN with no window before it builds a packet, and with window once the packet is built. + func hasSendWindow() -> Bool { + libssh2_channel_window_write_ex(channel, nil) > 0 } } diff --git a/apple/TablePro/Core/SSH/LibSSH2ForwardChannel.swift b/apple/TablePro/Core/SSH/LibSSH2ForwardChannel.swift index bc5ca4063c..19bb161e59 100644 --- a/apple/TablePro/Core/SSH/LibSSH2ForwardChannel.swift +++ b/apple/TablePro/Core/SSH/LibSSH2ForwardChannel.swift @@ -12,17 +12,28 @@ import Foundation import CLibSSH2 import TableProSSHTransport -/// Bridges `SSHForwardChannelOpening` to libssh2. Each attempt takes the session queue -/// only long enough to try the open and read back the error and block directions, so a -/// slow open never holds the queue against relays or keep-alive. +// Bridges `SSHForwardChannelOpening` to libssh2. Each attempt takes the session queue +// only long enough to try the open and read back the error and block directions, so a +// slow open never holds the queue against relays or keep-alive. nonisolated internal struct LibSSH2ForwardChannelOpener: SSHForwardChannelOpening { let session: OpaquePointer let destination: SSHForwardDestination let originPort: Int - let sessionQueue: DispatchQueue + let pump: SSHSessionPump + let turn: SSHSendTurn func attemptOpen() -> SSHForwardChannelAttempt { - sessionQueue.sync { + let attempt = openOnce() + if case .wouldBlock(let directions) = attempt { + turn.waitsToSend(directions.contains(.outbound)) + } else { + turn.end() + } + return attempt + } + + private func openOnce() -> SSHForwardChannelAttempt { + pump.perform { if let channel = LibSSH2ForwardChannel.open( session: session, destination: destination, @@ -41,9 +52,9 @@ nonisolated internal struct LibSSH2ForwardChannelOpener: SSHForwardChannelOpenin } } -/// Opens the channel that carries forwarded traffic to the destination. The two libssh2 -/// entry points behave identically to the caller: both return a channel that reads and -/// writes the same way, and both signal "not ready yet" with `LIBSSH2_ERROR_EAGAIN`. +// Opens the channel that carries forwarded traffic to the destination. The two libssh2 +// entry points behave identically to the caller: both return a channel that reads and +// writes the same way, and both signal "not ready yet" with `LIBSSH2_ERROR_EAGAIN`. nonisolated internal enum LibSSH2ForwardChannel { // Another open or relay can read this channel's reply into libssh2's queue and leave the socket // quiet, so a quiet wait is a retry and only the deadline ends an open. diff --git a/apple/TablePro/Core/SSH/LibSSH2JumpRelay.swift b/apple/TablePro/Core/SSH/LibSSH2JumpRelay.swift index cf66fe5429..83602c02d7 100644 --- a/apple/TablePro/Core/SSH/LibSSH2JumpRelay.swift +++ b/apple/TablePro/Core/SSH/LibSSH2JumpRelay.swift @@ -12,13 +12,15 @@ import Foundation import TableProSSHTransport nonisolated internal enum LibSSH2JumpRelay { - // The relay is the only thing that touches these once it starts, which the compiler cannot see + // The hop's pump is the only thing that touches these once it starts, which the compiler cannot see // through an OpaquePointer. nonisolated private struct Handles: @unchecked Sendable { let channel: OpaquePointer let session: OpaquePointer } + // The hop session carries one channel, relayed to `localFD`, which the pump closes. The fence finishes once + // the pump has stopped, so teardown frees the channel and session only after the pump's last libssh2 call. static func start( channel: OpaquePointer, localFD: Int32, @@ -26,36 +28,20 @@ nonisolated internal enum LibSSH2JumpRelay { session: OpaquePointer, sessionQueue: DispatchQueue ) -> SSHJumpRelayFence { - let relayQueue = DispatchQueue( - label: "com.TablePro.ssh.hop-relay", - qos: .utility - ) let handles = Handles(channel: channel, session: session) + let pump = SSHSessionPump(transportFD: transportFD, queue: sessionQueue) let fence = SSHJumpRelayFence { - shutdown(localFD, SHUT_RDWR) - shutdown(transportFD, SHUT_RDWR) + pump.stop() } - relayQueue.async { - defer { - fence.finish { - shutdown(localFD, SHUT_RDWR) - Darwin.close(localFD) - } - } - - let relay = SSHChannelRelay( - localFD: localFD, - transportFD: transportFD, - channelIO: LibSSH2ChannelIO( - channel: handles.channel, - session: handles.session, - sessionQueue: sessionQueue - ), - bufferSize: 32_768, - isActive: { fence.isActive } - ) - - _ = relay.run() + pump.whenStopped { + fence.finish() + } + pump.relay( + localFD: localFD, + channel: LibSSH2ChannelIO(channel: handles.channel, session: handles.session), + bufferSize: 32_768 + ) { _ in + pump.stop() } return fence } diff --git a/apple/TablePro/Core/SSH/LibSSH2Tunnel.swift b/apple/TablePro/Core/SSH/LibSSH2Tunnel.swift index a339bfdf6b..43cf592a3d 100644 --- a/apple/TablePro/Core/SSH/LibSSH2Tunnel.swift +++ b/apple/TablePro/Core/SSH/LibSSH2Tunnel.swift @@ -31,20 +31,14 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { private var keepAliveTask: Task? private let aliveLatch = TeardownLatch() - /// The relays still running, so teardown frees the session only once none of them can touch - /// it. A group rather than a collection of tasks: a relay leaves it by finishing, which is the - /// one thing a `[Task]` pruned on `isCancelled` never noticed, so a tunnel that had served - /// clients carried every one of them until it closed. It is also all a task was ever worth - /// here, since the relay runs on `relayQueue` outside the task's cancellation scope and stops - /// on `aliveLatch` rather than on `Task.isCancelled`. + // Each accepted client from accept until its channel is freed or given up, so teardown frees the + // session only once none of them can touch it. private let clientRelays = DispatchGroup() - /// Serial queue for all libssh2 calls on this tunnel's session. - /// libssh2 is not thread-safe per session, so every call must be serialized. - private let sessionQueue: DispatchQueue + // Every libssh2 call on the session, and the relays of its channels. + private let pump: SSHSessionPump - /// Concurrent queue for relay I/O (poll, send, recv — no libssh2 calls). - /// Individual libssh2 calls within each relay are dispatched to `sessionQueue`. + // Concurrent: channel opens, which wait on the transport between attempts. private let relayQueue: DispatchQueue // Serial: the acceptor's handler, which accepts and makes no libssh2 call. @@ -72,6 +66,10 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { private static let relayBufferSize = 32_768 // 32KB + private struct ChannelHandle: @unchecked Sendable { + let pointer: OpaquePointer + } + init( connectionId: UUID, localPort: Int, @@ -93,9 +91,9 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { self.timeoutEndpoint = timeoutEndpoint self.jumpChain = jumpChain self.createdAt = Date() - self.sessionQueue = DispatchQueue( - label: "com.TablePro.ssh.session.\(connectionId.uuidString)", - qos: .utility + self.pump = SSHSessionPump( + transportFD: socketFD, + queue: DispatchQueue(label: "com.TablePro.ssh.session.\(connectionId.uuidString)", qos: .utility) ) self.relayQueue = DispatchQueue( label: "com.TablePro.ssh.relay.\(connectionId.uuidString)", @@ -116,7 +114,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { // MARK: - Forwarding func startForwarding(destination: SSHForwardDestination) { - sessionQueue.sync { libssh2_session_set_blocking(session, 0) } + pump.perform { libssh2_session_set_blocking(session, 0) } // A handler already running when the latch is claimed may still spawn one relay; teardown // waits for the cancel handler before the relay group, so it waits for that relay too. @@ -141,27 +139,23 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { // MARK: - Keep-Alive func startKeepAlive() { - sessionQueue.sync { libssh2_keepalive_config(session, 1, 30) } + pump.perform { LibSSH2KeepAlive.configure(session: session) } keepAliveTask = Task.detached { [weak self] in guard let self else { return } while !Task.isCancelled && self.isRunning { - let failed = await withCheckedContinuation { (continuation: CheckedContinuation) in - self.sessionQueue.async { - var secondsToNext: Int32 = 0 - let rc = libssh2_keepalive_send(self.session, &secondsToNext) - continuation.resume(returning: sshKeepAliveDidFail(rc)) - } + let outcome = await self.pump.performAsync { + LibSSH2KeepAlive.send(session: self.session, transportFD: self.socketFD) } - if failed { + if outcome.failed { Self.logger.warning("Keep-alive failed, marking tunnel dead") self.markDead() break } - try? await Task.sleep(for: .seconds(10)) + try? await Task.sleep(for: .seconds(outcome.secondsToNext)) } } } @@ -182,17 +176,19 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { aliveLatch.claim() } - // `shutdown` wakes every poll on the transport but keeps the descriptor number, so no other - // socket can take it mid-poll. The listening socket closes only after the acceptor's cancel - // handler ran, which GCD runs once no accept handler is running. The relays are awaited after - // that, because the accept handler is the only thing that starts one: the group can only empty. + // `shutdown` wakes every open or free waiting on the transport but keeps the descriptor number, + // so no other socket can take it mid-poll. The listening socket closes only after the acceptor's + // cancel handler ran, which GCD runs once no accept handler is running. The clients are awaited + // after that, because the accept handler is the only thing that starts one: the group can only + // empty. The pump is awaited last, so no source of it can fire on the descriptors closed below. private func performTeardown() { acceptor?.cancel() keepAliveTask?.cancel() + pump.stop() shutdown(socketFD, SHUT_RDWR) - let sessionQueue = self.sessionQueue + let pump = self.pump let relayQueue = self.relayQueue let clientRelays = self.clientRelays let session = self.session @@ -208,10 +204,11 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { await withCheckedContinuation { (continuation: CheckedContinuation) in clientRelays.notify(queue: relayQueue) { continuation.resume() } } + await pump.waitUntilStopped() // Free before close: a closed number can go to another connection's socket, and this // session's DISCONNECT would land in it. - sessionQueue.sync { + pump.perform { libssh2_session_set_blocking(session, 1) tablepro_libssh2_session_disconnect(session, "Closing tunnel") libssh2_session_free(session) @@ -244,6 +241,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { acceptor?.cancel() keepAliveTask?.cancel() + pump.stop() shutdown(socketFD, SHUT_RDWR) @@ -261,7 +259,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { onDeath?(connectionId) } - // Off `sessionQueue` between open attempts, so a slow open cannot stall the relays and the + // Off the session queue between open attempts, so a slow open cannot stall the relays and the // keep-alive that share the session. private func openAndRelay( clientFD: Int32, @@ -269,12 +267,15 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { destination: SSHForwardDestination ) { let connectionDeadline = budget.deadline - let pump = SSHForwardChannelOpenPump( + let turn = SSHSendTurn(pump: pump) + defer { turn.end() } + let openPump = SSHForwardChannelOpenPump( opener: LibSSH2ForwardChannelOpener( session: session, destination: destination, originPort: localPort, - sessionQueue: sessionQueue + pump: pump, + turn: turn ), isActive: { [weak self] in guard self?.isRunning == true else { return false } @@ -295,7 +296,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { } ) - let outcome = pump.run() + let outcome = openPump.run() let relayTimedOut: Bool switch outcome { case .timedOut: @@ -312,6 +313,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { if case .opened(let channel) = outcome { Darwin.close(clientFD) freeChannel(channel) + clientRelays.leave() return } } @@ -327,8 +329,10 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { deadlineSeconds: deadlineSeconds ) handleChannelOpenOutcome(outcome, clientFD: clientFD) { channel in - runRelay(clientFD: clientFD, channel: channel, destination: destination) + relayChannel(clientFD: clientFD, channel: channel, destination: destination) } + if case .opened = outcome { return } + clientRelays.leave() } func consumeLastForwardFailure() -> (any Error)? { @@ -363,7 +367,8 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { } } - // On `relayQueue`, off the accept handler, so a slow open cannot delay the next accept. + // On `relayQueue`, off the accept handler, so a slow open cannot delay the next accept. The client + // stays in `clientRelays` until its relay ended and its channel was freed. private func spawnClient( clientFD: Int32, budget: ConnectionRelayDeadlineProvider.Budget, @@ -372,46 +377,42 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { let clientRelays = self.clientRelays clientRelays.enter() relayQueue.async { [weak self] in - defer { clientRelays.leave() } guard let self else { Darwin.close(clientFD) + clientRelays.leave() return } self.openAndRelay(clientFD: clientFD, budget: budget, destination: destination) } } - /// Blocking relay loop. Runs on `relayQueue`; libssh2 calls go through `sessionQueue`. - /// Every termination is logged, not just a hangup: a channel that opens only after the - /// database client already gave up ends as `.localClosed`, and staying silent about that - /// leaves no trace of a tunnel that worked but arrived too late. - private func runRelay(clientFD: Int32, channel: OpaquePointer, destination: SSHForwardDestination) { - let relay = SSHChannelRelay( + // Every termination is logged, not just a hangup: a channel that opens only after the database + // client already gave up ends as `.localClosed`, and staying silent about that leaves no trace + // of a tunnel that worked but arrived too late. + private func relayChannel(clientFD: Int32, channel: OpaquePointer, destination: SSHForwardDestination) { + let startedAt = Date() + let target = destination.logDescription + let clientRelays = self.clientRelays + let handle = ChannelHandle(pointer: channel) + pump.relay( localFD: clientFD, - transportFD: socketFD, - channelIO: LibSSH2ChannelIO(channel: channel, session: session, sessionQueue: sessionQueue), + channel: LibSSH2ChannelIO(channel: channel, session: session), bufferSize: Self.relayBufferSize, - isActive: { [weak self] in self?.isRunning ?? false }, byteCounter: byteCounter - ) - - let startedAt = Date() - let termination = relay.run() - let elapsed = Date().timeIntervalSince(startedAt) - - let target = destination.logDescription - Self.logger.debug( - "Relay to \(target) ended as \(String(describing: termination)) after \(elapsed, format: .fixed(precision: 1))s" - ) - - Darwin.close(clientFD) - guard self.isRunning else { return } + ) { [weak self] termination in + defer { clientRelays.leave() } + let elapsed = Date().timeIntervalSince(startedAt) + Self.logger.debug( + "Relay to \(target) ended as \(String(describing: termination)) after \(elapsed, format: .fixed(precision: 1))s" + ) + guard let self, self.isRunning else { return } - freeChannel(channel) + self.freeChannel(handle.pointer) - if termination == .transportHangup { - Self.logger.info("SSH transport hung up, marking tunnel dead for \(self.connectionId)") - markDead() + if termination == .transportHangup { + Self.logger.info("SSH transport hung up, marking tunnel dead for \(self.connectionId)") + self.markDead() + } } } @@ -419,7 +420,7 @@ internal final class LibSSH2Tunnel: @unchecked Sendable { let outcome = LibSSH2ChannelFree.run( channel: channel, session: session, - sessionQueue: sessionQueue, + pump: pump, transportFD: socketFD, isActive: { [weak self] in self?.isRunning ?? false } ) diff --git a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift index a1802d8c42..d1f5c51706 100644 --- a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift +++ b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift @@ -347,7 +347,7 @@ internal enum LibSSH2TunnelFactory { ) } } catch { - // Clean up nextSession and fds[1]; relay task owns fds[0] + // Clean up nextSession and fds[1]; the hop's pump owns fds[0] tablepro_libssh2_session_disconnect(nextSession, "Error") libssh2_session_free(nextSession) Darwin.close(fds[1]) @@ -610,8 +610,8 @@ internal enum LibSSH2TunnelFactory { libssh2_session_free(chain.session) Darwin.close(chain.socketFD) - // Wake every relay before waiting. A relay may be blocked in `poll` or a serialized - // libssh2 call, and neither a Swift Task cancellation nor freeing the handle is safe. + // Stop every hop before waiting. A hop's pump may be inside a libssh2 call, and freeing a + // handle under it is not safe. for hop in chain.jumpHops.reversed() { hop.relay.stop() shutdown(hop.socket, SHUT_RDWR) @@ -619,8 +619,8 @@ internal enum LibSSH2TunnelFactory { for hop in chain.jumpHops.reversed() { hop.relay.wait() } - // Only after the fence has drained can cleanup free the handles. The relay owns and closes - // its socketpair endpoint; this side owns the hop transport socket. + // Only after the fence has drained can cleanup free the handles. The hop's pump closes its + // socketpair endpoint; this side owns the hop transport socket. for hop in chain.jumpHops.reversed() { libssh2_channel_free(hop.channel) tablepro_libssh2_session_disconnect(hop.session, reason) @@ -1090,12 +1090,15 @@ internal enum LibSSH2TunnelFactory { probeQueue.sync { libssh2_session_set_blocking(session, 0) } defer { probeQueue.sync { libssh2_session_set_blocking(session, 1) } } + let probePump = SSHSessionPump(transportFD: socketFD, queue: probeQueue) + defer { probePump.stop() } let pump = SSHForwardChannelOpenPump( opener: LibSSH2ForwardChannelOpener( session: session, destination: destination, originPort: 0, - sessionQueue: probeQueue + pump: probePump, + turn: SSHSendTurn(pump: probePump) ), isActive: { !deadline.isExpired && !Task.isCancelled }, deadline: .distantFuture, diff --git a/apple/TablePro/Core/SSH/SSHKeepAliveResult.swift b/apple/TablePro/Core/SSH/SSHKeepAliveResult.swift index d22ea11684..d272658354 100644 --- a/apple/TablePro/Core/SSH/SSHKeepAliveResult.swift +++ b/apple/TablePro/Core/SSH/SSHKeepAliveResult.swift @@ -7,10 +7,13 @@ // the iOS build is the check. // +import Foundation + import CLibSSH2 +import TableProSSHTransport -/// The libssh2 return codes the keep-alive has to tell apart, named here so callers and tests -/// can work with them without pulling the C module in. +// The libssh2 return codes the keep-alive has to tell apart, named here so callers and tests +// can work with them without pulling the C module in. nonisolated internal enum SSHKeepAliveCode { static let sent: Int32 = 0 static let wouldBlock: Int32 = LIBSSH2_ERROR_EAGAIN @@ -18,10 +21,66 @@ nonisolated internal enum SSHKeepAliveCode { static let socketDisconnect: Int32 = LIBSSH2_ERROR_SOCKET_DISCONNECT } -/// Whether a keep-alive return code means the tunnel is dead. The session is non-blocking for -/// the whole time it forwards, so libssh2 answers `LIBSSH2_ERROR_EAGAIN` when the send would -/// block on a transport that is busy carrying query results. That is a healthy tunnel under -/// load, not a dead one, and tearing it down drops every connection running through it. +// Whether a keep-alive return code means the tunnel is dead. The session is non-blocking for +// the whole time it forwards, so libssh2 answers `LIBSSH2_ERROR_EAGAIN` when the send would +// block on a transport that is busy carrying query results. That is a healthy tunnel under +// load, not a dead one, and tearing it down drops every connection running through it. nonisolated internal func sshKeepAliveDidFail(_ resultCode: Int32) -> Bool { resultCode != SSHKeepAliveCode.sent && resultCode != SSHKeepAliveCode.wouldBlock } + +nonisolated internal struct SSHKeepAliveOutcome: Sendable { + let failed: Bool + let secondsToNext: Int +} + +nonisolated internal enum StackPacketReadiness { + case ready + case socketFull + case sendPending + case gone +} + +// libssh2 keeps a packet it could not send whole until a call with the same buffer address sends the rest, and +// turns every other send away until then (transport_send_existing). The keep-alive and the channel EOF build +// their packets on their own stack, and a retry from another frame cannot finish them, so one that blocks stops +// every channel on the session (apple/scripts/probes/check-libssh2-keepalive-pending.sh). Such a packet goes out +// only when nothing else is waiting to send and the socket has room for it whole. +nonisolated internal func stackPacketReadiness(session: OpaquePointer, transportFD: Int32) -> StackPacketReadiness { + switch transportRoom(transportFD) { + case .gone: + return .gone + case .full: + return .socketFull + case .room: + let pending = libssh2_session_block_directions(session) & LIBSSH2_SESSION_BLOCK_OUTBOUND != 0 + return pending ? .sendPending : .ready + } +} + +nonisolated internal enum LibSSH2KeepAlive { + // Without a reply asked for: libssh2 never reads one, and each REQUEST_FAILURE sshd sends back stays at the + // head of the session's packet list for good (channel.c, ssh2_channel_read skips it without unlinking it). + static let wantsReply: Int32 = 0 + static let intervalSeconds: UInt32 = 30 + + private static let retrySeconds = 1 + + static func configure(session: OpaquePointer) { + libssh2_keepalive_config(session, wantsReply, intervalSeconds) + } + + // libssh2 sends at most once per interval and answers how long until the next one is due. + static func send(session: OpaquePointer, transportFD: Int32) -> SSHKeepAliveOutcome { + switch stackPacketReadiness(session: session, transportFD: transportFD) { + case .gone: + return SSHKeepAliveOutcome(failed: true, secondsToNext: retrySeconds) + case .socketFull, .sendPending: + return SSHKeepAliveOutcome(failed: false, secondsToNext: retrySeconds) + case .ready: + var secondsToNext: Int32 = 0 + let resultCode = libssh2_keepalive_send(session, &secondsToNext) + return SSHKeepAliveOutcome(failed: sshKeepAliveDidFail(resultCode), secondsToNext: max(1, Int(secondsToNext))) + } + } +} diff --git a/apple/TablePro/Core/SSH/SSHTunnelManager.swift b/apple/TablePro/Core/SSH/SSHTunnelManager.swift index 74dc2dd190..d0d48c91f3 100644 --- a/apple/TablePro/Core/SSH/SSHTunnelManager.swift +++ b/apple/TablePro/Core/SSH/SSHTunnelManager.swift @@ -23,10 +23,6 @@ actor SSHTunnelManager: TunnelManaging { /// Static registry for synchronous termination during app shutdown private static let tunnelRegistry = OSAllocatedUnfairLock(initialState: [UUID: LibSSH2Tunnel]()) - /// Prevents App Nap from throttling SSH keepalive timers while tunnels are active. - /// Held as long as at least one tunnel exists; released when the last tunnel closes. - private var appNapActivity: NSObjectProtocol? - init(localPortCandidates: @escaping @Sendable () -> [Int] = { Array(60_000...65_000).shuffled() }) { self.localPortCandidates = localPortCandidates } @@ -122,7 +118,6 @@ actor SSHTunnelManager: TunnelManaging { tunnel.startForwarding(destination: destination) tunnel.startKeepAlive() - updateAppNapState() Self.logger.info("Tunnel created for \(connectionId) on local port \(listener.port)") return listener.port } @@ -136,7 +131,6 @@ actor SSHTunnelManager: TunnelManaging { private func discardTunnel(connectionId: UUID) async throws { guard let tunnel = tunnels.removeValue(forKey: connectionId) else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } - updateAppNapState() tunnel.close() } @@ -145,7 +139,6 @@ actor SSHTunnelManager: TunnelManaging { let currentTunnels = tunnels tunnels.removeAll() Self.tunnelRegistry.withLock { $0.removeAll(); return } - updateAppNapState() for (_, tunnel) in currentTunnels { tunnel.close() @@ -215,25 +208,7 @@ actor SSHTunnelManager: TunnelManaging { private func handleTunnelDeath(connectionId: UUID) async { guard tunnels.removeValue(forKey: connectionId) != nil else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } - updateAppNapState() Self.logger.warning("Tunnel died for connection \(connectionId)") await DatabaseManager.shared.handleSSHTunnelDied(connectionId: connectionId) } - - // MARK: - App Nap Prevention - - /// Acquires or releases an App Nap activity token based on whether tunnels exist. - private func updateAppNapState() { - if !tunnels.isEmpty && appNapActivity == nil { - appNapActivity = ProcessInfo.processInfo.beginActivity( - options: .userInitiatedAllowingIdleSystemSleep, - reason: "SSH tunnel keepalive requires timely execution" - ) - Self.logger.debug("App Nap prevention acquired") - } else if tunnels.isEmpty, let activity = appNapActivity { - ProcessInfo.processInfo.endActivity(activity) - appNapActivity = nil - Self.logger.debug("App Nap prevention released") - } - } } diff --git a/apple/TableProMobile/TableProMobile/SSH/SSHTunnel.swift b/apple/TableProMobile/TableProMobile/SSH/SSHTunnel.swift index 2177ce2d69..3d609ecd99 100644 --- a/apple/TableProMobile/TableProMobile/SSH/SSHTunnel.swift +++ b/apple/TableProMobile/TableProMobile/SSH/SSHTunnel.swift @@ -6,8 +6,8 @@ import TableProSSHTransport // One authenticated libssh2 session forwarding a local port, built by `SSHTunnelFactory`. // // A class rather than an actor, the shape `LibSSH2Tunnel` already has on macOS. Every libssh2 call -// is serialized on `sessionQueue`, the acceptor has a serial queue of its own and the relays share a -// concurrent one, so nothing here blocks a thread of Swift's cooperative pool. An actor gives mutual +// runs on the session's pump, the acceptor has a serial queue of its own and channel opens +// share a concurrent one, so nothing here blocks a thread of Swift's cooperative pool. An actor gives mutual // exclusion but never thread transfer, so a tunnel parked in `poll` held a cooperative thread for its // whole life: measured, 48 tunnels ran 12 at a time in 4,005ms where queues ran all 48 in 1,003ms. // `Task.detached` measured the same 12, because it is the same pool. @@ -24,10 +24,10 @@ nonisolated final class SSHTunnel: @unchecked Sendable { private let listenFD: Int32 private let jumpHops: [SSHJumpHop] - /// Serial: every libssh2 call on this session. libssh2 is not thread-safe per session. - private let sessionQueue: DispatchQueue + // Every libssh2 call on the session, and the relays of its channels. libssh2 is not thread-safe per session. + private let pump: SSHSessionPump - /// Concurrent: relay loops, which poll, send and recv but make no libssh2 call directly. + // Concurrent: channel opens, which wait on the transport between attempts. private let relayQueue: DispatchQueue // Serial: the acceptor's handler, which accepts and makes no libssh2 call. @@ -36,18 +36,18 @@ nonisolated final class SSHTunnel: @unchecked Sendable { private let aliveLatch = TeardownLatch() private let deathNotice = SSHTunnelDeathNotice() - /// The relays still running, so teardown frees the session only once none of them can touch - /// it. A group rather than a collection of tasks: a relay leaves it by finishing, which is the - /// one thing a `[Task]` pruned on `isCancelled` never noticed, so a tunnel that had served - /// clients carried every one of them until it closed. It is also all a task was ever worth - /// here, since the relay runs on `relayQueue` outside the task's cancellation scope and stops - /// on `aliveLatch` rather than on `Task.isCancelled`. + // Each accepted client from accept until its channel is freed or given up, so teardown frees the + // session only once none of them can touch it. private let clientRelays = DispatchGroup() private var acceptor: SSHListenerAcceptor? private var keepAliveTask: Task? private static let relayBufferSize = 32_768 + nonisolated private struct ChannelHandle: @unchecked Sendable { + let pointer: OpaquePointer + } + /// Bounds a forwarding channel open for a client that has already been accepted. libssh2 /// retries EAGAIN forever on its own, so without this a stuck open outlives the database /// driver's connect timeout and the client waits on a socket nothing will ever write to. @@ -55,8 +55,6 @@ nonisolated final class SSHTunnel: @unchecked Sendable { /// before the driver reports its own timeout, which names no cause. static let channelOpenDeadlineSeconds: TimeInterval = 6 - private static let keepAliveIntervalSeconds = 10 - init( session: OpaquePointer, socketFD: Int32, @@ -69,7 +67,7 @@ nonisolated final class SSHTunnel: @unchecked Sendable { self.socketFD = socketFD self.listenFD = listenFD self.localPort = localPort - self.sessionQueue = sessionQueue + self.pump = SSHSessionPump(transportFD: socketFD, queue: sessionQueue) self.jumpHops = jumpHops let label = UUID().uuidString self.relayQueue = DispatchQueue(label: "com.TablePro.ssh.relay.\(label)", qos: .utility, attributes: .concurrent) @@ -83,7 +81,7 @@ nonisolated final class SSHTunnel: @unchecked Sendable { // MARK: - Forwarding func startForwarding(destination: SSHForwardDestination) { - sessionQueue.sync { libssh2_session_set_blocking(session, 0) } + pump.perform { libssh2_session_set_blocking(session, 0) } // A handler already running when the latch is claimed may still spawn one relay; teardown // waits for the cancel handler before the relay group, so it waits for that relay too. @@ -108,27 +106,23 @@ nonisolated final class SSHTunnel: @unchecked Sendable { // MARK: - Keep-Alive func startKeepAlive() { - sessionQueue.sync { libssh2_keepalive_config(session, 1, 30) } + pump.perform { LibSSH2KeepAlive.configure(session: session) } keepAliveTask = Task.detached { [weak self] in guard let self else { return } while !Task.isCancelled && self.isRunning { - let failed = await withCheckedContinuation { (continuation: CheckedContinuation) in - self.sessionQueue.async { - var secondsToNext: Int32 = 0 - let rc = libssh2_keepalive_send(self.session, &secondsToNext) - continuation.resume(returning: sshKeepAliveDidFail(rc)) - } + let outcome = await self.pump.performAsync { + LibSSH2KeepAlive.send(session: self.session, transportFD: self.socketFD) } - if failed { + if outcome.failed { Self.logger.warning("Keep-alive failed, marking tunnel dead") self.markDead() break } - try? await Task.sleep(for: .seconds(Self.keepAliveIntervalSeconds)) + try? await Task.sleep(for: .seconds(outcome.secondsToNext)) } } } @@ -151,17 +145,18 @@ nonisolated final class SSHTunnel: @unchecked Sendable { deathNotice.died() } - // shutdown wakes every poll on the transport but keeps the number, so no other socket can take it mid-poll. + // shutdown wakes every open or free waiting on the transport but keeps the number, so no other socket can take it mid-poll. // The listening socket closes only after the acceptor's cancel handler ran, once no accept handler is running. - // The relays are awaited after that, since the accept handler is the only thing that starts one. - // The hops go last, in the Mac's order. + // The clients are awaited after that, since the accept handler is the only thing that starts one, and the pump + // last, so no source of it can fire on the descriptors closed below. The hops go last, in the Mac's order. private func performTeardown() { acceptor?.cancel() keepAliveTask?.cancel() + pump.stop() shutdown(socketFD, SHUT_RDWR) - let sessionQueue = self.sessionQueue + let pump = self.pump let relayQueue = self.relayQueue let clientRelays = self.clientRelays let session = self.session @@ -177,9 +172,10 @@ nonisolated final class SSHTunnel: @unchecked Sendable { await withCheckedContinuation { (continuation: CheckedContinuation) in clientRelays.notify(queue: relayQueue) { continuation.resume() } } + await pump.waitUntilStopped() // Free before close: a closed number can go to the next tunnel's socket, and this session's DISCONNECT would land in it. - sessionQueue.sync { + pump.perform { libssh2_session_set_blocking(session, 1) tablepro_libssh2_session_disconnect(session, "Closing tunnel") libssh2_session_free(session) @@ -194,14 +190,15 @@ nonisolated final class SSHTunnel: @unchecked Sendable { // MARK: - Private - // On `relayQueue`, off the accept handler, so a slow open cannot delay the next accept. + // On `relayQueue`, off the accept handler, so a slow open cannot delay the next accept. The client + // stays in `clientRelays` until its relay ended and its channel was freed. private func spawnClient(clientFD: Int32, acceptedAt: Date, destination: SSHForwardDestination) { let clientRelays = self.clientRelays clientRelays.enter() relayQueue.async { [weak self] in - defer { clientRelays.leave() } guard let self else { Darwin.close(clientFD) + clientRelays.leave() return } self.openAndRelay(clientFD: clientFD, acceptedAt: acceptedAt, destination: destination) @@ -209,12 +206,15 @@ nonisolated final class SSHTunnel: @unchecked Sendable { } private func openAndRelay(clientFD: Int32, acceptedAt: Date, destination: SSHForwardDestination) { - let pump = SSHForwardChannelOpenPump( + let turn = SSHSendTurn(pump: pump) + defer { turn.end() } + let openPump = SSHForwardChannelOpenPump( opener: LibSSH2ForwardChannelOpener( session: session, destination: destination, originPort: localPort, - sessionQueue: sessionQueue + pump: pump, + turn: turn ), isActive: { [weak self] in self?.isRunning ?? false }, deadline: acceptedAt.addingTimeInterval(Self.channelOpenDeadlineSeconds), @@ -225,11 +225,13 @@ nonisolated final class SSHTunnel: @unchecked Sendable { } ) - let outcome = pump.run() + let outcome = openPump.run() logChannelOpenOutcome(outcome, destination: destination) handleChannelOpenOutcome(outcome, clientFD: clientFD) { channel in - runRelay(clientFD: clientFD, channel: channel, destination: destination) + relayChannel(clientFD: clientFD, channel: channel, destination: destination) } + if case .opened = outcome { return } + clientRelays.leave() } private func logChannelOpenOutcome(_ outcome: ChannelOpenOutcome, destination: SSHForwardDestination) { @@ -250,32 +252,29 @@ nonisolated final class SSHTunnel: @unchecked Sendable { } } - private func runRelay(clientFD: Int32, channel: OpaquePointer, destination: SSHForwardDestination) { - let relay = SSHChannelRelay( - localFD: clientFD, - transportFD: socketFD, - channelIO: LibSSH2ChannelIO(channel: channel, session: session, sessionQueue: sessionQueue), - bufferSize: Self.relayBufferSize, - isActive: { [weak self] in self?.isRunning ?? false } - ) - + private func relayChannel(clientFD: Int32, channel: OpaquePointer, destination: SSHForwardDestination) { let startedAt = Date() - let termination = relay.run() - let elapsed = Date().timeIntervalSince(startedAt) - let target = destination.logDescription - Self.logger.debug( - "Relay to \(target) ended as \(String(describing: termination)) after \(elapsed, format: .fixed(precision: 1))s" - ) - - Darwin.close(clientFD) - guard isRunning else { return } + let clientRelays = self.clientRelays + let handle = ChannelHandle(pointer: channel) + pump.relay( + localFD: clientFD, + channel: LibSSH2ChannelIO(channel: channel, session: session), + bufferSize: Self.relayBufferSize + ) { [weak self] termination in + defer { clientRelays.leave() } + let elapsed = Date().timeIntervalSince(startedAt) + Self.logger.debug( + "Relay to \(target) ended as \(String(describing: termination)) after \(elapsed, format: .fixed(precision: 1))s" + ) + guard let self, self.isRunning else { return } - freeChannel(channel) + self.freeChannel(handle.pointer) - if termination == .transportHangup { - Self.logger.info("SSH transport hung up, marking tunnel dead on port \(self.localPort)") - markDead() + if termination == .transportHangup { + Self.logger.info("SSH transport hung up, marking tunnel dead on port \(self.localPort)") + self.markDead() + } } } @@ -283,7 +282,7 @@ nonisolated final class SSHTunnel: @unchecked Sendable { let outcome = LibSSH2ChannelFree.run( channel: channel, session: session, - sessionQueue: sessionQueue, + pump: pump, transportFD: socketFD, isActive: { [weak self] in self?.isRunning ?? false } ) diff --git a/apple/TableProTests/Core/SSH/LibSSH2KeepAliveTests.swift b/apple/TableProTests/Core/SSH/LibSSH2KeepAliveTests.swift new file mode 100644 index 0000000000..d874657b24 --- /dev/null +++ b/apple/TableProTests/Core/SSH/LibSSH2KeepAliveTests.swift @@ -0,0 +1,86 @@ +// +// LibSSH2KeepAliveTests.swift +// TableProTests +// +// The keep-alive and the channel EOF build their packets on their own stack, and libssh2 refuses every other send +// until the same frame finishes one that blocked (apple/scripts/probes/check-libssh2-keepalive-pending.sh). +// + +import CLibSSH2 +import Foundation +@testable import TablePro +import Testing + +struct LibSSH2KeepAliveTests { + @Test("A stack-built packet may go out on a socket with room and nothing else waiting to send") + func readyWithRoom() throws { + let fixture = try KeepAliveFixture() + defer { fixture.close() } + + #expect(stackPacketReadiness(session: fixture.session, transportFD: fixture.local) == .ready) + } + + @Test("A stack-built packet waits while the socket has no room for it") + func waitsOnAFullSocket() throws { + let fixture = try KeepAliveFixture() + defer { fixture.close() } + + fixture.fillLocal() + + #expect(stackPacketReadiness(session: fixture.session, transportFD: fixture.local) == .socketFull) + let outcome = LibSSH2KeepAlive.send(session: fixture.session, transportFD: fixture.local) + #expect(!outcome.failed) + #expect(outcome.secondsToNext == 1) + } + + @Test("A closed transport fails the keep-alive at once") + func failsOnAClosedTransport() throws { + let fixture = try KeepAliveFixture() + defer { fixture.close() } + + fixture.closePeer() + + #expect(stackPacketReadiness(session: fixture.session, transportFD: fixture.local) == .gone) + #expect(LibSSH2KeepAlive.send(session: fixture.session, transportFD: fixture.local).failed) + } +} + +private final class KeepAliveFixture { + struct SetupFailure: Error {} + + let session: OpaquePointer + let local: Int32 + private var peer: Int32 + + init() throws { + _ = LibSSH2TunnelFactory.initialized + var fds: [Int32] = [-1, -1] + guard socketpair(AF_UNIX, SOCK_STREAM, 0, &fds) == 0 else { throw SetupFailure() } + guard let session = tablepro_libssh2_session_init() else { + Darwin.close(fds[0]) + Darwin.close(fds[1]) + throw SetupFailure() + } + self.session = session + local = fds[0] + peer = fds[1] + } + + func fillLocal() { + let flags = fcntl(local, F_GETFL, 0) + _ = fcntl(local, F_SETFL, flags | O_NONBLOCK) + var chunk = [UInt8](repeating: 0x46, count: 4_096) + while Darwin.send(local, &chunk, chunk.count, 0) > 0 {} + } + + func closePeer() { + Darwin.close(peer) + peer = -1 + } + + func close() { + libssh2_session_free(session) + Darwin.close(local) + if peer >= 0 { Darwin.close(peer) } + } +} diff --git a/apple/changelog.d/4248.fixed.1.md b/apple/changelog.d/4248.fixed.1.md new file mode 100644 index 0000000000..ce7f934492 --- /dev/null +++ b/apple/changelog.d/4248.fixed.1.md @@ -0,0 +1 @@ +A connection through an SSH tunnel could wait half a second for a reply while others were open. diff --git a/apple/changelog.d/4248.fixed.2.md b/apple/changelog.d/4248.fixed.2.md new file mode 100644 index 0000000000..96ced0ec41 --- /dev/null +++ b/apple/changelog.d/4248.fixed.2.md @@ -0,0 +1 @@ +An SSH tunnel could stall every connection through it after a keep-alive met a full network buffer. diff --git a/apple/changelog.d/4248.fixed.md b/apple/changelog.d/4248.fixed.md new file mode 100644 index 0000000000..37d521498d --- /dev/null +++ b/apple/changelog.d/4248.fixed.md @@ -0,0 +1 @@ +An open SSH tunnel woke TablePro twice a second for each connection and kept macOS from napping it. diff --git a/apple/scripts/probes/check-libssh2-keepalive-pending.sh b/apple/scripts/probes/check-libssh2-keepalive-pending.sh new file mode 100755 index 0000000000..3b0466c0c5 --- /dev/null +++ b/apple/scripts/probes/check-libssh2-keepalive-pending.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash +# +# Shows why LibSSH2KeepAlive sends only into a socket with room, against the vendored libssh2 and a +# real OpenSSH server. +# +# libssh2_keepalive_send builds its packet on its own stack (keepalive.c) and counts a send that would +# block as sent. libssh2 keeps a packet it could not send whole and refuses every other send until a call +# with the same buffer address finishes it (transport_send_existing in transport.c). So a keep-alive that +# blocks stops every channel write on the session, the next keep-alive comes only an interval later, and +# from another stack frame it finishes nothing. The probe makes one send would-block through libssh2's +# send callback, then counts the channel writes refused while the socket is writable. Rerun after a +# libssh2 bump: exit 1 means the keep-alive no longer blocks the session and the guard can go. +# +# Usage: +# scripts/probes/check-libssh2-keepalive-pending.sh +# +# Needs Docker. Starts a throwaway OpenSSH container and removes it on exit. + +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +# shellcheck source=../lib/openssh-fixture.sh +source "$ROOT/scripts/lib/openssh-fixture.sh" + +CONTAINER="tp-keepalive-pending" +PORT="${TABLEPRO_KEEPALIVE_PENDING_PORT:-22024}" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"; docker rm -f -v "$CONTAINER" > /dev/null 2>&1 || true' EXIT + +docker rm -f -v "$CONTAINER" > /dev/null 2>&1 || true +docker run -d --name "$CONTAINER" -p "127.0.0.1:$PORT:2222" \ + -e USER_NAME=tp -e USER_PASSWORD=tppass -e PASSWORD_ACCESS=true -e SUDO_ACCESS=false \ + "$OPENSSH_FIXTURE_IMAGE_10" > /dev/null +openssh_wait_for_banner 127.0.0.1 "$PORT" 60 || { echo "no SSH banner within 60s" >&2; exit 2; } + +cat > "$WORK/probe.c" << 'PROBE' +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libssh2.h" + +static int refuse_next_send = 0; + +static LIBSSH2_SEND_FUNC(gated_send) { + (void)abstract; + if (refuse_next_send) { + refuse_next_send = 0; + return -EAGAIN; + } + ssize_t rc = send(socket, buffer, length, flags); + return rc < 0 ? -errno : rc; +} + +static double now(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec + t.tv_nsec / 1e9; +} + +__attribute__((noinline)) static int keepalive_here(LIBSSH2_SESSION *s) { + int next = 0; + return libssh2_keepalive_send(s, &next); +} + +__attribute__((noinline)) static int keepalive_deeper(LIBSSH2_SESSION *s) { + volatile char pad[512]; + pad[0] = 1; + return keepalive_here(s) + pad[0] - 1; +} + +static int refused_writes(LIBSSH2_CHANNEL *c, int sock, double seconds, int *accepted) { + const char *line = "ping\n"; + int refused = 0; + double until = now() + seconds; + *accepted = 0; + while (now() < until) { + ssize_t rc = libssh2_channel_write(c, line, strlen(line)); + if (rc > 0) { *accepted = 1; return refused; } + if (rc != LIBSSH2_ERROR_EAGAIN) { printf("write failed %zd\n", rc); exit(2); } + refused++; + struct pollfd p = { sock, POLLOUT, 0 }; + if (poll(&p, 1, 100) <= 0) { puts("socket not writable"); exit(2); } + } + return refused; +} + +int main(int argc, char **argv) { + int port = atoi(argv[1]); + libssh2_init(0); + int sock = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in a; + memset(&a, 0, sizeof(a)); + a.sin_family = AF_INET; + a.sin_port = htons((unsigned short)port); + a.sin_addr.s_addr = inet_addr("127.0.0.1"); + if (connect(sock, (struct sockaddr *)&a, sizeof(a))) { puts("tcp connect failed"); exit(2); } + LIBSSH2_SESSION *s = libssh2_session_init(); + if (libssh2_session_handshake(s, sock)) { puts("handshake failed"); exit(2); } + if (libssh2_userauth_password(s, "tp", "tppass")) { puts("auth failed"); exit(2); } + LIBSSH2_CHANNEL *c = libssh2_channel_open_session(s); + if (!c || libssh2_channel_exec(c, "cat")) { puts("could not start cat"); exit(2); } + libssh2_session_callback_set2(s, LIBSSH2_CALLBACK_SEND, (libssh2_cb_generic *)gated_send); + libssh2_session_set_blocking(s, 0); + libssh2_keepalive_config(s, 1, 2); + sleep(3); + + refuse_next_send = 1; + int rc = keepalive_here(s); + int accepted; + int blocked = refused_writes(c, sock, 3.0, &accepted); + printf("keep-alive whose send would block: rc %d; channel writes refused over 3 s with the socket writable: %d\n", rc, blocked); + int pending = blocked > 0 && !accepted; + + sleep(2); + rc = keepalive_deeper(s); + int refusedAfterOther = refused_writes(c, sock, 1.0, &accepted); + printf("after the next keep-alive from another stack frame (rc %d): refused %d, accepted %d\n", rc, refusedAfterOther, accepted); + int stillPending = refusedAfterOther > 0 && !accepted; + + sleep(2); + rc = keepalive_here(s); + int refusedAfterSame = refused_writes(c, sock, 1.0, &accepted); + printf("after a keep-alive from the first frame (rc %d): refused %d, accepted %d\n", rc, refusedAfterSame, accepted); + libssh2_exit(); + return pending && stillPending && accepted ? 0 : 1; +} +PROBE + +clang -w -arch arm64 -o "$WORK/probe" "$WORK/probe.c" \ + -I "$ROOT/TablePro/Core/SSH/CLibSSH2/include" \ + "$ROOT/Libs/libssh2_arm64.a" "$ROOT/Libs/libssl_arm64.a" "$ROOT/Libs/libcrypto_arm64.a" \ + -lz -framework Security -framework CoreFoundation + +"$WORK/probe" "$PORT" diff --git a/apple/scripts/probes/check-ssh-relay.sh b/apple/scripts/probes/check-ssh-relay.sh new file mode 100755 index 0000000000..079d35cd4d --- /dev/null +++ b/apple/scripts/probes/check-ssh-relay.sh @@ -0,0 +1,167 @@ +#!/usr/bin/env bash +# +# Measures the macOS app's SSH tunnel relay against a real OpenSSH server with Redis behind it: what +# an idle tunnel costs the process (wakeups, CPU and threads over a quiet window, with 0, 1 and 3 +# clients relayed), bulk throughput each way, and round-trip latency with several clients at once, +# alone and beside a bulk download. It prints numbers, not a verdict, so a relay change can be +# measured before and after. +# +# The harness is the one ssh-tunnel-matrix/build-mac-harness.sh builds from the app's own tunnel +# code, run the way check-ssh-tunnel-matrix.sh runs it: an empty home (CFFIXED_USER_HOME), no SSH +# agent, and the test servers' password. The server is v10 from scripts/ssh-test-servers.sh, which +# needs Docker. It shares the matrix's containers, so run one of the two at a time. That script +# starts a single server only once its keys exist, so a first run starts every server to make them. +# +# Usage: +# scripts/probes/check-ssh-relay.sh # build the harness, measure, remove the servers +# scripts/probes/check-ssh-relay.sh --keep # leave the servers running afterwards +# scripts/probes/check-ssh-relay.sh --harness # measure a harness built from other sources +# scripts/probes/check-ssh-relay.sh --wait 60 # seconds in each idle window (default 30) +# scripts/probes/check-ssh-relay.sh --size 128 # megabytes the bulk run moves each way (default 64) +# +# TABLEPRO_SSH_RELAY_BUILD is the build directory, which later runs reuse. TABLEPRO_SSH_TEST_PORT_BASE +# and TABLEPRO_SSH_TEST_DIR reach ssh-test-servers.sh unchanged. +# +# Exits 1 when a run prints an error or no result, 2 when the harness or the servers cannot start. + +set -uo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +HERE="$ROOT/scripts/probes/ssh-tunnel-matrix" +SERVERS="$ROOT/scripts/ssh-test-servers.sh" +BUILD="${TABLEPRO_SSH_RELAY_BUILD:-${TMPDIR:-/tmp}/tablepro-ssh-relay-$(printf '%s' "$ROOT" | shasum | cut -c1-8)}" +BUILD="${BUILD%/}" +KEEP=0 +HARNESS="" +WAIT=30 +SIZE=64 +STARTED=0 +FAILED=0 +RESULT="" + +usage() { + awk 'NR > 2 { if (!/^#/) exit; sub(/^# ?/, ""); print }' "${BASH_SOURCE[0]}" + exit 2 +} + +positive() { + [[ "$1" =~ ^[1-9][0-9]*$ ]] +} + +while [ $# -gt 0 ]; do + case "$1" in + --keep) KEEP=1 ;; + --harness) + [ -n "${2:-}" ] || usage + HARNESS="$2" + shift + ;; + --wait) + positive "${2:-}" || usage + WAIT="$2" + shift + ;; + --size) + positive "${2:-}" || usage + SIZE="$2" + shift + ;; + *) usage ;; + esac + shift +done + +WORK="$(mktemp -d)" +cleanup() { + rm -rf "$WORK" + if [ "$STARTED" = 1 ] && [ "$KEEP" != 1 ]; then + "$SERVERS" down > /dev/null 2>&1 || true + fi +} +trap cleanup EXIT + +setup_failure() { + echo "SETUP $*" >&2 + exit 2 +} + +build_harness() { + if [ -z "$HARNESS" ]; then + HARNESS="$("$HERE/build-mac-harness.sh" "$BUILD")" || exit 2 + fi + [ -x "$HARNESS" ] || setup_failure "no harness at $HARNESS" + local selftest + selftest="$("$HARNESS" --mode selftest 2>&1)" + [[ "$selftest" == *"HARNESS ok"* ]] || setup_failure "the harness did not run: $selftest" + mkdir -p "$WORK/home" +} + +start_server() { + local settings + settings="$("$SERVERS" env)" || setup_failure "could not read the SSH test servers' settings" + eval "$settings" + STARTED=1 + if [ -f "$SSH_TEST_KEYS/ed25519" ]; then + "$SERVERS" up v10 || setup_failure "the v10 test server did not start" + else + echo "no SSH test server keys yet: starting every server once to make them" >&2 + "$SERVERS" up || setup_failure "the SSH test servers did not start" + fi +} + +# Runs one mode, prints its result line after the label and keeps it in RESULT. The real home, +# ~/.ssh and SSH agent stay out of reach. +measure() { + local label="$1" kind="$2" output error + shift 2 + output="$(env -u SSH_AUTH_SOCK "HOME=$WORK/home" "CFFIXED_USER_HOME=$WORK/home" "$HARNESS" \ + --known-hosts "$WORK" --host "$SSH_TEST_HOST" --port "$SSH_TEST_PORT_V10" --user "$SSH_TEST_USER" \ + --auth password --password "$SSH_TEST_PASSWORD" --target "$SSH_TEST_TARGET" "$@" 2>&1)" + error="$(printf '%s\n' "$output" | grep -E '^ERROR' | tail -1)" + RESULT="$(printf '%s\n' "$output" | grep -E "^$kind " | tail -1)" + if [ -n "$error" ]; then + RESULT="$error" + FAILED=1 + elif [ -z "$RESULT" ]; then + RESULT="no result line: $(printf '%s' "$output" | tail -3 | tr '\n' ' ')" + FAILED=1 + fi + printf '%-28s %s\n' "$label" "$RESULT" +} + +# One name=value field of a result line, or nothing. +field() { + printf '%s\n' "$1" | tr ' ' '\n' | sed -n "s/^$2=//p" +} + +# The three-client idle run minus the run with none, over three. +per_relay() { + awk -v none="$(field "$1" "$3")" -v three="$(field "$2" "$3")" 'BEGIN { + if (none ~ /^[0-9.]+$/ && three ~ /^[0-9.]+$/) printf "%.2f", (three - none) / 3 + else printf "n/a" + }' +} + +build_harness +start_server +echo "macOS harness $HARNESS" +echo "server v10 on $SSH_TEST_HOST:$SSH_TEST_PORT_V10, target $SSH_TEST_TARGET" +echo "" + +IDLE_DEADLINE=$((WAIT + 120)) +TRANSFER_DEADLINE=$((SIZE * 2 + 600)) + +measure "idle, no client" IDLE --mode idle --count 0 --wait "$WAIT" --deadline "$IDLE_DEADLINE" +IDLE_NONE="$RESULT" +measure "idle, 1 client" IDLE --mode idle --count 1 --wait "$WAIT" --deadline "$IDLE_DEADLINE" +measure "idle, 3 clients" IDLE --mode idle --count 3 --wait "$WAIT" --deadline "$IDLE_DEADLINE" +IDLE_THREE="$RESULT" +measure "bulk, $SIZE MB" BULK --mode bulk --size "$SIZE" --deadline "$TRANSFER_DEADLINE" +measure "4 clients" CONCURRENT --mode concurrent --count 4 --rounds 500 --deadline "$TRANSFER_DEADLINE" +measure "4 clients, 16 MB GETs" CONCURRENT --mode concurrent --count 4 --rounds 500 --size 16 \ + --deadline "$TRANSFER_DEADLINE" + +echo "" +echo "each idle relay: wakeups/s=$(per_relay "$IDLE_NONE" "$IDLE_THREE" per_second)" \ + "threads=$(per_relay "$IDLE_NONE" "$IDLE_THREE" threads)" +[ "$FAILED" -eq 0 ] || exit 1 diff --git a/apple/scripts/probes/ssh-tunnel-matrix/build-mac-harness.sh b/apple/scripts/probes/ssh-tunnel-matrix/build-mac-harness.sh index bc117a87fa..e852e54eb9 100755 --- a/apple/scripts/probes/ssh-tunnel-matrix/build-mac-harness.sh +++ b/apple/scripts/probes/ssh-tunnel-matrix/build-mac-harness.sh @@ -10,6 +10,7 @@ # is reused. # # Usage: scripts/probes/ssh-tunnel-matrix/build-mac-harness.sh +# TABLEPRO_SSH_HARNESS_OPTIMIZATION=-O builds it optimized, as the app ships, for throughput measurements. # # Prints the harness path. Exits 2 when it cannot build, with the first compiler error. @@ -50,6 +51,8 @@ MACOS_VERSION="$(awk ' TARGET="arm64-apple-macos$MACOS_VERSION" SDK="$(xcrun --sdk macosx --show-sdk-path 2> /dev/null)" || setup_failure "no macosx SDK" COMPILER="$(xcrun --sdk macosx swiftc --version 2> /dev/null)" +# -O measures what the app ships; the default keeps a matrix build quick. +OPTIMIZATION="${TABLEPRO_SSH_HARNESS_OPTIMIZATION:--Onone}" # The app's SSHTunnelManager and three Core/SSH files need AppKit or DatabaseManager; mac-harness.swift # stands in for what the tunnel uses from them. @@ -62,6 +65,7 @@ for source in "$SSH"/*.swift "$SSH"/Auth/*.swift; do done APP_SOURCES+=( "$ROOT/TablePro/Core/Database/ConnectionDeadline.swift" + "$ROOT/TablePro/Core/Diagnostics/LogRedaction.swift" "$ROOT/TablePro/Core/Transport/TransportActivityRegistry.swift" "$ROOT/TablePro/Core/Transport/TransportByteCounter.swift" "$HERE/harness-common.swift" @@ -155,7 +159,7 @@ while read -r module package; do SOURCES+=("$source") done < "$OUT/$module.sources" STAMP="$( - printf '%s\n%s\n%s\n%s\n' "$COMPILER" "$TARGET" "${FEATURES[*]-}" "$STAMP" + printf '%s\n%s\n%s\n%s\n%s\n' "$COMPILER" "$OPTIMIZATION" "$TARGET" "${FEATURES[*]-}" "$STAMP" stat -f '%N %m %z' "${SOURCES[@]}" )" STAMP="$(printf '%s' "$STAMP" | shasum -a 256 | cut -c1-16)" @@ -165,7 +169,7 @@ while read -r module package; do fi echo "compiling $module (${#SOURCES[@]} files)" >&2 rm -f "$OUT/$module.stamp" - xcrun --sdk macosx swiftc -sdk "$SDK" -target "$TARGET" -swift-version 6 -Onone \ + xcrun --sdk macosx swiftc -sdk "$SDK" -target "$TARGET" -swift-version 6 "$OPTIMIZATION" \ -parse-as-library -module-name "$module" -package-name "$package" -DSWIFT_PACKAGE -I "$OUT" \ ${FEATURES[@]+"${FEATURES[@]}"} \ -emit-module -emit-module-path "$OUT/$module.swiftmodule" \ @@ -177,7 +181,7 @@ done <<< "$MODULES" # The app target's own settings in project.yml: its Swift and header paths for CLibSSH2, and the # libssh2, OpenSSL and zlib it links. The rpath stands in for the copy the app embeds. echo "compiling the harness (${#APP_SOURCES[@]} files)" >&2 -xcrun --sdk macosx swiftc -sdk "$SDK" -target "$TARGET" -swift-version 6 -Onone \ +xcrun --sdk macosx swiftc -sdk "$SDK" -target "$TARGET" -swift-version 6 "$OPTIMIZATION" \ -parse-as-library -module-name TablePro "${APP_FEATURES[@]}" \ -I "$OUT" -I "$SSH/CLibSSH2" -Xcc "-I$SSH/CLibSSH2/include" \ "${APP_SOURCES[@]}" "${LIBRARIES[@]}" \ diff --git a/apple/scripts/probes/ssh-tunnel-matrix/harness-common.swift b/apple/scripts/probes/ssh-tunnel-matrix/harness-common.swift index bb88d7dbbf..5c16db9889 100644 --- a/apple/scripts/probes/ssh-tunnel-matrix/harness-common.swift +++ b/apple/scripts/probes/ssh-tunnel-matrix/harness-common.swift @@ -33,8 +33,10 @@ nonisolated struct HarnessOptions: Sendable { var jumpPassphrase: String? var target = "redis:6379" var count = 1 + var rounds = 500 + var sizeMegabytes: Int? var marker: String? - var waitSeconds = 75 + var waitSeconds: Int? var deadlineSeconds = 60 var knownHostsDirectory = NSTemporaryDirectory() @@ -62,7 +64,9 @@ nonisolated struct HarnessOptions: Sendable { case "--jump-key": jumpKey = value case "--jump-passphrase": jumpPassphrase = value case "--target": target = value - case "--count": count = try Self.number(value, flag) + case "--count": count = try Self.number(value, flag, allowZero: true) + case "--rounds": rounds = try Self.number(value, flag) + case "--size": sizeMegabytes = try Self.number(value, flag) case "--marker": marker = value case "--wait": waitSeconds = try Self.number(value, flag) case "--deadline": deadlineSeconds = try Self.number(value, flag) @@ -70,6 +74,8 @@ nonisolated struct HarnessOptions: Sendable { default: throw HarnessFailure(description: "unknown option \(flag)") } } + // Only idle has a use for a tunnel with no client: it is the baseline the relays add to. + guard count >= 1 || mode == "idle" else { throw HarnessFailure(description: "--count takes a positive number") } } static func endpoint(_ text: String) throws -> (host: String, port: Int) { @@ -79,8 +85,10 @@ nonisolated struct HarnessOptions: Sendable { return (String(text[.. Int { - guard let value = Int(text), value > 0 else { throw HarnessFailure(description: "\(flag) takes a positive number") } + private static func number(_ text: String, _ flag: String, allowZero: Bool = false) throws -> Int { + guard let value = Int(text), value > 0 || (allowZero && value == 0) else { + throw HarnessFailure(description: "\(flag) takes a positive number") + } return value } } @@ -110,13 +118,15 @@ nonisolated enum Output { nonisolated enum LocalClient { static let ioTimeoutSeconds = 10 + // A bulk value is tens of megabytes: one slow read or write through the tunnel can pass 10 s. + static let bulkTimeoutSeconds = 60 - static func open(port: Int) throws -> Int32 { + static func open(port: Int, timeoutSeconds: Int = LocalClient.ioTimeoutSeconds) throws -> Int32 { let fd = socket(AF_INET, SOCK_STREAM, 0) guard fd >= 0 else { throw HarnessFailure(description: "socket: \(lastError())") } var on: Int32 = 1 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on, socklen_t(MemoryLayout.size)) - var timeout = timeval(tv_sec: ioTimeoutSeconds, tv_usec: 0) + var timeout = timeval(tv_sec: timeoutSeconds, tv_usec: 0) setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, socklen_t(MemoryLayout.size)) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, socklen_t(MemoryLayout.size)) var address = loopback(port: port) @@ -163,24 +173,52 @@ nonisolated enum LocalClient { try expectPong(fd) } - // The port is free when this returns, not reserved: whoever binds it next can still lose it. - static func freePort() throws -> Int { - let fd = socket(AF_INET, SOCK_STREAM, 0) - guard fd >= 0 else { throw HarnessFailure(description: "socket: \(lastError())") } - defer { Darwin.close(fd) } - var address = loopback(port: 0) - let bound = withUnsafePointer(to: &address) { - $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { - Darwin.bind(fd, $0, socklen_t(MemoryLayout.size)) + static func sendAll(_ fd: Int32, _ bytes: [UInt8]) throws { + var offset = 0 + while offset < bytes.count { + let sent = bytes.withUnsafeBytes { Darwin.send(fd, $0.baseAddress?.advanced(by: offset), $0.count - offset, 0) } + if sent > 0 { + offset += sent + } else if sent < 0, errno == EINTR { + continue + } else { + throw HarnessFailure(description: "send after \(offset) of \(bytes.count) bytes: \(lastError())") } } - guard bound == 0 else { throw HarnessFailure(description: "bind 127.0.0.1:0: \(lastError())") } - var length = socklen_t(MemoryLayout.size) - let named = withUnsafeMutablePointer(to: &address) { - $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { getsockname(fd, $0, &length) } + } + + // One byte at a time, so nothing after the line is taken off the socket. + static func readLine(_ fd: Int32) throws -> String { + var line: [UInt8] = [] + var byte: UInt8 = 0 + while true { + let count = Darwin.recv(fd, &byte, 1, 0) + if count == 1 { + if byte == UInt8(ascii: "\n") { break } + line.append(byte) + } else if count == 0 { + throw HarnessFailure(description: "the tunnel closed the client after \(line.count) bytes of a line") + } else if errno != EINTR { + throw HarnessFailure(description: "recv: \(lastError())") + } + } + if line.last == UInt8(ascii: "\r") { line.removeLast() } + return String(bytes: line, encoding: .utf8) ?? "" + } + + // Reads exactly count bytes, each chunk over the last in buffer. + static func receiveExactly(_ fd: Int32, count: Int, into buffer: inout [UInt8]) throws { + var remaining = count + while remaining > 0 { + let received = buffer.withUnsafeMutableBytes { Darwin.recv(fd, $0.baseAddress, min($0.count, remaining), 0) } + if received > 0 { + remaining -= received + } else if received == 0 { + throw HarnessFailure(description: "the tunnel closed the client with \(remaining) of \(count) bytes to go") + } else if errno != EINTR { + throw HarnessFailure(description: "recv with \(remaining) of \(count) bytes to go: \(lastError())") + } } - guard named == 0 else { throw HarnessFailure(description: "getsockname: \(lastError())") } - return Int(UInt16(bigEndian: address.sin_port)) } private static func loopback(port: Int) -> sockaddr_in { @@ -197,9 +235,193 @@ nonisolated enum LocalClient { } } +// Raw RESP to the redis behind the tunnel. +nonisolated enum RedisClient { + static func command(_ arguments: [String], valueLength: Int? = nil) -> [UInt8] { + var text = "*\(arguments.count + (valueLength == nil ? 0 : 1))\r\n" + for argument in arguments { + text += "$\(argument.utf8.count)\r\n\(argument)\r\n" + } + if let valueLength { + text += "$\(valueLength)\r\n" + } + return Array(text.utf8) + } + + // The value goes out from its own buffer rather than being copied into the request. + static func set(_ fd: Int32, key: String, value: [UInt8]) throws { + try LocalClient.sendAll(fd, command(["SET", key], valueLength: value.count)) + try LocalClient.sendAll(fd, value) + try LocalClient.sendAll(fd, Array("\r\n".utf8)) + let reply = try LocalClient.readLine(fd) + guard reply == "+OK" else { throw HarnessFailure(description: "SET \(key): expected +OK, got \(reply.debugDescription)") } + } + + static func get(_ fd: Int32, key: String, buffer: inout [UInt8]) throws -> Int { + try LocalClient.sendAll(fd, command(["GET", key])) + let header = try LocalClient.readLine(fd) + guard header.first == "$", let length = Int(header.dropFirst()), length >= 0 else { + throw HarnessFailure(description: "GET \(key): expected a bulk string, got \(header.debugDescription)") + } + try LocalClient.receiveExactly(fd, count: length, into: &buffer) + var trailer = [UInt8](repeating: 0, count: 2) + try LocalClient.receiveExactly(fd, count: 2, into: &trailer) + guard trailer == Array("\r\n".utf8) else { + throw HarnessFailure(description: "GET \(key): \(length) bytes did not end in CRLF") + } + return length + } + + static func delete(_ fd: Int32, key: String) throws { + try LocalClient.sendAll(fd, command(["DEL", key])) + let reply = try LocalClient.readLine(fd) + guard reply.hasPrefix(":") else { throw HarnessFailure(description: "DEL \(key): got \(reply.debugDescription)") } + } +} + +// Whole-process counters. The iOS harness reads neither and prints n/a. +nonisolated enum ProcessCounters { + static func wakeups() -> UInt64? { + #if os(macOS) + var info = rusage_info_v4() + let status = withUnsafeMutablePointer(to: &info) { + $0.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(getpid(), RUSAGE_INFO_V4, $0) } + } + guard status == 0 else { return nil } + return info.ri_interrupt_wkups + info.ri_pkg_idle_wkups + #else + return nil + #endif + } + + static func threads() -> Int? { + #if os(macOS) + var list: thread_act_array_t? + var count: mach_msg_type_number_t = 0 + guard task_threads(mach_task_self_, &list, &count) == KERN_SUCCESS, let threads = list else { return nil } + for index in 0...stride) + ) + return Int(count) + #else + return nil + #endif + } +} + +// Shared by the concurrent mode's client threads; every field is read and written under the lock. +nonisolated final class ConcurrentRun: @unchecked Sendable { + private let lock = NSLock() + private var pingersLeft: Int + private var latencies: [UInt64] = [] + private var failure: String? + private var startedAt: UInt64 = 0 + private var pingsEndedAt: UInt64 = 0 + private var bulkBytes = 0 + private var bulkEndedAt: UInt64 = 0 + + init(pingers: Int) { + pingersLeft = pingers + } + + var firstFailure: String? { + lock.withLock { failure } + } + + func begin() { + lock.withLock { startedAt = Self.now() } + } + + func ping(_ fd: Int32, client: Int, rounds: Int) { + var measured: [UInt64] = [] + measured.reserveCapacity(rounds) + var error: String? + for _ in 0.. 0 }) + let endedAt = Self.now() + lock.withLock { + bulkBytes = total + bulkEndedAt = endedAt + failure = failure ?? error + } + } + + func line(clients: Int, rounds: Int, bulk: Bool) -> String { + lock.withLock { + let sorted = latencies.sorted() + let slow = sorted.filter { $0 > 100_000_000 }.count + var text = "CONCURRENT clients=\(clients) rounds=\(rounds)" + text += " p50=\(Self.milliseconds(Self.percentile(sorted, 0.50)))" + text += " p99=\(Self.milliseconds(Self.percentile(sorted, 0.99)))" + text += " max=\(Self.milliseconds(sorted.last ?? 0)) over100ms=\(slow)" + text += " total=\(String(format: "%.3f", Double(pingsEndedAt - startedAt) / 1e9))" + if bulk { + let megabytes = Double(bulkBytes) / Double(TunnelHarness.bytesPerMegabyte) + text += " bulk=\(String(format: "%.1f", megabytes / (Double(bulkEndedAt - startedAt) / 1e9)))" + } + return text + } + } + + private static func now() -> UInt64 { + DispatchTime.now().uptimeNanoseconds + } + + // Nearest rank. + private static func percentile(_ sorted: [UInt64], _ fraction: Double) -> UInt64 { + guard !sorted.isEmpty else { return 0 } + let rank = Int((fraction * Double(sorted.count)).rounded(.up)) + return sorted[min(max(rank, 1), sorted.count) - 1] + } + + private static func milliseconds(_ nanoseconds: UInt64) -> String { + String(format: "%.1f", Double(nanoseconds) / 1e6) + } +} + nonisolated enum TunnelHarness { typealias Opener = @Sendable (HarnessOptions) async throws -> any HarnessTunnel + static let bytesPerMegabyte = 1 << 20 + static func options() -> HarnessOptions { do { return try HarnessOptions(arguments: Array(CommandLine.arguments.dropFirst())) @@ -223,6 +445,12 @@ nonisolated enum TunnelHarness { await tunnels(options, open: open) case "loss": await loss(options, open: open) + case "idle": + await idle(options, open: open) + case "bulk": + await bulk(options, open: open) + case "concurrent": + await concurrent(options, open: open) default: Output.finish("ERROR harness: unknown --mode \(options.mode)", code: 3) } @@ -325,9 +553,10 @@ nonisolated enum TunnelHarness { let clock = ContinuousClock() let start = clock.now let cpuAtStart = cpuSeconds() + let waitSeconds = options.waitSeconds ?? 75 var relayingStopped: Duration? var idleStopped: Duration? - while relayingStopped == nil || idleStopped == nil, start.duration(to: clock.now) < .seconds(options.waitSeconds) { + while relayingStopped == nil || idleStopped == nil, start.duration(to: clock.now) < .seconds(waitSeconds) { if relayingStopped == nil, !relaying.isRunning { relayingStopped = start.duration(to: clock.now) } if idleStopped == nil, !idle.isRunning { idleStopped = start.duration(to: clock.now) } try? await Task.sleep(for: .milliseconds(100)) @@ -340,6 +569,159 @@ nonisolated enum TunnelHarness { ) } + // The counters are the whole process, so the run with no client is the tunnel's own cost and the + // runs with clients add each relay's. + static func idle(_ options: HarnessOptions, open: Opener) async -> Never { + let waitSeconds = options.waitSeconds ?? 30 + let tunnel = await openOrFinish(options, open: open) + var sockets: [Int32] = [] + do { + for _ in 0.. Never { + let megabytes = options.sizeMegabytes ?? 64 + let size = megabytes * bytesPerMegabyte + let key = "tp-relay-bulk" + let tunnel = await openOrFinish(options, open: open) + let upload: Duration + let download: Duration + do { + let fd = try LocalClient.open(port: tunnel.localPort, timeoutSeconds: LocalClient.bulkTimeoutSeconds) + let value = [UInt8](repeating: UInt8(ascii: "a"), count: size) + let clock = ContinuousClock() + let uploadStart = clock.now + try RedisClient.set(fd, key: key, value: value) + upload = uploadStart.duration(to: clock.now) + + var buffer = [UInt8](repeating: 0, count: 1 << 20) + let downloadStart = clock.now + let received = try RedisClient.get(fd, key: key, buffer: &buffer) + download = downloadStart.duration(to: clock.now) + guard received == size else { throw HarnessFailure(description: "GET returned \(received) of \(size) bytes") } + + try RedisClient.delete(fd, key: key) + Darwin.close(fd) + } catch { + Output.finish("ERROR relay: bulk: \(error)", code: 1) + } + tunnel.close() + let uploadSeconds = interval(upload) + let downloadSeconds = interval(download) + Output.finish( + "BULK size=\(megabytes)MB upload=\(String(format: "%.1f", Double(megabytes) / uploadSeconds))" + + " download=\(String(format: "%.1f", Double(megabytes) / downloadSeconds))" + + " upload_s=\(String(format: "%.3f", uploadSeconds)) download_s=\(String(format: "%.3f", downloadSeconds))", + code: 0 + ) + } + + // Every client connects, and the bulk value is stored, before the gate starts them all at once. + static func concurrent(_ options: HarnessOptions, open: Opener) async -> Never { + let key = "tp-relay-concurrent" + let rounds = options.rounds + let tunnel = await openOrFinish(options, open: open) + var pingers: [Int32] = [] + var bulkClient: (fd: Int32, size: Int)? + do { + for _ in 0..) in + group.notify(queue: .global()) { continuation.resume() } + } + + if let failure = run.firstFailure { + Output.finish("ERROR relay: \(failure)", code: 1) + } + if let bulkClient { + try? RedisClient.delete(bulkClient.fd, key: key) + Darwin.close(bulkClient.fd) + } + pingers.forEach { Darwin.close($0) } + tunnel.close() + Output.finish(run.line(clients: options.count, rounds: rounds, bulk: bulkClient != nil), code: 0) + } + + private static func pingEach(_ sockets: [Int32], when: String) { + for (index, fd) in sockets.enumerated() { + do { + try LocalClient.sendPing(fd) + try LocalClient.expectPong(fd) + } catch { + Output.finish("ERROR relay: client \(index + 1) of \(sockets.count) \(when): \(error)", code: 1) + } + } + } + private static func openOrFinish(_ options: HarnessOptions, open: Opener) async -> any HarnessTunnel { do { return try await open(options) @@ -350,8 +732,11 @@ nonisolated enum TunnelHarness { private static func seconds(_ duration: Duration?) -> String { guard let duration else { return "none" } - let value = Double(duration.components.seconds) + Double(duration.components.attoseconds) / 1e18 - return String(format: "%.1f", value) + return String(format: "%.1f", interval(duration)) + } + + private static func interval(_ duration: Duration) -> Double { + Double(duration.components.seconds) + Double(duration.components.attoseconds) / 1e18 } private static func cpuSeconds() -> Double { diff --git a/apple/scripts/probes/ssh-tunnel-matrix/mac-harness.swift b/apple/scripts/probes/ssh-tunnel-matrix/mac-harness.swift index dae0360e53..2e2b16eb68 100644 --- a/apple/scripts/probes/ssh-tunnel-matrix/mac-harness.swift +++ b/apple/scripts/probes/ssh-tunnel-matrix/mac-harness.swift @@ -60,9 +60,7 @@ nonisolated final class PromptKeyboardInteractiveProvider: KeyboardInteractivePr nonisolated extension LibSSH2Tunnel: HarnessTunnel {} nonisolated enum MacTunnelOpener { - private static let portAttempts = 5 - - // What SSHTunnelManager.createTunnel does, with a free port in place of its random one. + // What SSHTunnelManager.createTunnel does, with its default port candidates. static func open(_ options: HarnessOptions) async throws -> LibSSH2Tunnel { let config = try configuration(options) let target = try HarnessOptions.endpoint(options.target) @@ -74,28 +72,20 @@ nonisolated enum MacTunnelOpener { keyboardInteractivePromptProvider: nil ) let deadline = ConnectionDeadline(configuredSeconds: nil) - var attempt = 1 - while true { - let localPort = try LocalClient.freePort() - do { - let tunnel = try await Task.detached { - try await LibSSH2TunnelFactory.createTunnel( - connectionId: UUID(), - config: config, - credentials: credentials, - destination: destination, - localPort: localPort, - deadline: deadline - ) - }.value - tunnel.startForwarding(destination: destination) - tunnel.startKeepAlive() - return tunnel - } catch SSHTunnelError.tunnelCreationFailed(let message) - where message.contains("already in use") && attempt < portAttempts { - attempt += 1 - } - } + let listener = try LibSSH2TunnelFactory.bindLocalListener(candidates: Array(60_000...65_000).shuffled()) + let tunnel = try await Task.detached { + try await LibSSH2TunnelFactory.createTunnel( + connectionId: UUID(), + config: config, + credentials: credentials, + destination: destination, + listener: listener, + deadline: deadline + ) + }.value + tunnel.startForwarding(destination: destination) + tunnel.startKeepAlive() + return tunnel } private static func configuration(_ options: HarnessOptions) throws -> SSHConfiguration {