If you've spent any time building on Solana using Anchor, you've probably encountered the dreaded localnet startup errors. You run anchor test or solana-test-validator, and instead of a smooth deployment, you're greeted with something like:
Unable to get latest blockhash. Test validator does not look started. Check .anchor/test-ledger/test-ledger-log.txt for errors.
Or perhaps you see connection refused errors on port 8899.
More often than not, the culprit is a zombie process holding onto the validator ports from a previous run that didn't shut down cleanly.
The Problem with pkill
Your first instinct might be to reach for pkill. You might try something like:
pkill -f solana-test-validator
While this works sometimes, it's notoriously unreliable for this specific issue. The process name might be slightly different, or the port might be held by a child process or a detached thread that pkill misses.
The Solution: fuser
Instead of guessing the process name, target the ports directly. The Solana test validator primarily uses ports 8899 (RPC) and 8900 (WebSocket).
You can forcefully kill whatever is listening on these ports using the fuser command:
fuser -k 8899/tcp
fuser -k 8900/tcp
The -k flag tells fuser to send a SIGKILL signal to any process using the specified port and protocol (tcp).
If you're also dealing with UDP port conflicts (sometimes port 8001 gets stuck during Anchor tests), you can clear those too:
fuser -k 8001/tcp
fuser -k 8001/udp
A Quick Cleanup Script
To make your life easier, you can alias this or drop it into a quick bash script:
#!/bin/bash
echo "Killing zombie Solana validator processes..."
fuser -k 8899/tcp 2>/dev/null
fuser -k 8900/tcp 2>/dev/null
fuser -k 8001/tcp 2>/dev/null
fuser -k 8001/udp 2>/dev/null
echo "Ports cleared. Ready for anchor test."
Conclusion
Next time anchor test hangs or complains about the validator not starting, skip the pkill guessing game. Use fuser -k to surgically clear the ports, and get back to shipping your Solana programs!
Top comments (0)