From e0ec28f02a802862e1b68c3b9cf07097cfa436c3 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 08:24:56 +0100 Subject: [PATCH] revert: drop $log clone and sdks in-lock re-read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both were defensive against rare edge cases the reviewer flagged but which don't justify the complexity: - $log clone: protects against tag pollution on the per-request Log if reportError fires AND the request later errors. http.php's request-end handler overwrites the core fields (namespace, message, action, etc.) anyway; only addTag/addExtra accumulate. Aligns with Embeddings/Text/Create.php precedent which mutates $log directly. - sdks in-lock re-read: closes a sequential-acquire stale-read race on the sdks append-list. The race exists but the impact is bounded — one SDK registration delayed until the next request from that SDK fires. Self-healing on retry. The codebase already accepts this exact race for auths, oAuthProviders, services, identities, sessions, factors, etc. Special-casing this one site is precision the analytics use case doesn't need. --- app/controllers/shared/api.php | 34 ++++++++++++---------------------- src/Appwrite/Locking/Lock.php | 34 +++++++++++++++------------------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2a1beadc04..ce0fd004cf 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -232,37 +232,27 @@ Http::init() $updates = new Document(); $accessedAt = $dbKey->getAttribute('accessedAt', 0); - $accessedAtStale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt; - if ($accessedAtStale) { + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt) { $updates->setAttribute('accessedAt', DateTime::now()); } $sdkValidator = new WhiteList($servers, true); $sdk = $request->getHeader('x-sdk-name', 'UNKNOWN'); - $sdkCandidate = ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk) && ! in_array($sdk, $dbKey->getAttribute('sdks', []))) ? $sdk : null; - if (! $updates->isEmpty() || $sdkCandidate !== null) { - $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $sdkCandidate, $apiKey, $project, $user, $team) { - // Re-read inside the lock so two sequential acquirers don't clobber each other's - // sdks-array appends. Skip semantics still drop concurrent acquirers' updates, - // but a same-SDK retry on the next request will pick the registration up. - if ($sdkCandidate !== null) { - $current = $dbForPlatform->getAuthorization()->skip( - fn () => $dbForPlatform->getDocument('keys', $dbKey->getId()) - ); - $sdks = $current->getAttribute('sdks', []); - if (! in_array($sdkCandidate, $sdks)) { - $sdks[] = $sdkCandidate; - $updates->setAttribute('sdks', $sdks); - $updates->setAttribute('accessedAt', DateTime::now()); - } - } + if ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk)) { + $sdks = $dbKey->getAttribute('sdks', []); - if ($updates->isEmpty()) { - return; - } + if (! in_array($sdk, $sdks)) { + $sdks[] = $sdk; + $updates->setAttribute('sdks', $sdks); + $updates->setAttribute('accessedAt', DateTime::now()); + } + } + + if (! $updates->isEmpty()) { + $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); if (! empty($apiKey->getProjectId())) { diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 6227ae3f5e..9ef6d70c03 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -209,30 +209,26 @@ final class Lock } self::$lastReportAt[$bucket] = $now; - // Clone the per-request Log so our lock-error fields don't bleed into the - // request-end submission of $this->log (action/message/tags get reset on the - // shared instance otherwise). - $lockLog = clone $this->log; - $lockLog->setNamespace('http'); - $lockLog->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); - $lockLog->setVersion(APP_VERSION_STABLE); - $lockLog->setType(Log::TYPE_WARNING); - $lockLog->setMessage('Distributed lock '.$action.': '.$e->getMessage()); - $lockLog->setAction("lock.{$action}"); - $lockLog->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + $this->log->setNamespace('http'); + $this->log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $this->log->setVersion(APP_VERSION_STABLE); + $this->log->setType(Log::TYPE_WARNING); + $this->log->setMessage('Distributed lock '.$action.': '.$e->getMessage()); + $this->log->setAction("lock.{$action}"); + $this->log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); - $lockLog->addTag('lock.target', $target); - $lockLog->addTag('lock.project', $this->projectInternalId); + $this->log->addTag('lock.target', $target); + $this->log->addTag('lock.project', $this->projectInternalId); // Strip trailing document ID to keep aggregator cardinality bounded. - $lockLog->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); - $lockLog->addTag('code', $e->getCode()); - $lockLog->addExtra('file', $e->getFile()); - $lockLog->addExtra('line', $e->getLine()); - $lockLog->addExtra('trace', $e->getTraceAsString()); + $this->log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $this->log->addTag('code', $e->getCode()); + $this->log->addExtra('file', $e->getFile()); + $this->log->addExtra('line', $e->getLine()); + $this->log->addExtra('trace', $e->getTraceAsString()); try { - $this->logger->addLog($lockLog); + $this->logger->addLog($this->log); } catch (Throwable) { } }