Repository navigation
Description
What happened?
TablePro sits at ~90–100% of a CPU core while completely idle (no query running, app in the background). Profiling shows the CPU is not in the UI/SwiftUI layer at all — the main/SwiftUI thread is parked, and the busy thread is a background SSH-tunnel relay task for a connection that has already been closed by the peer, spinning in its read loop instead of tearing down.
In other words: an SSH tunnel drops (laptop sleep, network change, or server-side idle timeout), the underlying socket goes to TCP CLOSED, but the relay loop keeps running at full tilt forever until the app is quit. With two dropped tunnels held open, this pegs a core continuously and accumulated 15h+ of CPU time over the app's uptime on my machine.
There are earlier (now-closed) high-CPU-on-idle reports (#368, #394, #646), but I have no evidence they share this SSH-tunnel cause — at least #646 was on local SQLite, which doesn't use a tunnel, so there may be more than one path to idle CPU. I'm only reporting what I can substantiate below.
Steps to reproduce
- Open a connection that uses SSH tunneling (single hop or jump host).
- Cause the tunnel to drop without cleanly closing the connection in-app — e.g. let the machine sleep, change networks, or let the SSH server time the session out.
- Leave TablePro idle.
- Observe TablePro pinned near 100% CPU in Activity Monitor even though nothing is running and there are no live connections.
Expected behavior
When an SSH tunnel's socket is hung up / errored, the relay loop should detect the hangup and tear the tunnel down, returning the app to ~0% CPU at idle.
Database type
N/A — this is in the SSH transport layer (TablePro/Core/SSH); the code path appears independent of the database driver. I observed it with my own SSH-tunneled connection after the tunnel dropped.
TablePro version
0.52.1 (build 101)
macOS version & chip
macOS 26.5.1 (build 25F80) / Apple M3 Pro
Screenshots / Logs
Per-second CPU while otherwise idle (ps -p <pid> -o %cpu): 100, 91, 92, 92, 91, then oscillating 2–55% — bursty but heavy, never settling. This confirms the process is genuinely CPU-bound, not just a wall-clock artifact.
sample taken while ps showed ~100% CPU — the main/SwiftUI thread is parked in mach_msg (not the source). The only non-idle background thread is a Swift-concurrency cooperative-pool task repeatedly observed in recv()/__recvfrom. "Sort by top of stack" from the sample:
mach_msg2_trap 9864 (idle run loops — parked)
__workq_kernreturn 6578 (idle worker threads — parked)
__recvfrom 3233 (the spinning recv path — the CPU)
Note: poll appears 0 times in the sample while recv dominates. A healthy idle relay should spend most of its time blocked in poll(…, 500); here the hot path is spending no observable time in that wait — either the outer poll() is returning immediately on a ready/hangup fd, or libssh2 is repeatedly reading the dependent transport fd inside its own read path.
lsof of the process (IPs redacted):
TCP <local-ip>:60666 -> <ssh-host>:22 (CLOSED)
TCP <local-ip>:64018 -> <ssh-host>:22 (CLOSED)
TCP 127.0.0.1:61790 (LISTEN)
established TCP connections: 0
Two SSH sockets in TCP CLOSED state, zero established connections — yet the app burns a full core relaying for tunnels that no longer exist.
sudo fs_usage -f network <pid> during a burst — pins the exact spinning syscall and fd:
recvfrom F=30 B=0x0 ... 0.000001 TablePro.39001127
recvfrom F=30 B=0x0 ... 0.000001 TablePro.39001127
... (60/60 captured events identical) ...
- Always the same fd (
F=30), at ~1µs spacing — a genuine tight spin (thousands of calls/sec), on the same thread (39001127) seen in the sample. B=0x0—fs_usagereports zero bytes transferred for eachrecvfrom; combined withlsofshowing fd 30 as a socketpair with no peer, this is consistent with EOF / a closed peer. I did not see an error marker (e.g.EAGAIN) on these lines.lsofmaps fd 30 to a UNIX-domainsocketpairwhose peer is gone:30u unix … ->(none)— i.e. the relay/transport socketpair, not the SSH TCP socket. This is the channel-relay socketpair fromstartChannelRelay(socketpair created atLibSSH2TunnelFactory.swift:174).
So: after the tunnel collapsed, fd 30 — a relay/dependent-hop socketpair fd whose peer is gone — is being read in a tight loop at ~1µs cadence. The relay's own direct recv(socketFD) path should exit on EOF, so the likelier owner is a libssh2 operation on the dependent hop/session that uses the other socketpair end as its transport (or another re-entry around that relay/session boundary). Either way, the closed socketpair is never treated as terminal.
Root cause (proposed)
Both relay loops poll the client and SSH sockets but only ever inspect POLLIN:
TablePro/Core/SSH/LibSSH2Tunnel.swift→runRelay(...)(loop at ~L365;pollFDs[*].revents & POLLINchecks at L376/L400)TablePro/Core/SSH/LibSSH2TunnelFactory.swift→ hop-relayTask(loop at ~L746; checks at L756/L781)
After the tunnel collapses, the relay socketpair's far end is closed, so the fd is permanently poll-ready (readable-at-EOF). Something reads that fd in a tight ~1µs loop, getting 0 bytes each time, with no observable time spent in the intended poll(…, 500) wait (matches the sample: recv dominant, poll absent). Critically, neither relay loop inspects POLLHUP/POLLERR/POLLNVAL (confirmed: those constants appear nowhere in Core/SSH), so a hung-up/closed relay fd is never treated as terminal; the loop keeps running instead of tearing down.
What I've confirmed vs. what's still open:
- Confirmed: process is CPU-pinned (
ps); SSH socketsCLOSEDwith zero live connections (lsof); the spin isrecvfromon fd 30 at ~1µs cadence returning 0 bytes (fs_usage); fd 30 is asocketpairwith a closed peer (the channel-relay socketpair); and neither relay loop handlesPOLLHUP/POLLERR/POLLNVAL. - Open (you'll know the ownership better than I can from a stripped binary): which read owns the loop. The relay's own
recv(socketFD)atLibSSH2TunnelFactory.swift:782has anif socketRead <= 0 { return }guard that should fire on EOF — so the more plausible owner is the other socketpair end:fds[1]becomescurrentSocketFDfor the next/dependent libssh2 session (LibSSH2TunnelFactory.swift:202,:248-255), whose transport fd is then polled and read viatablepro_libssh2_channel_readinrunRelay(LibSSH2Tunnel.swift:365-394). The empirical fact is just: a closed relay socketpair is read in a tight EOF loop and nothing tears it down.
Suggested fix
Both relay loops need to react to exceptional revents. Care is needed because on stream sockets POLLHUP can coexist with still-readable buffered data, so closing immediately on POLLHUP risks dropping the last bytes. A safer minimal shape: treat POLLERR/POLLNVAL as fatal right away, but for POLLHUP drain the existing POLLIN branches for that iteration first, then tear down:
let fatalMask = Int16(POLLERR | POLLNVAL)
if (pollFDs[0].revents & fatalMask) != 0 || (pollFDs[1].revents & fatalMask) != 0 {
return
}
let sawHangup = (pollFDs[0].revents & Int16(POLLHUP)) != 0
|| (pollFDs[1].revents & Int16(POLLHUP)) != 0
// ... run the existing POLLIN read/drain branches ...
if sawHangup {
return // peer hung up; stop relaying instead of spinning
}(Placed around the existing read branches, after if pollResult < 0 { break }.) Exact placement should preserve any buffered channel/socket data. Because the spinning fd is a relay socketpair, the closed peer should also propagate teardown to whatever depends on it — e.g. cancel the dependent hop's SSH session when its transport socketpair peer closes, so an inner session can't keep reading a dead socketpair. Separately, using libssh2_session_block_directions() around the EAGAIN read/write paths may reduce avoidable wakeups while the transport is healthy — but EOF/HUP still needs explicit teardown; it's not a substitute. I haven't built/tested any of this against the project, so treat it as a direction rather than a finished patch.
I'm happy to open a PR with this change plus a regression test if it'd be useful.
Disclosure (methodology): The investigation and this write-up were done with an AI assistant (Claude, Opus model) while tracing a real, user-observed high-CPU regression on my own machine. The
ps/sample/lsofoutput is from my live TablePro process and the source references are from the public repo — observed, not synthesized. I'm stating this up front so you can weigh the analysis accordingly; happy to gather any additional traces you'd want.
Activity
datlechin commentedon Jun 25, 2026
Hello @drekinov, thank you for the reporting. Glad to see your PR
fix(ssh): tear down tunnel relay on socket hangup instead of busy-spi…