Repository navigation
perf(editor): delete selection without pre-read - #13479
yuxi-liu-wired wants to merge 2 commits into
Conversation
get-render-snapshots failed its whole batch when one resource named a block the worker no longer has, and the page fell to its error screen. A delete op can reach the worker before a snapshot batch that the renderer queued earlier, so a resource whose owner uuid (the key's second element, the owner the renderer's delta handling reads) names no entity now renders as nil and watches that entity, as view-data and block-breadcrumb already do for missing targets.
Deleting selected blocks read them from the worker first, only to keep the top-level blocks and to skip a selection of recycle roots. The op now carries the selected ids with a :selection option and the worker resolves it inside :delete-blocks into the ops the window used to send, so the recorded ops are unchanged. The editor moves to the block above when the op is sent instead of when the worker replies; the deleted rows still leave when the delta arrives.
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| (catch :default error | ||
| (if-let [owner (deleted-owner-uuid db resource-key)] | ||
| [#{[:entity owner]} nil] | ||
| (throw error))))) |
There was a problem hiding this comment.
🟡 Malformed resource requests silently succeed
When an owner UUID is absent, render-resource turns malformed resource parameters into a successful nil snapshot. Invalid requests no longer fail validation, hiding caller errors.
Learn more
Resource renderers validate more than owner existence. For example, block-display-properties validates its display context after resolving the owner. If the owner is missing and the context is malformed, owner lookup throws first. The broad catch converts that failure to nil and skips validation. The same catch also converts any other renderer failure for a missing owner into a successful snapshot.
Example: Request [:block-display-properties missing-uuid {:page-title? true}]. The owner is absent and the context lacks required flags. The response contains a ready nil resource instead of reporting the invalid context.
Recommended fix: Validate resource parameters independently of owner existence before applying the missing-owner fallback. Limit recovery to the specific missing-owner exception, rather than catching every renderer error for a missing UUID.
Was this helpful? React with 👍 or 👎 to provide feedback.
What changes
Deleting selected blocks (Backspace or Delete on a selection, and the delete step of Cut) went through
cut-selection-blocksinsrc/main/frontend/handler/editor.cljs. It read the selected blocks from the worker (db-async/<get-blocks) only to keep the top-level blocks of the selection and to skip a selection made only of recycle roots, thendelete-blocks!sent:delete-blocks. The editor moved to the block above when the worker replied (run-response-editor-callback!insrc/main/frontend/db/transact.cljs), although the reply of a delete carries no editor rows (operation-row-uuidsreturns none for:delete-blocks). A key pressed before the reply found no editor and was lost.The delete is sent without a read. The window sends the uuids of the selected rows in selection order, with the facts it reads from the DOM as before:
[:delete-blocks [uuids {:selection {:original-ids {...}}}]].:original-idsmaps a rendered linked block to its linking block (originalblockid, the same lookupblock-handler/get-top-level-blocksuses), and on mobile:delete-journals? trueis added.logseq.outliner.op/apply-ops!resolves an op with:selectionagainst the db into the ops the window used to send:outliner-core/blocks-with-level;:logseq.property/deleted-atcounts: the:block/parentof a<get-blocksrow is a ref summary without that attribute, soldb/recycled?never saw a recycled parent there;:delete-pageops.The recorded outliner ops, which undo and sync read, are the ones the window sent before (unit tests below compare them). Page nodes are still deleted as nodes (db-test#1242). Callers of
:delete-blockswithout:selectionare unchanged.The editor moves when the op is sent.
delete-blocks!marks its editor callback with:editor/edit-block-on-send?, andapply-outliner-opsschedules it when it queues the request instead of running it after the reply; the reply then only applies its delta. The target is found in the DOM as before (edit-previous-window-block-fnon the row above the first selected row). The window changes no data: the deleted rows leave when the worker's delta arrives, as before.cut-selection-blocksno longer waits for the reply, socut-blocks-and-clear-selections!clears the selection right after the send.The callback runs at the next animation frame (
on-next-frame!, which the file already has), after the key event is fully dispatched. Two other timings failed on the way:KeyboardShortcutHandlers onwindow, and the editing-only one (:editor/backspace,:editor/delete) receives the key after the one that handles the selection, so it also deleted the last character of the block above.setTimeout0 task, Chromium dispatched a key pressed 10-35 ms after the Backspace before the timer task (a renderer trace shows the delete's keydown task, a frame, the next keydowns, then the timer), and that key found no editor: Shift+ArrowUp landed in 15 of 30 tries on the desktop app.The worker renders the resources of a deleted block as nil (a separate commit, first on the branch).
:thread-api/get-render-snapshotsfailed its whole batch when one resource named a block the worker no longer had, and the page fell to the "Something went wrong" screen. A delete op can reach the worker before a snapshot batch the renderer queued earlier, since the loader sends one batch at a time. Master has the same race in its code; I have not seen it fail there (240 runs of the steps below), and this branch sends the op sooner and keeps the page working while the delete is in flight. In 60 runs of thedeletesteps on the e2e build, with the unit suite running beside them, 2 ended on that screen after:block-display-propertiesfailed for the block just deleted ("Missing renderer resource entity").render-snapshotsnow catches a resource failure whose owner uuid (the key's second element, the ownerfrontend.db.subs/apply-delta-storereads) names no entity, and returns a nil value watching[:entity owner], asmissing-view-dataandblock-breadcrumbalready do for their missing targets; every window consumer of these resources treats nil as not loaded. A failing resource of a live block still fails.block-comment-summary-resource-rejects-invalid-uuid-and-thread-testexpected a random uuid to fail; that case moved to the new test.When the worker refuses the delete (a built-in node; the worker broadcasts its notification as before) or fails, the rows stay and the editor stays on the block above; the error is logged as
:editor/delete-selection-failed. Before this change the editor did not move and the selection stayed. The editor is not moved back, since a key typed into it after the delete would be lost again. A selection of recycle roots, or of blocks the worker no longer has, now also leaves the editor on the block above, where before nothing happened.:ui/page-idis no longer computed for this op:apply-outliner-opsremoves it before the worker call and no code reads it.The desktop app, release compiles of the renderer, the db worker and the Electron main from 1 commit each (master
16c4ed1a0and this branch), a new graph without sync, a page of 4 blocks. Each delete selects the last block with Escape and presses Backspace, asdelete-testdoes; each key waits until no worker request has been in flight for 400 ms. Median (range), sessions in the order master, branch, branch, master, 30 deletes each:A key pressed right after the Backspace, as fast as the test driver sends it, 30 times each, 1 session per build (a second master session gave the same counts):
x: it lands in the editor of the block above in 0 of 30 on master (pressed 5.6 - 21.7 ms after the Backspace), 30 of 30 here (7.6 - 30.7 ms);Those branch sessions ran the delete change alone. The branch with both commits, 1 more session of each kind: 13.8 ms (7.5 - 51.2) to the editor, and the letter and Shift+ArrowUp landed 30 of 30 each. No branch session logged a console error.
The e2e web build (
clj-e2edefines, release compile with--debug), master16c4ed1a0against this branch, 3 runs side by side on this machine:outliner-basic-test'sdeletesteps as master has them (Shift+ArrowUp right after the Backspace, without a wait for the editor), repeated 20 times per run on a fresh page: master lost the first Shift+ArrowUp in 8 of 60 iterations (b3's editor mounted after that key), this branch in 0 of 180 (run with the whole unit suite beside it). Backspace to b3's editor mounted, median (p90, max): master 72 ms (330, 504), this branch 20 ms (76, 138). Both builds also end some iterations with no editor (master 3 of 60, this branch 7 of 180), each after the test's second Escape, the race test(e2e): wait out editor races in delete-test #13474 fixes in the test. No iteration ended on the error screen.outliner-basic-test: master 3 of 5 runs pass (failures inconsecutive-backspace-does-not-restore-deleted-blocks-test,backspace-empty-first-child-keeps-empty-parent-subtree-test,delete-end-test,enter-then-tab-on-collapsed-block-expands-it-test), this branch 4 of 4.editor-basic-test: master 0 of 2 runs pass (move-blocks-cmdk,page-ref-navigate-persists-edit-buffer-with-open-popup-test,move-blocks-mod+shift+m), this branch 2 of 2.Tests
logseq.outliner.op-test(deps/outliner/test): the worker keeps the top-level blocks in selection order and records[:delete-blocks [[a11 a b] {}]]for a selection[a11 a a1 b]; a selection of a recycle root resolves to no op, while a block under a recycle root and a recycle root beside a live block are deleted; a rendered linked block deletes its linking block and keeps the target; with:delete-journals?a journal page becomes a:delete-pageop and goes to the recycle bin. On master these 4 tests fail (8 failures).apply-outliner-ops-runs-an-edit-on-send-callback-when-the-op-is-sent-testandapply-outliner-ops-keeps-an-edit-on-send-when-the-worker-fails-testinfrontend.db.transact-test: the request is out and the editor has not moved within the task that sent it; the callback runs at the next animation frame, once, before the reply; it still runs when the worker fails, and the failure reaches the caller. On master they fail (4 failures). A check of the same kind failed against the microtask version.delete-selection-sends-the-selection-and-focuses-the-previous-block-at-sendinfrontend.handler.editor-async-testreplaces the test that asserted the editor moved only from the reply: no worker read before the op, the op carries the selected ids, the editor moves before the reply and not again after it. On master it fails (3 failures).cut-selection-blocks-uses-inner-text-property-value-testinfrontend.handler.editor-testnow checks that Cut sends the inner property-value block without a read (2 failures on master).resources-of-a-deleted-block-do-not-fail-their-snapshot-batch-testinfrontend.worker.handler.render-resource-test: a batch with the display properties and comment summary of a deleted block beside the display properties of a live one renders the first 2 as nil and the third as before; a bad request for a live block still throws. On master it errors.delete-selected-child-page-unlinks-namespace-and-keeps-content,delete-selected-block-still-removes-its-children) pass through the worker path.TZ=Etc/GMT+12(2518 tests, 12547 assertions,logseq.outliner.op-testincluded) and its time-zone-dependent namespaces withTZ=Pacific/Kiritimati(1483 tests, 9100 assertions): 0 failures, 0 errors.Allow edits from maintainers.