| … | … | @@ -169,9 +169,14 @@ async function handleConnect(service, serverKey, repoName, httpsUrl, stdout, std |
| 169 | 169 | stdout.write('\n') |
| 170 | 170 | |
| 171 | 171 | function cleanup (code) { |
| 172 | // Hard kill safety — if async teardown hangs, don't spin forever |
| 173 | const hardKill = setTimeout(() => process.exit(code), 5000) |
| 174 | hardKill.unref() |
| 172 | 175 | try { conn.destroy() } catch {} |
| 173 | | try { connector.destroy() } catch {} |
| 174 | | process.exit(code) |
| 176 | connector.destroy().catch(() => {}).finally(() => { |
| 177 | clearTimeout(hardKill) |
| 178 | process.exit(code) |
| 179 | }) |
| 175 | 180 | } |
| 176 | 181 | |
| 177 | 182 | // Manual bidirectional forwarding — NOT pipe()! |
| … | … | @@ -204,7 +209,15 @@ async function handleConnect(service, serverKey, repoName, httpsUrl, stdout, std |
| 204 | 209 | |
| 205 | 210 | } catch (err) { |
| 206 | 211 | stderr.write(`[punch] P2P connection failed: ${err.message}\n`) |
| 207 | | await connector.destroy() |
| 212 | |
| 213 | // Destroy swarm fully before exiting — premature process.exit() |
| 214 | // during async Hyperswarm teardown causes double-free crashes |
| 215 | // (libudx/io_uring buffers freed while still in use) and zombie |
| 216 | // processes that spin at 100% CPU. |
| 217 | const hardKill = setTimeout(() => process.exit(128), 5000) |
| 218 | hardKill.unref() |
| 219 | try { await connector.destroy() } catch {} |
| 220 | clearTimeout(hardKill) |
| 208 | 221 | |
| 209 | 222 | if (httpsUrl) { |
| 210 | 223 | stderr.write(`[punch] Falling back to HTTPS: ${httpsUrl}\n`) |