Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind: address already in use #6

Closed
milahu opened this issue Nov 29, 2023 · 2 comments
Closed

bind: address already in use #6

milahu opened this issue Nov 29, 2023 · 2 comments

Comments

@milahu
Copy link

milahu commented Nov 29, 2023

im getting this error after killing and restarting the socks5 server

$ ./cmd/socks5/socks5
[socks5] 2023/11/29 17:40:38 listen tcp :1080: bind: address already in use

maybe the server is missing some cleanup code, to close all connections

after killing and restarting the socks5 server

im killing the server with control-c (sigint)
so the server should have enough time for cleanup

run.sh

#!/usr/bin/env bash

set -e

netstat_out="$(netstat -nap 2>/dev/null | grep -w 1080 || true)"
if [ -n "$netstat_out" ]; then
  echo "error: address already in use"
  echo "$netstat_out"
  if true; then
    exit 1
  else
    echo "killing worker procs using port 1080"
    netstat -nap 2>/dev/null | grep -w 1080 | awk '{ print $7 }' | cut -d/ -f1 | xargs kill
  fi
fi

cd cmd/socks5/ &&
go build &&
cd ../.. &&
./cmd/socks5/socks5 &
socks5_pid=$!;
echo socks5_pid: $socks5_pid;
sleep 1;
curl --proxy socks5h://localhost:1080 https://httpbin.org/ip;
kill $socks5_pid
$ ./run.sh 
socks5_pid: 371297
{
  "origin": "xxxxxxxxxxxxx"
}

$ ./run.sh 
error: address already in use
tcp6       0      0 :::1080                 :::*                    LISTEN      371359/./cmd/socks5 
tcp6       0      0 ::1:60936               ::1:1080                TIME_WAIT   -                   

problem: the worker process 371359 keeps running
after then main process 371297 was killed

@wzshiming
Copy link
Owner

wzshiming commented Nov 30, 2023

cd cmd/socks5/ &&
go build &&
cd ../.. &&
./cmd/socks5/socks5 &
socks5_pid=$!;

The socks5_pid is not the pid of socks5 but the pipeline ... && ... && ... &

cd cmd/socks5/ &&
go build &&
cd ../..

#########################

./cmd/socks5/socks5 &
socks5_pid=$!

Separate builds and runs to get their pid correctly

@milahu
Copy link
Author

milahu commented Nov 30, 2023

facepalm. thanks!

run.sh
#!/usr/bin/env bash

set -e

proxy_port=1080

netstat_out="$(netstat -nap 2>/dev/null | grep -w $proxy_port || true)"
if [ -n "$netstat_out" ]; then
  echo "error: address already in use"
  echo "found $(echo "$netstat_out" | wc -l) open connections with port $proxy_port:"
  echo "$netstat_out"
  #if true; then
  if false; then
    exit 1
  else
    echo "killing worker procs using port $proxy_port"
    netstat -nap 2>/dev/null | grep -w $proxy_port | awk '{ print $7 }' | cut -d/ -f1 | grep -vxFe - | xargs -r kill
  fi
fi

cd cmd/socks5/
go build
cd ../..

./cmd/socks5/socks5 &

socks5_pid=$!;
echo socks5_pid: $socks5_pid;
sleep 2;

#if true; then
if false; then
  # socks5
  curl --no-keepalive --proxy socks5://localhost:$proxy_port https://httpbin.org/ip;
else
  # socks5h
  curl --no-keepalive --proxy socks5h://localhost:$proxy_port https://httpbin.org/ip;
fi

kill $socks5_pid

now after running some cycles, there are some open connections
which disappear after a few seconds

$ netstat -nap 2>/dev/null | grep -w 1080
tcp6       0      0 ::1:59198               ::1:1080                TIME_WAIT   -                   
tcp6       0      0 ::1:40238               ::1:1080                TIME_WAIT   -                   
tcp6       0      0 ::1:54384               ::1:1080                TIME_WAIT   -                   

but they are not blocking the start of a new proxy server

@milahu milahu closed this as completed Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants