Skip to content

fix(deletes): keep a recreated domain's certificate when its old rule is deleted - #13570

Merged
HarshMN2345 merged 14 commits into
mainfrom
claude/deletes-preserve-recreated-domain
Sep 18, 2026
Merged

HarshMN2345 merged 14 commits into
mainfrom
claude/deletes-preserve-recreated-domain

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Rule deletion is queued, so the deletes worker can run after the same domain has been added again under a new rule — delete a rule, add the domain back, and the old delete job lands second. On main, Deletes::deleteRule() then tears down the provider certificate and routing that the new rule just set up.

The recreated domain is left with no TLS until someone retries it by hand.

The fix guards the provider teardown on the domain still being free:

  • Provider cleanup and RuleDeleted only when no rule holds the domain any more. rules._key_domain is a unique index, so a single findOne on domain is exact. If a rule exists, the domain is live and the provider must be left alone.
  • The certificate document is still deleted unconditionally, as on main. A replacement rule always allocates its own certificate document, so keeping the old one orphans a record that reschedules renewals forever.

deleteCertificate() also now receives the rule's domain type, derived the same way the issuing sites derive it (getAttribute('deploymentResourceType', getAttribute('type'))). deploymentResourceType defaults to '' and is always persisted, so an API rule issues under '' — the delete has to agree, or a multi-provider deployment routes the teardown to a different bucket than the one that issued.

Note: this argument is inert in server-ce — LetsEncrypt::deleteCertificate() ignores it and this is the only call site — so it only matters downstream.

Test Plan

Verified manually, not by an automated test. The race needs a real certificate for a real domain, and the e2e stack cannot issue one: verifyRule() rejects a test domain, so a *.custom.localhost rule never reaches certificate issuance, certificateId stays '', and LetsEncrypt::deleteCertificate() unlinks a directory that was never created. A worker-level test was tried and dropped — standing in for the queue, the database and the provider around the single call under test does not buy confidence here. Per review, the check is to reproduce on prod, deploy to stage, and confirm the same flow works there.

tests/e2e/Services/Proxy/ProxyBase.php gains testDeleteAPIRule, covering API rule deletion, the missing-rule errors, and domain reuse through the public API. To be explicit: it does not gate this fix and passes unchanged on main. It is general Proxy-surface coverage that did not exist before.

Pint, PHPStan (level 4) and a Rector dry run are clean on the changed files.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — no API metadata changed.

… is deleted

Rule deletion is queued, so the deletes worker can run after the same
domain has been added again under a new rule. It then tore down the
provider certificate and routing that the new rule had just set up, and
dropped the certificate record the new rule may reference, leaving the
recreated domain without TLS until someone retried it by hand.

Only clean up the provider and dispatch RuleDeleted when no rule holds
the domain any more, and only delete the certificate record when no rule
references it. Pass the rule's domain type to the provider so cleanup
reaches the provider that issued the certificate.
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no new blocking or non-blocking findings.

Summary

This PR prevents a delayed rule-deletion job from tearing down provider routing and TLS after the same domain has been recreated.

  • Checks whether another rule currently owns the domain before dispatching provider cleanup.
  • Continues deleting the obsolete certificate document.
  • Adds public-API coverage for API-rule deletion, missing-rule responses, and domain reuse.
  • Removes the previously criticized implementation-coupled worker test.

Reviews (13) · Last reviewed commit: "Merge branch 'main' into claude/deletes-..."

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → claude/deletes-preserve-recreated-domain (after).

Metric Before After Change
🚀 Requests/sec 218.18 219.08 ⚪ +0.4%
⏱️ Latency P50 80.07 ms 79.46 ms ⚪ -0.8%
⏱️ Latency P95 190.54 ms 191.53 ms ⚪ +0.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 79.46 191.53 13,737 219.08 +0.98
Account 157.36 283.57 723 12.12 -20.54
TablesDB 76 145.68 7,471 121.32 +3.36
Storage 73.29 158.15 3,615 60.32 -13.3
Functions 117.75 231.97 1,928 32.74 +4.09

Top API waits (after)

API request Max wait (ms)
functions.variables.get 547.39
functions.variables.update 496.71
account.name.update 474.76
account.prefs.update 392.7
tablesdb.rows.create 374.99

HarshMN2345 and others added 2 commits September 9, 2026 10:57
API rules persist `deploymentResourceType` as '' (the attribute default),
and `Document::getAttribute()` only substitutes its default for an
absent attribute, so the fallback to `type` never fired for them once
the rule had been through the database.

Build the test collections from the canonical definitions so the schema
the worker is tested against cannot drift from production, and seed the
API case with the persisted default so the fallback is exercised on real
data.
@HarshMN2345
HarshMN2345 force-pushed the claude/deletes-preserve-recreated-domain branch from ad1305f to b667f6c Compare September 9, 2026 12:17
Comment thread .github/workflows/ci.yml Resolved
Comment thread tests/e2e/Services/Proxy/ProxyBase.php Resolved

Copy link
Copy Markdown
Member Author

Tests / E2E / Screenshots (dedicated) is red on c5bb2b2b17, but not because of this PR.

  • Failure: SitesConsoleClientTest::testSiteScreenshot — Failed asserting that 404 matches expected 200 on GET /storage/buckets/screenshots/files/{id}/view, four seconds into the job.
  • The Deletes.php change is unchanged since b667f6cbb7, where this same check passed (run 34350185516). c5bb2b2b17 adds Proxy/General tests, a test fixture, and a CI step that runs only in the Proxy job — nothing on the Sites, screenshots, or storage path. RuleDeleted has no subscriber in this repo, so the deletion guard cannot affect routing either.

No fix to port. I'll re-run the failed job once the run completes; if it fails the same way twice it gets looked at on its own, not here.


Generated by Claude Code

The worker test drives the queue and the database directly and never
builds an HTTP client, so the response-format override read by the e2e
Scope is meaningless to it. Dropping it also keeps the workflow_dispatch
input out of this step's shell command.
HarshMN2345 pushed a commit that referenced this pull request Sep 15, 2026
main dropped one of the getDB callables from Deletes::action(), leaving it at
18 parameters. DeletesTest still passed 19, so every argument from the sixth
onward landed one position early — a closure arrived where Device
$deviceForFiles was expected.

PHPStan caught it in CI as "invoked with 19 parameters, 18 required"; it was
invisible locally because this container cannot install composer.lock (the
project now needs PHP 8.5+ and the sandbox has 8.4), so the vendored
dependencies still carry the old signature.

The split PRs #13570-#13573 drive this worker through the registered worker and
the real DI container instead of a hand-written argument list, so none of them
had to change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav
Refreshes the base so CI runs against current main (PHP 8.5, the reworked
Deletes worker, the executions store). No conflicts: main did not touch
deleteRule(), and this branch's test drives the registered worker through
DI rather than calling action() positionally, so main's signature change
is absorbed.
HarshMN2345 pushed a commit that referenced this pull request Sep 17, 2026
HarshMN2345 pushed a commit that referenced this pull request Sep 17, 2026
Picks up main via #13572 -> #13571 -> #13570 so CI runs
against current main.

Conflict in Certificates.php: main added an inline notifyError() call in
catch and gave notifyError() a sixth $console parameter. This branch
defers that email to the finally block, which sends only once the lease
is confirmed still ours, so a stale worker cannot email an admin about a
failure that a newer retry has already superseded. Kept the deferred
form, dropped the inline call, and carried main's new parameter into the
deferred call site -- git would have merged the signature change without
touching the call.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav
@HarshMN2345
HarshMN2345 force-pushed the claude/deletes-preserve-recreated-domain branch from 75b6731 to 193ce56 Compare September 18, 2026 09:06
HarshMN2345 added a commit that referenced this pull request Sep 18, 2026
Picks up main via #13570 so CI runs against current main.
HarshMN2345 added a commit that referenced this pull request Sep 18, 2026
Picks up main via #13571 -> #13570 so CI runs against
current main.
HarshMN2345 added a commit that referenced this pull request Sep 18, 2026
Picks up main via #13572 -> #13571 -> #13570 so CI runs
against current main.

Conflict in Certificates.php: main added an inline notifyError() call in
catch and gave notifyError() a sixth $console parameter. This branch
defers that email to the finally block, which sends only once the lease
is confirmed still ours, so a stale worker cannot email an admin about a
failure that a newer retry has already superseded. Kept the deferred
form, dropped the inline call, and carried main's new parameter into the
deferred call site -- git would have merged the signature change without
touching the call.
The Proxy adapter matrix is where this PR runs the certificate deletion
worker tests, and it is currently failing on MariaDB and MongoDB. The
failure cannot be read back: the job log is around twenty thousand lines,
the API serves only the last five thousand, and an unbounded dump of every
container's output fills that window with runner metrics.

Same bound already applied to this step further up the stack, ported down
so every pull request below it inherits a readable log.
The certificateId lookup could never match a rule. Every rule creation site
persists an empty certificateId, the only writer of a real id is the
certificates worker updating the single rule it processed, and _key_domain is
unique, so a certificate document is 1:1 with a rule. The rule's own row is
always gone before the worker runs. That left an unindexed scan of rules on
every deletion, guarding a delete it always permitted. Restore the
unconditional cleanup: a replacement rule allocates its own certificate
document, so keeping the old one orphans a renewal that reschedules forever.

Derive the domain type the way the issuing sites do. deploymentResourceType
defaults to '' and is always persisted, so the default argument never fires
and an API rule issues under ''. Falling back on empty instead sent the delete
to 'api', a different bucket than the one the certificate was issued under.
…r test

The suite needed four classes to run. A Database subclass switched over
_APP_DB_ADAPTER by hand, which the worker's own message resources already do
from the production factory, and that switch was the only thing the dedicated
CI step existed for: phpunit already collects tests/e2e/General recursively, so
e2e_general ran the file anyway. A Listener subclass observed a RuleDeleted
dispatch that has no subscriber and sits in the same branch as the provider
call, so the recorded provider calls carry the claim. The Provider fake becomes
an anonymous class in the method that needs it.

Two cases go with the helpers: both fabricated a pair of rules sharing one
certificateId, which a unique domain index makes unreachable.

testDeleteProjectRules duplicated testCreateRuleDeletesOrphanedRule, which
already covers the same throwaway project, conflict, project delete and
recreate. Neither Proxy test can observe this fix: verifyRule() rejects a test
domain, so those rules never get a certificate at all.

Restore the unbounded failure log dump, which was a separate concern and
applied to one of the three dumps.
Comment thread tests/e2e/General/DeletesTest.php Resolved
HarshMN2345 and others added 2 commits September 18, 2026 16:10
Resolving dbForPlatform from the container pulled in the pool-backed cache,
and popping a pooled connection needs a Swoole coroutine the PHPUnit process
is not in, so every case errored with "Failed to acquire lock". Build the
database from the registry connection with no cache instead, which is what
keeps this test out of the pools entirely.

Restore the queue boundary with it. Calling the action directly skipped
message encoding and job routing, so a break in either would not have failed
the test. The publisher, broker and worker come back; the provider fake stays
an anonymous class and the listener stays gone.
Comment thread tests/e2e/General/DeletesTest.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this test, its not something we can E2E properly, mocking everything around is not really good test either.
I would trust manual QA much more here - reproduce problem on prod, then deploy this to stage, and see same flow work on stage

HarshMN2345 and others added 2 commits September 18, 2026 16:28
The race needs a real certificate for a real domain, which the e2e stack
cannot issue, so the harness had to stand in for the queue, the database and
the provider around the one call under test. Verifying this on stage against
a reproduction from prod is the more honest check.
@HarshMN2345
HarshMN2345 merged commit 6a3f978 into main Sep 18, 2026
1 check passed
HarshMN2345 added a commit that referenced this pull request Sep 18, 2026
main dropped the worker-level deletion test from #13570 before merging, and
with it the Certificates/ fixtures. This branch still needs two of them:
Database.php builds the throwaway schema every certificate test runs against,
and Provider.php is the recording certificate provider WorkerTest drives. Both
are kept; DeletesTest.php and Deletion.php belonged to the deletion test and go
with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants