shadowsocks/shadowsocks-go

报错 "Address Already in use",然而端口没被占用

plbin97 opened this issue · 5 comments

Shadowsocks-server 输出:
2018/11/26 21:16:51 new udp port 39645 added
2018/11/26 21:16:51 listening udp port 39645
2018/11/26 21:16:51 server listening port 39645 ...
2018/11/26 21:16:51 new port 38847 added
2018/11/26 21:16:51 new udp port 38847 added
2018/11/26 21:16:51 listening udp port 38847
2018/11/26 21:16:51 error listening port 38847: listen tcp :38847: bind: address already in use
^C
[root@teenet thatseed]# lsof -i:38847
[root@teenet thatseed]#

我写了一个脚本用于自动添加很多的端口用户,通过UDP的用户管理API。
但是每次添加很多用户的时候,经常会报这个错。而且报错的端口是随机的。
我确认了,端口没有重复,而且用lsof确认了端口确实没有占用。
系统是CentOS 7.4 x64。
求大佬帮忙

日志就这些,我改了以下源码,如果出现用户添加报错,暂停半秒然后再重复操作就好了。

Well, you should know that close a tcp listener require a system call ,but, in golang ,the syscall is not called imediately, because golang use another thread to avoid the syscall from blocking the whole program.

@plbin97 I assume the net.Listener have their own file descriptor, so closing the net.Listener doesn’t close fds, and if those are left open, I assume the OS leaves that port bound, so you cannot rebind.

Perhaps you can try to enable port reuse by doing some hacks to net pcakage, or more simple ,just close the listener's fd.