<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vaibhav Shukla</title>
    <description>The latest articles on DEV Community by Vaibhav Shukla (@vaibhav_shukla_e69fea9037).</description>
    <link>https://dev.to/vaibhav_shukla_e69fea9037</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3875057%2Ff42c2c16-3fb0-4216-a257-561cd886eec5.jpg</url>
      <title>DEV Community: Vaibhav Shukla</title>
      <link>https://dev.to/vaibhav_shukla_e69fea9037</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vaibhav_shukla_e69fea9037"/>
    <language>en</language>
    <item>
      <title>I Got Tired of Killing My Dev Server Just to Free a Port, So I Built Something Weird</title>
      <dc:creator>Vaibhav Shukla</dc:creator>
      <pubDate>Sun, 12 Apr 2026 15:19:30 +0000</pubDate>
      <link>https://dev.to/vaibhav_shukla_e69fea9037/i-got-tired-of-killing-my-dev-server-just-to-free-a-port-so-i-built-something-weird-f3c</link>
      <guid>https://dev.to/vaibhav_shukla_e69fea9037/i-got-tired-of-killing-my-dev-server-just-to-free-a-port-so-i-built-something-weird-f3c</guid>
      <description>&lt;p&gt;You're running &lt;code&gt;npm run dev&lt;/code&gt; on port 3000. You need that port for 30 seconds to test something else. Your only option today: kill the dev server. Restart it later. Lose your in-memory caches, build watcher state, websocket clients, and warm-up time.&lt;/p&gt;

&lt;p&gt;I kept doing this. So I built &lt;a href="https://github.com/mr-vaibh/park" rel="noopener noreferrer"&gt;park&lt;/a&gt; — a CLI that freezes a running process AND releases its TCP port, then brings it back later. Same PID. Same memory. Same open files.&lt;/p&gt;

&lt;h2&gt;
  
  
  See it in action
&lt;/h2&gt;


&lt;div class="ltag_asciinema"&gt;
  
&lt;/div&gt;
&lt;br&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;park 3000                          &lt;span class="c"&gt;# server freezes, port frees&lt;/span&gt;
python3 &lt;span class="nt"&gt;-m&lt;/span&gt; http.server 3000 &amp;amp;     &lt;span class="c"&gt;# grab :3000 for something else&lt;/span&gt;
&lt;span class="nb"&gt;kill&lt;/span&gt; %1                            &lt;span class="c"&gt;# done with it&lt;/span&gt;
park resume 3000                   &lt;span class="c"&gt;# original server is back&lt;/span&gt;
curl localhost:3000                &lt;span class="c"&gt;# still serving, same warm state&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/mr-vaibh/park/main/install.sh | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why not just Ctrl+Z?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Ctrl+Z&lt;/code&gt; sends SIGSTOP. The process freezes — but the kernel &lt;strong&gt;still considers the port bound&lt;/strong&gt;. Try binding something else to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OSError: [Errno 48] Address already in use
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The port is locked until the process actually closes the socket or dies. SIGSTOP does neither.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trick: inject close(fd) from the outside
&lt;/h2&gt;

&lt;p&gt;The only thing that frees a TCP port is closing the file descriptor that owns the listening socket. park does this &lt;strong&gt;without modifying, recompiling, or restarting the target process&lt;/strong&gt; — it reaches inside and runs &lt;code&gt;close(fd)&lt;/code&gt; in the target's own context.&lt;/p&gt;

&lt;p&gt;This works on any process: &lt;code&gt;npm run dev&lt;/code&gt;, &lt;code&gt;python manage.py runserver&lt;/code&gt;, &lt;code&gt;uvicorn&lt;/code&gt;, &lt;code&gt;cargo run&lt;/code&gt;, anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it actually works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Linux — ptrace + syscall injection
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Find the PID owning the port by walking &lt;code&gt;/proc/net/tcp&lt;/code&gt; and &lt;code&gt;/proc/*/fd/*&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PTRACE_ATTACH&lt;/code&gt; to the target process&lt;/li&gt;
&lt;li&gt;Save the register state&lt;/li&gt;
&lt;li&gt;Find executable scratch space (the &lt;code&gt;[vdso]&lt;/code&gt; page), write a tiny &lt;code&gt;syscall; int3&lt;/code&gt; stub&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;RAX=3&lt;/code&gt; (close), &lt;code&gt;RDI=fd&lt;/code&gt;, &lt;code&gt;RIP=scratch&lt;/code&gt;, &lt;code&gt;PTRACE_CONT&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The target executes one &lt;code&gt;close()&lt;/code&gt; syscall, traps on &lt;code&gt;int3&lt;/code&gt;, we read the return value&lt;/li&gt;
&lt;li&gt;Restore everything, &lt;code&gt;PTRACE_DETACH&lt;/code&gt;, &lt;code&gt;SIGSTOP&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On resume: inject &lt;code&gt;socket() + setsockopt() + bind() + listen() + dup2()&lt;/code&gt; to recreate the listener at the exact same fd number. The process picks up exactly where it was.&lt;/p&gt;

&lt;h3&gt;
  
  
  macOS (Apple Silicon) — Mach kernel injection
&lt;/h3&gt;

&lt;p&gt;macOS doesn't have Linux's ptrace model. The approach is fundamentally different:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;task_for_pid()&lt;/code&gt; to get a Mach port to the target&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;task_suspend()&lt;/code&gt; to halt every thread&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;thread_suspend()&lt;/code&gt; each original thread individually — so they stay frozen even when we resume the task&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mach_vm_allocate()&lt;/code&gt; three 16KB pages inside the target: code (RX), data (RW), stack (RW)&lt;/li&gt;
&lt;li&gt;Write an arm64 &lt;code&gt;LDR+svc+brk&lt;/code&gt; stub onto the code page — the stub &lt;strong&gt;loads its arguments from the data page&lt;/strong&gt; via PC-relative LDR instructions rather than trusting registers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;thread_create_running()&lt;/code&gt; — spawn a &lt;strong&gt;brand-new thread&lt;/strong&gt; inside the target. We never touch any of the target's existing threads. This was the key insight that made macOS resume reliable.&lt;/li&gt;
&lt;li&gt;The injection thread runs the stub, hits &lt;code&gt;brk #0&lt;/code&gt;, we catch it via a Mach exception port&lt;/li&gt;
&lt;li&gt;On resume, also inject &lt;code&gt;kevent()&lt;/code&gt; to re-register the new listener with kqueue and &lt;code&gt;fcntl(F_SETFL, O_NONBLOCK)&lt;/code&gt; so async frameworks like uvicorn survive&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Three bugs that took the longest to find
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Closing the wrong fd on macOS.&lt;/strong&gt; The very first syscall after attaching had its &lt;code&gt;x0&lt;/code&gt; register clobbered by the kernel completing the target's in-progress syscall (usually &lt;code&gt;kqueue&lt;/code&gt;). So &lt;code&gt;close(3)&lt;/code&gt; was actually running as &lt;code&gt;close(0)&lt;/code&gt;. Fix: load args from memory via LDR instructions instead of trusting register state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Target dying after park exits.&lt;/strong&gt; Park's Mach exception port registration outlived the process — when park exited, the port became a dead name, and the next breakpoint in the target hit a dead-name send and died. Fix: save and restore the original exception port registration on detach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. uvicorn not responding after resume.&lt;/strong&gt; The recreated listener had no kqueue registration (the kernel removed it when we closed the old fd). Asyncio's event loop never woke up to call &lt;code&gt;accept()&lt;/code&gt;. TCP connections piled up in the kernel's backlog. Fix: inject &lt;code&gt;kevent(EVFILT_READ|EV_ADD)&lt;/code&gt; to re-register with every kqueue the target held. Also, the new socket was blocking by default — asyncio needs &lt;code&gt;O_NONBLOCK&lt;/code&gt; or it hangs in &lt;code&gt;accept()&lt;/code&gt; after the first connection.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it works with
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;th&gt;macOS (Apple Silicon)&lt;/th&gt;
&lt;th&gt;Linux x86_64&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;uvicorn / FastAPI&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;python http.server&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flask / Django dev&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Go net/http&lt;/td&gt;
&lt;td&gt;park only&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;npm run dev (Node)&lt;/td&gt;
&lt;td&gt;park only&lt;/td&gt;
&lt;td&gt;park + resume&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/mr-vaibh/park/main/install.sh | sh
park &lt;span class="nt"&gt;--help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Single binary. No runtime dependencies. MIT licensed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/mr-vaibh/park" rel="noopener noreferrer"&gt;github.com/mr-vaibh/park&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Website:&lt;/strong&gt; &lt;a href="https://byvaibhav.com/park/" rel="noopener noreferrer"&gt;byvaibhav.com/park&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;park is written in Go with one dependency (&lt;code&gt;golang.org/x/sys&lt;/code&gt;). CGO is used only on macOS for the Mach APIs; the Linux path is pure Go. The full source is ~2000 lines across 20 files.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
