Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to Prismarr are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- **Prowlarr manual search returned nothing (and flagged Prowlarr down) when indexers were slow.** A search fans out to every enabled indexer and only answers once the slowest has replied — 7–20 s with a dozen indexers — but it went through the client's default 8 s timeout, so most searches came back empty, and the read timeout also opened the cross-request circuit breaker, showing Prowlarr as down everywhere for the breaker window. Search now gets a 90 s budget (`ProwlarrClient::SEARCH_TIMEOUT`), and, as for Radarr/Sonarr since #41, a read timeout after a successful connect fails only that call instead of opening the breaker.

## [1.3.0] - 2026-10-21

### Security
Expand Down
27 changes: 23 additions & 4 deletions symfony/src/Service/Media/ProwlarrClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,20 @@ public function getIndexerStatus(): array

// ── Search ────────────────────────────────────────────────────────────────

/**
* Budget for a manual search. Prowlarr fans the query out to every
* enabled indexer and only answers once the slowest one has replied (or
* hit Prowlarr's own per-indexer timeout): 7–20 s is normal with a dozen
* indexers, so the default 8 s turned most searches into "no results".
* Kept under PHP's default 120 s max_execution_time.
*/
public const SEARCH_TIMEOUT = 90;

public function search(string $query, ?int $indexerId = null, string $type = 'search', int $limit = 100): array
{
$params = ['query' => $query, 'type' => $type, 'limit' => $limit];
if ($indexerId !== null) $params['indexerIds'] = $indexerId;
$data = $this->get('/api/v1/search', $params);
$data = $this->get('/api/v1/search', $params, self::SEARCH_TIMEOUT);
if ($data === null) return [];

return array_map(fn($r) => [
Expand Down Expand Up @@ -619,7 +628,7 @@ private function normalizeIndexer(array $i): array

// ── HTTP ──────────────────────────────────────────────────────────────────

private function get(string $path, array $params = []): ?array
private function get(string $path, array $params = [], int $timeout = 8): ?array
{
if ($this->health->isDown(self::SERVICE_KEY)) {
$this->serviceUnavailable = true;
Expand All @@ -640,7 +649,7 @@ private function get(string $path, array $params = []): ?array
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, // SSRF guard
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, // (block file:// gopher:// ...)
CURLOPT_TIMEOUT => 8,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => 4,
CURLOPT_NOSIGNAL => 1,
CURLOPT_HTTPHEADER => ["X-Api-Key: {$this->apiKey}", 'Accept: application/json'],
Expand All @@ -649,14 +658,24 @@ private function get(string $path, array $params = []): ?array
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
$errno = curl_errno($ch);
$connected = curl_getinfo($ch, CURLINFO_CONNECT_TIME) > 0.0;
curl_close($ch);

if ($body === false || $code !== 200) {
$this->logger->warning("ProwlarrClient GET {$path} → HTTP {$code} {$err}");
$this->recordError('GET', $path, (int) $code, is_string($body) ? $body : '', $err);
if ($err !== '' || (int) $code === 0) {
// Same rule as RadarrClient::get() (issue #41): a read
// timeout after a successful connect means "slow answer"
// (a long indexer fan-out), not "dead service" — it fails
// this call and short-circuits the rest of the request, but
// must not open the cross-request breaker and flag Prowlarr
// down everywhere.
$this->serviceUnavailable = true;
$this->health->markDown(self::SERVICE_KEY);
if (!($errno === CURLE_OPERATION_TIMEDOUT && $connected)) {
$this->health->markDown(self::SERVICE_KEY);
}
}
return null;
}
Expand Down
76 changes: 76 additions & 0 deletions symfony/tests/Service/Media/ProwlarrClientTimeoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Tests\Service\Media;

use App\Service\ConfigService;
use App\Service\Media\ProwlarrClient;
use App\Service\Media\ServiceHealthCache;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

/**
* A manual Prowlarr search fans out to every indexer and routinely takes
* longer than the client's default 8 s. That read timeout used to empty the
* results AND open the cross-request circuit breaker, flagging Prowlarr down
* everywhere for the breaker window.
*/
#[AllowMockObjectsWithoutExpectations]
class ProwlarrClientTimeoutTest extends TestCase
{
private function client(string $baseUrl, ServiceHealthCache $health): ProwlarrClient
{
$config = $this->createMock(ConfigService::class);
$config->method('get')->willReturn(null);
$config->method('require')->willReturnCallback(
static fn(string $key) => $key === 'prowlarr_url' ? $baseUrl : 'test-key',
);

return new ProwlarrClient($config, new NullLogger(), $health);
}

private function get(ProwlarrClient $client, int $timeout): ?array
{
return (new \ReflectionMethod($client, 'get'))->invoke($client, '/api/v1/search', ['query' => 'x'], $timeout);
}

public function testSearchGetsABudgetWellAboveTheDefault(): void
{
self::assertGreaterThanOrEqual(60, ProwlarrClient::SEARCH_TIMEOUT);
self::assertLessThan(120, ProwlarrClient::SEARCH_TIMEOUT, 'must stay under PHP max_execution_time');
}

public function testAReadTimeoutAfterConnectDoesNotMarkProwlarrDown(): void
{
// A listening socket nobody accept()s on: the TCP connect succeeds
// (kernel backlog), the HTTP answer never comes — a slow indexer
// fan-out as far as curl can tell.
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
self::assertNotFalse($server, $errstr);
$port = (int) substr(strrchr(stream_socket_get_name($server, false), ':'), 1);

$health = new ServiceHealthCache(new ArrayAdapter());
$client = $this->client('http://127.0.0.1:' . $port, $health);

self::assertNull($this->get($client, 1));
self::assertFalse($health->isDown('prowlarr'), 'a slow answer is not a dead service');
self::assertSame([], $client->search('again'), 'the rest of this request still short-circuits');

fclose($server);
}

public function testAConnectionFailureStillMarksProwlarrDown(): void
{
// Grab a free port, then close it so the connect is refused.
$probe = stream_socket_server('tcp://127.0.0.1:0');
$port = (int) substr(strrchr(stream_socket_get_name($probe, false), ':'), 1);
fclose($probe);

$health = new ServiceHealthCache(new ArrayAdapter());
$client = $this->client('http://127.0.0.1:' . $port, $health);

self::assertNull($this->get($client, 1));
self::assertTrue($health->isDown('prowlarr'));
}
}