Closed
Description
Symfony version(s) affected
Description
When Controller's action method is returning a StreamedResponse like following sample code:
`
public function event(HttpClientInterface $client): StreamedResponse
{
$response = new StreamedResponse();
$response->setCallback(function () use ($client) {
$url = 'https://symfony.com';
$response = $client->request('GET', $url);
});
return $response;
}
`
And the callback method is sending an HTTP request thru the symfony HTTP Client.
The bug: The profiler's Http Client panel is empty.
According our investigation, this is due to the fact that kernel.response event is dispatched (and thus the call to HttpClientDataCollector::collect() method) before HttpClient send the request, so no data are yet collected.
HttpClientDataCollector is already implementing LateDataCollectorInterface but the lateCollect() is only resetting the client object.
How to reproduce
- Fork this github repo: https://github.com/pforesi/HttpClientDataCollector_issue
- Launch the app (symfony server:start)
- Browse https://127.0.0.1:8000/bug/reproducer
- Follow the " Steps to check the bug" on the served page.
Possible Solution
Do data collecting into the lateCollect() instead of the collect() method:
`
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$this->reset();
}
public function lateCollect()
{
foreach ($this->clients as $name => $client) {
[$errorCount, $traces] = $this->collectOnClient($client);
$this->data['clients'][$name] = [
'traces' => $traces,
'error_count' => $errorCount,
];
$this->data['request_count'] += \count($traces);
$this->data['error_count'] += $errorCount;
}
foreach ($this->clients as $client) {
$client->reset();
}
}
`
Additional Context
No response