Repository navigation
recover from corrupt ace multi-select editor state - #18424
Conversation
An exception escaping into Ace's multi-select machinery could leave a source editor permanently broken until the IDE was reloaded: mouse text selection dead, typed input silently discarded, multi-select mode impossible to exit, and editor commands landing in a previously focused tab. Ace's forEachSelection() and $moveLines() mutate selection state (temporary Selection object, silenced event registry, inVirtualSelectionMode, detached range list) and restore it with no exception protection, so a single mid-operation throw stranded that state permanently. This change fixes and hardens things on the RStudio side, leaving the bundled Ace behavior untouched so the defects can be reported upstream against stock Ace: - acesupport mixin (mixins/multi_select_guard) wraps forEachSelection and $moveLines with snapshot/restore semantics, so an aborting exception no longer strands the temporary selection, the silenced event registry, inVirtualSelectionMode, or a detached range list - AceEditorNative gains isMultiSelectStateCorrupt() and resetMultiSelectState(), extending the #13436 mouse-handler recovery to detect and repair leftover multi-select corruption; the check now also runs (deferred) when an editor regains focus - AceEditor.focus() updates the last-focused-editor tracking eagerly, so editor commands (e.g. insert pipe operator) cannot target a previously focused tab when the DOM focus event does not re-fire - applyChanges() and doSetCode() exit multi-select mode before driving command streams / replacing the document from async contexts - AceEditorNative.setHandler() callback is now $entry-wrapped, closing the one Ace-to-GWT callback that let GWT exceptions propagate raw into Ace's dispatch - removed a stray debugger; statement (shipped in both ace bundles since the #14804 null guard) that froze the IDE whenever devtools were open and the guard was reached The new Playwright spec injects a throwing document listener during a multi-cursor edit and asserts the editor survives; without the mixin it reproduces the corruption (stuck inVirtualSelectionMode, stranded temporary selection) deterministically. Addresses #13605.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
Upstream issue filed with the deterministic repro from this PR's negative control, verified against stock ace-builds 1.44.0: ajaxorg/ace#5996 |
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
Code review findingsActionable items from an automated multi-agent review. Claims below were traced against the bundled Ace source ( Critical
Important
Test gaps
Comments
Suggestions
|
- run only the multi-select corruption check from the focus path: the mouse-handler check misfires during an Alt+drag block selection, whose mousedown stops the event before the default capture handler runs (isMousePressed stays false while inVirtualSelectionMode is set) - have the corruption detector return a reason string so recovery logs say which corruption branch fired - resetMultiSelectState: restore session.$undoSelect (addRange clears it, and the bypassed $onRemoveRange is what normally restores it); log failures in the individual recovery steps; repair the selection marker count after a failed marker removal; and on a $onSingleSelect failure remove the 'exec' rerouting handler explicitly and keep the editor flagged as corrupt so the reset is retried - doSetCode/applyChanges: reset stranded corrupt state instead of calling the silently no-op exitMultiSelectMode; log when extra cursors are dropped - focus(): claim s_lastFocusedEditor only when focus actually landed, so focusing a hidden or detached editor can't steal command routing - multi_select_guard: describe each wrapped function's behavior separately, name the Ace internals the wrappers depend on, and log which stranded state a finally block repaired - e2e: prove fault injection with a window sentinel instead of exception routing; assert exact cursor and insertion counts; add coverage for the $moveLines guard, a healthy multi-cursor selection surviving tab switches, a detached-range-list corruption shape, and focus-path repair without a tab switch
|
Applied in d34fbb7 -- item-by-item, numbered as in the review: 1: Confirmed the mid-drag reset (e.stop() -> captureMouse skipped -> focusEditor -> deferred check), fixed by keeping the mouse-handler check on the activation path only. One correction: the selection-collapse mechanism doesn't hold -- 2: Confirmed; the reset now restores 3: Confirmed; 4: Ran the spec as requested -- it passes as written: the injected throw is recorded in 5: Applied as suggested; verified 6: The detector now returns a reason string, logged on recovery. Kept 7: Confirmed; on failure the reset removes the exec rerouting handler explicitly, logs, and re-sets 8: Confirmed; inner catches now log with step context, outer catch is annotated as backstop. 9-13: All applied -- healthy 3-cursor selection survives tab switch (typing lands 3x), 14-20: All comment fixes applied (per-function mixin header, named Ace-internal dependencies, javadoc pointer to the guard plus the unwrapped 21: Added a 22: Confirmed; failures logged and the marker count repaired after a failed removal. Detector left unchanged -- flagging cosmetic marker residue would trigger full resets. 23-24: Guarded 25: Leaving as-is -- the |
Pin the everyday multi-select behaviors that the multi_select_guard wrappers must leave intact -- typing at keyboard-added cursors, single-step undo of a multi-cursor edit, splitIntoLines, and moving lines with multiple cursors -- driven through the real keyboard shortcuts. Addresses #13605.
Addresses #13605.
The problem
An exception escaping into Ace's multi-select machinery permanently corrupts a source editor tab, producing the long-reported symptom cluster from #13605 (and the related #13436): mouse text selection stops working, typed input is silently discarded, multi-select mode cannot be exited, and editor commands (e.g. Ctrl+Shift+M) can land in a previously focused tab. Only reloading the IDE recovers.
Root cause: Ace's
forEachSelection()and$moveLines()temporarily mutate selection state -- swapping in a throwawaySelection, silencing the real selection's_eventRegistry, settinginVirtualSelectionMode, detaching the range list -- and restore it only on the way out, with no exception protection. One mid-operation throw (from any document/selection listener) strands all of it. The stranded state is silent: with the listeners gone, nothing throws afterward; the editor just stops responding.The fix (RStudio-side only; bundled Ace untouched)
Ace behavior is deliberately left as-is so the defects can be filed upstream against stock Ace with the deterministic repro developed here (a follow-up task).
acesupport/acemixins/multi_select_guard.js(the load-bearing piece): wrapsEditor.forEachSelection/Editor.$moveLineswith snapshot/restore semantics. The wrappers know nothing about what the wrapped code does in between, so they should survive Ace updates. Exceptions still propagate; only cleanup becomes unconditional.AceEditorNative.isMultiSelectStateCorrupt()/resetMultiSelectState(): extends the mouse cannot select any text in source editor #13436 mouse-handler recovery to detect and repair leftover multi-select corruption (stranded temporary selection, editor/selection mode-flag disagreement, empty or desynced range bookkeeping, detached range list). Runs on tab activation and now also (deferred) on editor focus, with aDebug.logline so dailies show which corruption fires in the wild.AceEditor.focus()eagerly updatess_lastFocusedEditor, soEXECUTION_POLICY_FOCUSEDcommand routing cannot go stale when the DOM focus event doesn't re-fire (the "shortcuts appear in another tab" symptom).applyChanges()/doSetCode()exit multi-select mode first; these run from async contexts (e.g. visual-mode canonical save) and previously drove Ace command streams through multi-select handling mid-corruption-window.AceEditorNative.setHandler()is now$entry-wrapped -- it was the one Ace-to-GWT callback that let GWT exceptions propagate raw into Ace's synchronous dispatch.debugger;statement that has shipped in both ace bundles since the add null check in multiselect change handler #14804 null guard (June 2024); it froze the IDE whenever devtools were open and the guard line was reached -- exactly the situation when we ask reporters to debug. Note: the corresponding patch in therstudio/rstudio-acefork branch (rstudio/globemaster-allium) should also be amended, or the nextsync-ace-commitswill resurrect it.Reviewed but intentionally not changed:
ShortcutManager'sdisableCount_(a stuck counter kills app-wide shortcuts). TheModalDialogBaseopen/close pairing is already idempotent and correctly ordered; a heuristic reset risks enabling shortcuts during a modal, so it stays as-is.Testing
New Playwright spec
multiselect_recovery.test.ts:changelistener, types with 3 cursors active, and asserts the abort was real (marker exception recorded, partial per-range application) and that the editor survives (no stranded temporary selection,inVirtualSelectionModeclear, Escape exits multi-select, typing still lands).Negative control (validated locally, not committed): with only the mixin's
requirecommented out, test 1 deterministically reproduces the #13605 corruption --inVirtualSelectionModestucktrue, temporary selection (selection.index === 2) still installed -- and fails. This doubles as the upstream repro recipe.Companion spec
multiselect_basic.test.tspins the everyday multi-select behaviors the guard wrappers must leave intact -- typing at keyboard-added cursors, single-step undo of a multi-cursor edit,splitIntoLines, and moving lines with multiple cursors -- driven through the real keyboard shortcuts (so Ctrl+Alt+Down dispatches theaddCursorBelowAppCommand, also exercising theEXECUTION_POLICY_FOCUSEDrouting).Also run locally:
ant test(572 unit tests OK; 608 testpage assertions pass, exercising the modified acesupport),ant draft, and e2e typecheck.Note for reviewers:
ant acesupportalone does not update what the dev shim serves --acesupport.jsis inlined into the compiled GWT output as aClientBundleresource, so acesupport changes needant draftto be visible.