Skip to content

Commit a7963b7

Browse files
committed
Fix ComposerRepository handling of offline state to allow resolution as long as everything is present in the cache, fixes #10116
1 parent edccad4 commit a7963b7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Composer/Repository/ComposerRepository.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,14 @@ protected function fetchFile($filename, $cacheKey = null, $sha256 = null, $store
11781178
throw $e;
11791179
}
11801180

1181+
// try to detect offline state (if dns resolution fails it is pretty likely to keep failing) and avoid retrying in that case
1182+
if ($e instanceof TransportException && $e->getStatusCode() === null) {
1183+
$responseInfo = $e->getResponseInfo();
1184+
if (isset($responseInfo['namelookup_time']) && $responseInfo['namelookup_time'] == 0) {
1185+
$retries = 0;
1186+
}
1187+
}
1188+
11811189
if ($retries) {
11821190
usleep(100000);
11831191
continue;
@@ -1349,7 +1357,7 @@ private function asyncFetchFile($filename, $cacheKey, $lastModifiedTime = null)
13491357
return $data;
13501358
};
13511359

1352-
$reject = function ($e) use (&$retries, $httpDownloader, $filename, $options, &$reject, $accept, $io, $url, &$degradedMode, $repo) {
1360+
$reject = function ($e) use (&$retries, $httpDownloader, $filename, $options, &$reject, $accept, $io, $url, &$degradedMode, $repo, $lastModifiedTime) {
13531361
if ($e instanceof TransportException && $e->getStatusCode() === 404) {
13541362
$repo->packagesNotFoundCache[$filename] = true;
13551363

@@ -1361,6 +1369,14 @@ private function asyncFetchFile($filename, $cacheKey, $lastModifiedTime = null)
13611369
$retries = 0;
13621370
}
13631371

1372+
// try to detect offline state (if dns resolution fails it is pretty likely to keep failing) and avoid retrying in that case
1373+
if ($e instanceof TransportException && $e->getStatusCode() === null) {
1374+
$responseInfo = $e->getResponseInfo();
1375+
if (isset($responseInfo['namelookup_time']) && $responseInfo['namelookup_time'] == 0) {
1376+
$retries = 0;
1377+
}
1378+
}
1379+
13641380
if (--$retries > 0) {
13651381
usleep(100000);
13661382

@@ -1372,6 +1388,11 @@ private function asyncFetchFile($filename, $cacheKey, $lastModifiedTime = null)
13721388
}
13731389
$degradedMode = true;
13741390

1391+
// if the file is in the cache, we fake a 304 Not Modified to allow the process to continue
1392+
if ($lastModifiedTime) {
1393+
return $accept(new Response(array('url' => $url), 304, array(), ''));
1394+
}
1395+
13751396
// special error code returned when network is being artificially disabled
13761397
if ($e instanceof TransportException && $e->getStatusCode() === 499) {
13771398
return $accept(new Response(array('url' => $url), 404, array(), ''));

src/Composer/Util/Http/CurlDownloader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ public function tick()
341341
if (!$error && function_exists('curl_strerror')) {
342342
$error = curl_strerror($errno);
343343
}
344+
$progress['error_code'] = $errno;
344345
throw new TransportException('curl error '.$errno.' while downloading '.Url::sanitize($progress['url']).': '.$error);
345346
}
346347
$statusCode = $progress['http_code'];

0 commit comments

Comments
 (0)