Discussion:
curl get stuck when connect to and from the same ip/port
王岩
2018-10-31 05:36:34 UTC
Permalink
Hi,

I am running curl in linux and I meet the following issue.
(curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8
libidn/1.32 librtmp/2.3)

curl pick up a local port to connect a remote port, but if the remote is
also in the same instance, there is a small possibility(around 1/10000)
that the selected local port is just the same with the remote port. It
means that there will be a TCP connection from and to the same ip/port and
the curl command will get stuck forever.


I am always able to reproduce this issue by the following bash.
for i in {1..100000}
do
curl -XPOST "http://localhost:8288/" -s -d "dummy"
done

Note that there is no need to setup any service listening to 8288 port,
when the above bash loops at some point, curl will get stuck.

At this time, if we check the process, we can find curl process is taking
up the 8288 port unexpected.
sudo netstat -pant | grep 8288
tcp 0 0 127.0.0.1:8288 127.0.0.1:8288
ESTABLISHED 4072/curl
(The situation is the same as
https://stackoverflow.com/questions/4949858/how-can-you-have-a-tcp-connection-back-to-the-same-port
)

A workaround is set timeout(-m) to the curl command, but I still think this
behaviour is strange and causes unexpected behaviour. I will appreciate if
anyone could give some insight about this issue.

Thanks in advance.
Wang Yan
Daniel Stenberg
2018-10-31 22:03:43 UTC
Permalink
Post by 王岩
A workaround is set timeout(-m) to the curl command, but I still think this
behaviour is strange and causes unexpected behaviour. I will appreciate if
anyone could give some insight about this issue.
This is a side-effect of how port numbers and TCP works. It is also a reason
why you often run servers on port numbers that aren't included in the "random
pool range" that is used for source address when you connect with TCP.
--
/ daniel.haxx.se
Loading...