Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions legacy/src/Service/CurlCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public function run(string $baseUrl, InputInterface $input, OutputInterface $out
$shouldRetry = false;
$newToken = '';
$stdoutBuffer = '';
$onOutput = function ($type, $buffer) use ($censor, $output, $stdErr, $process, $retryOn401, &$newToken, &$shouldRetry, &$stdoutBuffer) {
if ($shouldRetry) {
// Ensure there is no output after a retry is triggered.
return;
}
$stderrBuffer = '';
$onOutput = function ($type, $buffer) use ($censor, $output, $stdErr, $retryOn401, &$stdoutBuffer, &$stderrBuffer) {
if ($type === Process::OUT) {
if ($retryOn401) {
// Buffer stdout when we might need to retry on 401.
Expand All @@ -85,17 +82,8 @@ public function run(string $baseUrl, InputInterface $input, OutputInterface $out
return;
}
if ($type === Process::ERR) {
if ($retryOn401 && $this->parseCurlStatusCode($buffer) === 401 && $this->api->isLoggedIn()) {
$shouldRetry = true;
$stdoutBuffer = ''; // Discard buffered stdout from the 401 response.
$process->clearErrorOutput();
$process->clearOutput();

$newToken = $this->api->getAccessToken(true);
$stdErr->writeln('The access token has been refreshed. Retrying request.');

$process->stop();
return;
if ($retryOn401) {
$stderrBuffer .= $buffer;
}
if ($stdErr->isVeryVerbose()) {
$stdErr->write($censor($buffer));
Expand All @@ -107,11 +95,22 @@ public function run(string $baseUrl, InputInterface $input, OutputInterface $out

$process->run($onOutput);

// Check for 401 after the process completes, using the full accumulated
// stderr buffer. This avoids a race condition where the process may exit
// before the callback processes the stderr chunk containing the status line.
if ($retryOn401 && $this->parseCurlStatusCode($stderrBuffer) === 401 && $this->api->isLoggedIn()) {
$shouldRetry = true;
$stdoutBuffer = ''; // Discard buffered stdout from the 401 response.
$newToken = $this->api->getAccessToken(true);
$stdErr->writeln('The access token has been refreshed. Retrying request.');
}

if ($shouldRetry) {
// Create a new curl process, replacing the access token.
$commandline = $this->buildCurlCommand($url, $newToken, $input);
$process = Process::fromShellCommandline($commandline);
$stdoutBuffer = ''; // Reset the buffer for the retry.
$stdoutBuffer = ''; // Reset the buffers for the retry.
$stderrBuffer = '';
$shouldRetry = false;

// Update the $token variable in the $censor closure.
Expand Down