From 00b08aebf915142568f36ff4f9ed9a686d0bcd8a Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 18 Nov 2026 04:01:12 +0000 Subject: [PATCH 1/3] Check for calls from the background so a stalled API can't freeze the app The call check on the Log, Profile, Notifications, Remotes and conversation screens polled GET remotes/call every 3 seconds on the UI thread. When the server took the connection and never answered, each check held the UI thread for the full 20 second timeout, and the next check was already due when it gave up, so taps went unanswered until the server came back. The check is now signed on the UI thread and sent with FetchInBackground, like the screens' refreshes. Only one is out at a time, it gives up after 8 seconds instead of 20, and a failed check stays quiet. Co-Authored-By: Claude Opus 5.5 --- app/Devnull/Client.php | 31 ++++++++-- app/Devnull/FetchInBackground.php | 9 +-- .../Concerns/WatchesForCalls.php | 58 +++++++++++++++++-- tests/Feature/Screens/CallTest.php | 48 +++++++++++++++ 4 files changed, 133 insertions(+), 13 deletions(-) diff --git a/app/Devnull/Client.php b/app/Devnull/Client.php index b6538ae5..ee21af30 100644 --- a/app/Devnull/Client.php +++ b/app/Devnull/Client.php @@ -142,6 +142,16 @@ */ public const string ScheduledCommitsPath = 'scheduled-commits'; + /** + * Where the call ringing for you is, as the web checks for one every few seconds. + */ + public const string IncomingCallPath = 'remotes/call'; + + /** + * How long a request may take, in seconds, before it gives up as one that couldn't connect. + */ + public const int Timeout = 20; + public function __construct(private DeviceKey $deviceKey, private string $baseUrl, private ResponseCache $cache) {} /** @@ -685,7 +695,17 @@ public function unblock(string $username): void */ public function incomingCall(): ?IncomingCall { - $call = $this->json($this->send('GET', 'remotes/call'))['call'] ?? null; + return self::incomingCallIn($this->json($this->send('GET', self::IncomingCallPath))); + } + + /** + * The call ringing for you in devnull's answer to a call check, or null. + * + * @param array $json + */ + public static function incomingCallIn(array $json): ?IncomingCall + { + $call = $json['call'] ?? null; return is_array($call) ? IncomingCall::fromArray($call) : null; } @@ -888,13 +908,14 @@ private function signedMultipart(string $target, string $payload, array $files): /** * Sends a request signed elsewhere, exactly as signed: the body as the * bytes that were signed, or no body and no Content-Type when there was - * none. Returns the response whatever its status. + * none. Returns the response whatever its status. One that takes longer + * than $timeout seconds gives up as one that couldn't connect. * * @throws ConnectionException */ - public static function sendSigned(SignedRequest $request): Response + public static function sendSigned(SignedRequest $request, int $timeout = self::Timeout): Response { - $pending = Http::acceptJson()->withHeaders($request->headers)->timeout(20); + $pending = Http::acceptJson()->withHeaders($request->headers)->timeout($timeout); $pending = $request->body === '' ? $pending->withRequestMiddleware(fn (RequestInterface $request): RequestInterface => $request->withoutHeader('Content-Type')) @@ -947,7 +968,7 @@ private function send(string $method, string $path, array $query = [], ?array $p $body = self::body($payload); $headers = $this->sign($method, $target, $body); - $request = Http::acceptJson()->withHeaders($headers)->timeout(20); + $request = Http::acceptJson()->withHeaders($headers)->timeout(self::Timeout); $request = $payload === null ? $request->withRequestMiddleware(fn (RequestInterface $request): RequestInterface => $request->withoutHeader('Content-Type')) diff --git a/app/Devnull/FetchInBackground.php b/app/Devnull/FetchInBackground.php index bbb087cb..fb4ef273 100644 --- a/app/Devnull/FetchInBackground.php +++ b/app/Devnull/FetchInBackground.php @@ -22,8 +22,9 @@ * this, since it waits in memory until you scroll near it anyway. * * A write's body goes with the type it was signed with: JSON, or multipart - * for a commit with photos. It comes last, since a task's arguments are - * passed in order. + * for a commit with photos. It comes after the rest, since a task's arguments + * are passed in order, and so does how long the request may take, for a check + * that is soon worth nothing, such as one for a call ringing. */ final class FetchInBackground extends AsyncTask { @@ -67,14 +68,14 @@ public static function jsonOf(mixed $result): array * @param array $headers * @return array{status: int, json: mixed, retry_after: string, from_devnull: bool} */ - public function handle(string $url, array $headers, string $method = 'GET', string $body = '', bool $withImages = false, ?int $pruneBefore = null, string $contentType = 'application/json'): array + public function handle(string $url, array $headers, string $method = 'GET', string $body = '', bool $withImages = false, ?int $pruneBefore = null, string $contentType = 'application/json', int $timeout = Client::Timeout): array { if ($pruneBefore !== null) { DownloadImages::prune($pruneBefore); } try { - $response = Client::sendSigned(new SignedRequest($url, $headers, $method, $body, $contentType)); + $response = Client::sendSigned(new SignedRequest($url, $headers, $method, $body, $contentType), $timeout); } catch (ConnectionException) { return ['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false]; } diff --git a/app/NativeComponents/Concerns/WatchesForCalls.php b/app/NativeComponents/Concerns/WatchesForCalls.php index adc6271e..9a9a40e9 100644 --- a/app/NativeComponents/Concerns/WatchesForCalls.php +++ b/app/NativeComponents/Concerns/WatchesForCalls.php @@ -4,32 +4,82 @@ use App\Devnull\Account; use App\Devnull\Client; -use App\Devnull\Exceptions\ApiException; +use App\Devnull\Data\IncomingCall; use App\Devnull\Exceptions\KeychainUnavailable; +use App\Devnull\FetchInBackground; use App\NativeComponents\Call; +use Carbon\CarbonImmutable; use Native\Mobile\Attributes\Poll; use Native\Mobile\Edge\Transition; /** * Rings while the screen is up, as the web checks for a call every three seconds on every page: a call ringing for * you opens the call screen to pick up or decline. One you already saw ring doesn't ring again. + * + * Each check is signed here, where the Keychain is, and sent from a background PHP thread (see FetchInBackground), + * so the screen never waits on it. Asked here, a devnull that took the connection and never answered held the + * whole app for the request's 20 seconds, and the next check was due by then, so no tap was answered until devnull + * did. A check still out isn't sent again, and one that fails says nothing: the next one tries again. */ trait WatchesForCalls { + /** + * How long a check may wait for devnull, in seconds. The next one is due long before an answer this late would + * be worth anything, so it gives up and frees its background thread rather than wait out a request's usual time. + */ + public const int CallCheckTimeout = 8; + + /** + * When the check still out was sent, or null when none is. NativePHP drops the answer to one sent from a screen + * that has gone under another, so a check older than its time counts as gone. + */ + protected ?float $callCheckSentAt = null; + #[Poll(3000)] public function checkForCall(): void { - if (app(Account::class)->user() === null) { + if (app(Account::class)->user() === null || $this->checkingForCall()) { return; } try { - $call = app(Client::class)->incomingCall(); - } catch (ApiException|KeychainUnavailable) { + $request = app(Client::class)->signedGet(Client::IncomingCallPath); + } catch (KeychainUnavailable) { // The screen's own requests say what's wrong; this tries again on the next check. return; } + $this->callCheckSentAt = self::callCheckClock(); + + FetchInBackground::dispatch($request->url, $request->headers, 'GET', '', false, null, 'application/json', self::CallCheckTimeout) + ->timeout(self::CallCheckTimeout + 2) + ->finished(function (mixed $result): void { + $this->callCheckSentAt = null; + + if (FetchInBackground::failureOf($result) === null) { + $this->openRingingCall(Client::incomingCallIn(FetchInBackground::jsonOf($result))); + } + }) + ->failed(function (): void { + $this->callCheckSentAt = null; + }); + } + + private function checkingForCall(): bool + { + return $this->callCheckSentAt !== null && self::callCheckClock() - $this->callCheckSentAt < self::CallCheckTimeout + 2; + } + + private static function callCheckClock(): float + { + return (float) CarbonImmutable::now()->format('U.u'); + } + + /** + * Opens the call screen for a call ringing for you that hasn't rung here before. + */ + private function openRingingCall(?IncomingCall $call): void + { if ($call === null || $call->offer === '' || Call::hasSeen($call->offer)) { return; } diff --git a/tests/Feature/Screens/CallTest.php b/tests/Feature/Screens/CallTest.php index 032ca680..81adb40a 100644 --- a/tests/Feature/Screens/CallTest.php +++ b/tests/Feature/Screens/CallTest.php @@ -1,6 +1,7 @@ firePoll('checkForCall')->navigationIntent())->toBeNull(); }); +it('checks for a call from the background, so a devnull that never answers holds nothing up', function () { + holdRefreshes(); + serveApi([ + 'GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])], + 'GET /api/v1/remotes/call' => [['call' => ['partner' => userJson(['handle' => 'ada', 'name' => 'Ada Lovelace']), 'sdp' => CallOffer, 'is_muted' => false]]], + ]); + $thread = finishRefreshes(Native::visit('/remotes/conversations/ada')); + $sent = refreshesSent($thread); + + // The check is out, and the next one isn't sent on top of it. + $thread->firePoll('checkForCall')->firePoll('checkForCall'); + + expect(sentTo('GET', 'remotes/call'))->toBe([]) + ->and(refreshesSent($thread))->toBe($sent + 1) + ->and($thread->navigationIntent())->toBeNull(); + + finishRefreshes($thread)->assertNavigatedTo('/call/ada'); +}); + +it('checks for a call again once one devnull never answered has had its time', function () { + holdRefreshes(); + serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])]]); + $thread = finishRefreshes(Native::visit('/remotes/conversations/ada')); + $sent = refreshesSent($thread); + + $thread->firePoll('checkForCall'); + $this->travel(Log::CallCheckTimeout + 2)->seconds(); + $thread->firePoll('checkForCall'); + + expect(refreshesSent($thread))->toBe($sent + 2); +}); + +it('says nothing when a check for a call fails, and the next one goes', function () { + holdRefreshes(); + serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])]]); + $thread = finishRefreshes(Native::visit('/remotes/conversations/ada')); + $sent = refreshesSent($thread); + fakeOffline(); + + finishRefreshes($thread->firePoll('checkForCall'))->firePoll('checkForCall'); + + expect(refreshesSent($thread))->toBe($sent + 2) + ->and($thread->navigationIntent())->toBeNull() + ->and($thread->instance()->error)->toBeNull(); + $thread->assertNativeNotCalled('ToastKit.Show'); +}); + it('logs calls in the thread in the web\'s words, naming them where devnull leaves the author off', function () { $me = userJson()['id']; serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([ From 747c400baca38e6f9a75db12000a9363f862523e Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 18 Nov 2026 04:07:39 +0000 Subject: [PATCH 2/3] Say a commit may not have gone when the API took it and never answered A commit sent while the server was stalled timed out after 20 seconds and came back as "could not connect to the server", with the draft reopened. That's wrong: the request did reach the server, which may still post it, so sending the draft again could post it twice. FetchInBackground now says when a request timed out, as against one that never connected, and the outbox treats a timeout the way it already treats a task that gave up waiting: Outbox::NoAnswer, which asks you to check before you send it again. Co-Authored-By: Claude Opus 5.5 --- app/Devnull/FetchInBackground.php | 17 +++++++++--- app/NativeComponents/Concerns/SyncsOutbox.php | 5 ++-- tests/Feature/Devnull/ClientCacheTest.php | 13 ++++++++- tests/Feature/Screens/OutboxTest.php | 27 +++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/Devnull/FetchInBackground.php b/app/Devnull/FetchInBackground.php index fb4ef273..7872a9c7 100644 --- a/app/Devnull/FetchInBackground.php +++ b/app/Devnull/FetchInBackground.php @@ -54,6 +54,16 @@ public static function failureOf(mixed $result): ?ApiException }; } + /** + * Whether the request this task sent ran out of time before devnull + * answered: devnull, or something in front of it, took the connection and + * never said anything back. A write may have reached devnull all the same. + */ + public static function timedOut(mixed $result): bool + { + return is_array($result) && ($result['timed_out'] ?? false) === true; + } + /** * The JSON devnull answered a write with, or an empty array for a write it answered with nothing. * @@ -66,7 +76,7 @@ public static function jsonOf(mixed $result): array /** * @param array $headers - * @return array{status: int, json: mixed, retry_after: string, from_devnull: bool} + * @return array{status: int, json: mixed, retry_after: string, from_devnull: bool, timed_out?: bool} */ public function handle(string $url, array $headers, string $method = 'GET', string $body = '', bool $withImages = false, ?int $pruneBefore = null, string $contentType = 'application/json', int $timeout = Client::Timeout): array { @@ -76,8 +86,9 @@ public function handle(string $url, array $headers, string $method = 'GET', stri try { $response = Client::sendSigned(new SignedRequest($url, $headers, $method, $body, $contentType), $timeout); - } catch (ConnectionException) { - return ['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false]; + } catch (ConnectionException $exception) { + // cURL's "Operation timed out", as against a connection refused or a name that didn't resolve. + return ['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => str_contains($exception->getMessage(), 'cURL error 28')]; } $result = [ diff --git a/app/NativeComponents/Concerns/SyncsOutbox.php b/app/NativeComponents/Concerns/SyncsOutbox.php index 52b9a63e..8e069ca0 100644 --- a/app/NativeComponents/Concerns/SyncsOutbox.php +++ b/app/NativeComponents/Concerns/SyncsOutbox.php @@ -135,8 +135,9 @@ public function outboxAnswered(string $id = '', string $status = 'finished', mix return; } - // The task gave up waiting, so devnull may have a commit without having said so. - if ($status === 'failed') { + // The task gave up waiting, or devnull took the request and never answered it in time, so devnull may have a + // commit without having said so. + if ($status === 'failed' || FetchInBackground::timedOut($result)) { $this->writeTurnedDown($write, $write->isCommit() ? Outbox::NoAnswer : 'fatal: '.($message ?? 'the request did not finish.')); return; diff --git a/tests/Feature/Devnull/ClientCacheTest.php b/tests/Feature/Devnull/ClientCacheTest.php index dba1838d..781c69c8 100644 --- a/tests/Feature/Devnull/ClientCacheTest.php +++ b/tests/Feature/Devnull/ClientCacheTest.php @@ -7,6 +7,7 @@ use App\Devnull\RequestSigner; use App\Devnull\ResponseCache; use Carbon\CarbonImmutable; +use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; @@ -108,7 +109,17 @@ $request = $this->devnull->signedGet('me'); - expect((new FetchInBackground)->handle($request->url, $request->headers))->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false]); + expect((new FetchInBackground)->handle($request->url, $request->headers))->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => false]); +}); + +it('says when devnull took a request from the background and never answered it in time', function () { + serveApi(fn () => throw new ConnectionException('cURL error 28: Operation timed out after 20001 milliseconds with 0 bytes received')); + + $request = $this->devnull->signedGet('me'); + $result = (new FetchInBackground)->handle($request->url, $request->headers); + + expect($result)->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => true]) + ->and(FetchInBackground::timedOut($result))->toBeTrue(); }); it("fails a GET that gets a 200 with something other than devnull's answer, and keeps nothing from it", function (string $body) { diff --git a/tests/Feature/Screens/OutboxTest.php b/tests/Feature/Screens/OutboxTest.php index 7fa45271..7d5d5b38 100644 --- a/tests/Feature/Screens/OutboxTest.php +++ b/tests/Feature/Screens/OutboxTest.php @@ -4,6 +4,7 @@ use App\Devnull\Outbox; use App\Devnull\Stash; use App\Models\PendingWrite; +use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Str; @@ -264,6 +265,32 @@ function writesSent(): array ->and(PendingWrite::query()->count())->toBe(0); }); +it('says a commit may not have gone when devnull took it and never answered in time, and gives you the draft back', function () { + serveApi(fn (Request $request) => $request->method() === 'POST' + ? throw new ConnectionException('cURL error 28: Operation timed out after 20001 milliseconds with 0 bytes received') + : Http::response(pageJson([adasCommit()]))); + + $screen = Native::visit('/log', platform: 'android')->press('composeInSheet') + ->input('sheet-body', 'Shipped while it hung') + ->tap('sheet-commit') + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === Outbox::NoAnswer && $params['variant'] === 'error'); + + expect(nodeByRef($screen, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($screen, 'sheet-body')['props']['value'])->toBe('Shipped while it hung') + ->and(array_map(fn ($shown): string => $shown->id, $screen->instance()->commits))->toBe(['c1']); +}); + +it('still says devnull could not be reached when a commit never got through', function () { + serveApi(fn (Request $request) => $request->method() === 'POST' + ? throw new ConnectionException('cURL error 7: Failed to connect to devnull.test port 443') + : Http::response(pageJson([adasCommit()]))); + + Native::visit('/log', platform: 'android')->press('composeInSheet') + ->input('sheet-body', 'Shipped offline') + ->tap('sheet-commit') + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === "fatal: unable to access 'http://devnull.test': could not connect to the server"); +}); + it('brings back the photos and poll of a commit devnull turned down when its composer opens again', function () { $commit = leftover(PendingWrite::Commit, null, [ 'body' => 'Which one?', From c596b630cbdfd8926a6e2e154b3b15e1f5dd8a9c Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Wed, 18 Nov 2026 04:18:38 +0000 Subject: [PATCH 3/3] Don't leave a tab on skeleton rows while the API is stalled On Android a tab draws skeleton rows first and fills in what it kept once FirstFrame, an empty task, comes back from a background thread. There are four of those threads. While the server takes requests and doesn't answer, all four sit waiting on it, so FirstFrame queued behind them and the tab showed skeleton rows until its 10 second watchdog fired, with the kept rows ready the whole time. FirstFrame now gives up after 2 seconds, and the tab fills in then. Co-Authored-By: Claude Opus 5.5 --- app/Devnull/FirstFrame.php | 8 ++++++++ .../Concerns/DrawsSkeletonFirst.php | 2 +- tests/Feature/Screens/LogsTest.php | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/Devnull/FirstFrame.php b/app/Devnull/FirstFrame.php index d22e192f..7b990b53 100644 --- a/app/Devnull/FirstFrame.php +++ b/app/Devnull/FirstFrame.php @@ -11,6 +11,14 @@ */ final class FirstFrame extends AsyncTask { + /** + * How long a tab waits for it, in seconds, before it fills in anyway. It's + * back in a moment unless every background thread is busy, as they all are + * while devnull takes requests and doesn't answer them. The tab then + * shows what it kept rather than skeleton rows until one is free. + */ + public const int Within = 2; + public function handle(): bool { return true; diff --git a/app/NativeComponents/Concerns/DrawsSkeletonFirst.php b/app/NativeComponents/Concerns/DrawsSkeletonFirst.php index 0c72f6be..cdf5145e 100644 --- a/app/NativeComponents/Concerns/DrawsSkeletonFirst.php +++ b/app/NativeComponents/Concerns/DrawsSkeletonFirst.php @@ -43,7 +43,7 @@ protected function fillInAfterFirstFrame(string $then): void $this->skeletonFrame = true; FirstFrame::dispatch() - ->timeout(10) + ->timeout(FirstFrame::Within) ->finished(fn () => $this->fillIn($then)) ->failed(fn () => $this->fillIn($then)); } diff --git a/tests/Feature/Screens/LogsTest.php b/tests/Feature/Screens/LogsTest.php index 87ecfc52..615b43c6 100644 --- a/tests/Feature/Screens/LogsTest.php +++ b/tests/Feature/Screens/LogsTest.php @@ -1,6 +1,7 @@ toBe(['a']); }); +it('fills in what it kept when the first frame has had its time, though every background thread is busy', function () { + config(['devnull.skeleton_first' => true]); + holdRefreshes(); + keep('log/following', pageJson([commitJson(['id' => 'f1', 'body' => 'Kept following'])])); + + $screen = Native::visit('/log')->assertSet('skeletonFrame', true)->assertDontSee('Kept following'); + $firstFrame = $screen->bridge()->callsTo('AsyncTask.Dispatch')[0]['params']; + + expect($firstFrame['timeout'])->toBe(FirstFrame::Within); + + // Its watchdog fails it, as when it never got a thread. + $screen->emitNative(AsyncTaskFailed::class, ['id' => $firstFrame['id'], 'exceptionClass' => 'RuntimeException', 'message' => 'The async task did not complete within 2 seconds.']) + ->assertSet('skeletonFrame', false) + ->assertSee('Kept following'); +}); + it('opens on skeleton rows, and fills in what it kept straight after, so a tab tap shows at once', function () { config(['devnull.skeleton_first' => true]); holdRefreshes();