php / php/doc-en

HTTPS cURL request fails at 340th request when used in a recursive function

Open
#1,757 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Extension: curl Status: Needs Triage
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description

Recently a consumer of an API I had made ran into some strange issues, upon inspection of his code I discovered that the problem was because he used cURL in a recursive function, once put in a regular loop the problems disappeared.

Further inspection revealed this only seemed to occur with HTTPS requests, not plain HTTP. The server that the requests are made to does not make a difference as I've tested several differen hosts.

The included code uses the default snakeoil Apache configuration, the extra cert opts do not affect the outcome if left out on a properly configured server with TLS.

The following code:

<?php

/**
 * This will end at request php/php-src#340
 */
function recursiveFunctionTest($request_no = 1)
{
	echo "Request no #$request_no...\n";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://localhost/');
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($ch, CURLOPT_CAINFO, '/etc/ssl/certs/ssl-cert-snakeoil.pem');

	$response = curl_exec($ch);

	curl_close($ch);

	if ($response === false) {
		echo 'Request returned false', PHP_EOL;
		exit(1);
	}

	if ($request_no < 400) {
		recursiveFunctionTest(++$request_no);
	}

	echo "Done!\n";
}

recursiveFunctionTest();

Resulted in this output:

Request no php/php-src#1...
Request no php/php-src#2...
Request no php/php-src#3...
[...]
Request no php/php-src#338...
Request no php/php-src#339...
Request no php/php-src#340...
Request returned false

But I expected this output instead:

Request no php/php-src#1...
Request no php/php-src#2...
Request no php/php-src#3...
[...]
Request no php/php-src#398...
Request no php/php-src#399...
Request no php/php-src#400...
Done!

This code will run just fine:

<?php

/**
 * This will comfortably come to request php/php-src#400
 */
function whileLoopTest()
{
	$request_no = 0;

	do {
		$request_no++;

		echo "Request no #$request_no...\n";

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://localhost/');
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_CAINFO, '/etc/ssl/certs/ssl-cert-snakeoil.pem');

		$response = curl_exec($ch);

		curl_close($ch);

		if ($response === false) {
			echo 'Request returned false', PHP_EOL;
			exit(1);
		}
	} while($request_no < 400);

	echo "Done!\n";
}

whileLoopTest();
PHP Version

8.1.8

Operating System

Ubuntu 22.04 LTS

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided recursiveFunctionTest and whileLoopTest examples on PHP 8.1.8 under Ubuntu 22.04, comparing HTTPS cURL behavior near request 340. Trace the failing curl_exec result and determine what project change or documentation update is expected; done means the failure is explained and the issue has a reproducible, verifiable resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.