Bug #80830 [Com]: Running out of local ports when using curl

From: Date: Thu, 04 Mar 2021 20:07:01 +0000
Subject: Bug #80830 [Com]: Running out of local ports when using curl
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232565@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80830&edit=1 ID: 80830 Comment by: rtrtrtrtrt at dfdfdfdf dot dfd Reported by: external dot maik dot dieterich at bosch dot com Summary: Running out of local ports when using curl Status: Not a bug Type: Bug Package: Network related Operating System: Windows Server 2019 PHP Version: 7.4.15 Assigned To: cmb Block user comment: N Private report: N New Comment: it's just how TCP works https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states Previous Comments: ------------------------------------------------------------------------ [2021-03-04 18:32:27] external dot maik dot dieterich at bosch dot com First thanks for the quick response. What is not clear for me, is why the connection I open to the external server can be closed immediately and why this local local port connections, which i do not have if i use curl from command line, are still open even if the external connection I open is closed. Because each connection is opened in a different thread i think I can not reuse them. ------------------------------------------------------------------------ [2021-03-04 17:19:18] cmb@php.net I think @danack is spot on. But instead of fiddling with these Windows settings, I suggest to re-use the same curl resource (object as of PHP 8.0.0), like described by Dan, to avoid calling curl_easy_cleanup() under the hood, because[1] | re-using handles is a key to good performance with libcurl Anyway, this is not a PHP issue. [1] <https://curl.se/libcurl/c/curl_easy_cleanup.html> ------------------------------------------------------------------------ [2021-03-04 15:51:58] danack@php.net This is a feature of Windows. It keeps network connections open for a long time by default. You could either work around this by "Adjust the MaxUserPort and TcpTimedWaitDelay settings" - https://docs.microsoft.com/en-us/biztalk/technical-guides/settings-that-can-be-modified-to-improve-network-performance Or you can re-use the same Curl object, which will almost certainly re-use the underlying network connection if possible, ``` $ch = curl_init($url); for($i = 0; $i < 5000; $i++) { curl_setopt($ch, CURLOPT_URL, $url); $response = curl_exec($ch); } curl_close($ch); ``` I'll leave it open, so that someone who is more sure that there is nothing to be done in PHP, can close it. ------------------------------------------------------------------------ [2021-03-04 14:27:36] external dot maik dot dieterich at bosch dot com Description: ------------ We are having an application which runs in Apache and gets files via an API. These files are stored on a storage over a rest API. For transferring the files to the storage we are using PHP with curl. Php opens a local port to local port connection when a curl handle is initialized with a connection to an external server. This local connection stays in TIME_WAIT after the external connection and the curl handle is closed. When open connections with curl from command line without PHP these local port local port connections are not opened. Test script: --------------- for($i = 0; $i < 5000; $i++) { $ch = curl_init("your url"); $response = curl_exec($ch); curl_close($ch); } Expected result: ---------------- No connections in TIME_WAIT. Actual result: -------------- Many connections in TIME_WAIT. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80830&edit=1

« previous php.bugs (#232565) next »