fix(ssh): prevent duplicate connections and race conditions#856
fix(ssh): prevent duplicate connections and race conditions#856arnestrickmann merged 1 commit intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile OverviewGreptile SummaryThis PR adds comprehensive connection deduplication and resource management to prevent duplicate SSH connections and race conditions across the application stack. Key improvements:
The changes are defensive and well-structured, with clear guard clauses at each layer of the stack (renderer → IPC → service). Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| src/main/services/ssh/SshService.ts | Added connection deduplication, pool limits, and fixed stale close handler race condition |
| src/main/ipc/sshIpc.ts | Added cleanup of stale connections before reconnect attempts |
| src/renderer/hooks/useRemoteProject.ts | Added client-side connect deduplication and stabilized reconnect reference to prevent interval loops |
Flowchart
flowchart TD
A[Renderer: useRemoteProject.connect] --> B{connectingIds has ID?}
B -->|Yes| C[Return early - deduplicate]
B -->|No| D[Add to connectingIds set]
D --> E[IPC: sshConnect]
E --> F[IPC Handler: sshIpc]
F --> G[SshService.connect]
G --> H{Connection exists?}
H -->|Yes| I[Return existing ID]
H -->|No| J{Pending connection?}
J -->|Yes| K[Coalesce onto promise]
J -->|No| L{Pool at limit?}
L -->|Yes| M[Throw error]
L -->|No| N[createConnection]
N --> O[Create SSH2 Client]
O --> P[Set up event handlers]
P --> Q[Connect]
Q --> R{Success?}
R -->|Ready| S[Add to pool]
R -->|Error| T[Reject promise]
S --> U[Delete from pending]
T --> V[Delete from pending]
U --> W[Return to renderer]
V --> X[Error to renderer]
W --> Y[Remove from connectingIds]
X --> Y
Z[SSH2 Client: close event] --> AA{Is this client in pool?}
AA -->|Yes| AB[Remove from pool]
AA -->|No| AC[Ignore stale event]
AD[Monitor: reconnect event] --> AE{isConnected?}
AE -->|Yes| AF[Disconnect stale]
AE -->|No| AG[Skip disconnect]
AF --> AH[Call connect]
AG --> AH
Last reviewed commit: d1bfadf
Additional Comments (1)
Consider wrapping the error handler to clean up properly: |
Summary
SshService.connect()— coalesces in-flight connection attempts for the same ID and reuses existing connections instead of opening duplicate TCP socketscloseevent handler that could remove a newer connection from the pool when an old client disconnectsconnectingIdsset to prevent multiple React re-renders from firing parallel connectsreconnectRefto stabilize the health-check interval and avoid unnecessary teardown/recreation loopsChanges
src/main/services/ssh/SshService.ts— Connection deduplication, pool limits, guardedclosehandlersrc/main/ipc/sshIpc.ts— Disconnect stale connection before reconnect attemptsrc/renderer/hooks/useRemoteProject.ts— Client-side connect deduplication, stable reconnect ref, guard against reconnecting while already connecting