From 396a03bc8a319ab1f0cf9fa887dac74ea96bc9f9 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:09:19 +0100 Subject: [PATCH] fix: don't resurrect a canceled deployment on dispatch A deployment canceled while its source was being prepared (the Builds worker's template-into-repo push can take a while) was still built: submit() wrote the caller's stale status through upload() and then unconditionally moved it to 'waiting', so the Jobs worker saw a live build and could activate it. Drop the caller's stale status before upload() and guard the queued transition with notEqual('status', 'canceled'), so a cancel can no longer be clobbered. When the guard rejects the write, no job is submitted and the active deployment is left alone. The failure write is guarded the same way, so a cancel landing while the job is being submitted stays canceled. Co-Authored-By: Claude Opus 5 (1M context) --- src/Appwrite/Deployment/Deployments.php | 41 +++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Deployment/Deployments.php b/src/Appwrite/Deployment/Deployments.php index 4c18f3860c..82707f05bb 100644 --- a/src/Appwrite/Deployment/Deployments.php +++ b/src/Appwrite/Deployment/Deployments.php @@ -80,9 +80,10 @@ readonly class Deployments /** * Finalizes the deployment document (see upload() for what `$deployment` * should carry) and dispatches it for building from its own uploaded - * source. Deactivates any other active deployment for $resource, marks - * it queued and writes its buildPath. Returns the persisted, updated - * deployment. + * source. Marks it queued, writes its buildPath and deactivates any other + * active deployment for $resource. A deployment canceled before it is + * queued is left canceled and never dispatched. Returns the persisted, + * updated deployment. */ public function createFromUpload(Document $resource, Document $deployment): Document { @@ -128,22 +129,44 @@ readonly class Deployments private function submit(Document $resource, Document $deployment, ?array $source): Document { + // The caller may have been holding this deployment for a while (the + // Builds worker pushes a template commit first), so its status is stale + // by now — dropping it keeps upload() from writing a cancel away, and + // the queued transition below is guarded instead. + $deployment->removeAttribute('status'); $deployment = $this->upload($resource, $deployment); - $this->deactivateOthers($resource, $deployment); - $deployment = $this->dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + $queued = $this->dbForProject->updateDocuments('deployments', new Document([ 'status' => 'waiting', 'buildPath' => static::buildPath($this->project->getId(), $deployment->getId()), - ])); + ]), [ + Query::equal('$id', [$deployment->getId()]), + Query::notEqual('status', 'canceled'), + ]); + + $deployment = $this->dbForProject->getDocument('deployments', $deployment->getId()); + + // Canceled before it could be queued: leave it canceled, submit nothing, + // and leave the currently active deployment alone. + if ($queued === 0) { + return $deployment; + } + + $this->deactivateOthers($resource, $deployment); try { $this->jobs->create(...static::payload($this->project, $resource, $deployment, $this->platform, $source)); } catch (\Throwable $error) { - $deployment = $this->dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + // Guarded like the transition above: a cancel that landed while the + // job was being submitted must not be reported as a failure. + $this->dbForProject->updateDocuments('deployments', new Document([ 'status' => 'failed', 'buildLogs' => "\nAn internal error occurred while building. Please try again, and contact support if the problem persists.\n", 'buildEndedAt' => DateTime::now(), - ])); + ]), [ + Query::equal('$id', [$deployment->getId()]), + Query::notEqual('status', 'canceled'), + ]); throw $error; } @@ -163,7 +186,7 @@ readonly class Deployments /** * Deactivates any other active deployment for $resource before this one - * goes live, called right before the deployment is marked queued. + * goes live, called once the deployment is queued for building. */ protected function deactivateOthers(Document $resource, Document $deployment): void {