When running Apache Bench (ab) on macOS to test a local Elasticsearch instance at http://localhost:9202
, developers often encounter the cryptic error:
apr_socket_connect(): Invalid argument (22)
This typically occurs when ab attempts to establish a TCP connection but fails due to misconfigured networking parameters.
The error stems from macOS's specific handling of IPv6 and localhost resolution. The APR (Apache Portable Runtime) library used by ab gets confused when:
- DNS resolution returns both IPv4 and IPv6 addresses for localhost
- The target port isn't properly specified in the connection attempt
- Network stack receives malformed socket parameters
Here are three proven methods to resolve this issue:
Method 1: Force IPv4 resolution
ab -n 100 -c 10 http://127.0.0.1:9202/
Using the explicit IPv4 loopback address bypasses IPv6 resolution issues entirely.
Method 2: Update your ab version
brew update && brew upgrade ab
Newer versions of Apache Bench handle dual-stack sockets more gracefully.
Method 3: Modify hosts file
# Edit /etc/hosts
::1 localhost
127.0.0.1 localhost
For persistent cases, try these diagnostic commands:
# Check port accessibility
nc -zv localhost 9202
# Verify DNS resolution
dig localhost
# Test with curl for comparison
curl -v http://localhost:9202
If the issue persists across tools, examine your Elasticsearch network binding configuration in elasticsearch.yml
:
network.host: 0.0.0.0
http.port: 9202
When ab proves problematic, consider these alternatives:
wrk
: Modern HTTP benchmarking toolsiege
: Advanced load testinghey
: Golang-based ab alternative
Example with hey:
hey -n 100 -c 10 http://localhost:9202
When running Apache Bench (ab) on macOS to test endpoints like http://localhost:9202
, you might encounter:
apr_socket_connect(): Invalid argument (22)
This typically indicates a socket connection issue, often related to DNS resolution, port configuration, or IPv6/IPv4 handling.
Here are frequent cases where this occurs:
- Testing
localhost
with explicit port numbers - Using hostnames instead of IP addresses
- IPv6 misconfiguration in modern macOS versions
Solution 1: Force IPv4
ab -n 100 -c 10 http://127.0.0.1:9202/
Replace localhost
with 127.0.0.1
to bypass IPv6 resolution.
Solution 2: Check Port Availability
lsof -i :9202 netstat -an | grep 9202
For persistent cases, enable verbose logging:
ab -v 4 -n 1 http://localhost:9202
Sample output analysis:
LOG: open socket: Invalid argument (22) LOG: Connect to localhost:9202 failed
Edit /etc/hosts
to ensure proper resolution:
127.0.0.1 localhost ::1 localhost
If issues persist, consider:
brew install wrk wrk -t4 -c100 -d10s http://localhost:9202