Skip to content

fix(segments): stop a failing word segment window from killing the IME - #2234

Merged
WhiredPlanck merged 2 commits into
osfans:developfrom
WhiredPlanck:segment-crash-fix
Nov 17, 2026
Merged

WhiredPlanck merged 2 commits into
osfans:developfrom
WhiredPlanck:segment-crash-fix

Conversation

@WhiredPlanck

Copy link
Copy Markdown
Collaborator

Pull request

Issue tracker

Fixes #2230

Feature

Problem. Using the clipboard word segment window makes the keyboard disappear on some apps (Firefox web search, MT Manager) and some ROMs (reported on realme/ColorOS). The keyboard does not come back until the IME is selected again.

Mechanism. SegmentsWindow.onAttached() ran the ICU word break iterator synchronously, unprotected, while BoardWindowManager.attachWindow() was in the middle of a switch. Anything the tokenizer threw — or anything a window's onCreateView() / onAttached() threw — escaped to the main thread, where the release build's uncaught handler exits the process. Android does not restart an input method after that, so the user sees the keyboard vanish rather than a crash dialog. The same failure also left the window manager pointing at a window whose view had never been added, so even a caught failure would have meant an empty input area.

What changed (4 files, +124/−26):

File Change
app/src/main/java/com/osfans/trime/util/NativeTokenizer.kt Validates the boundaries ICU reports (a boundary that does not advance would otherwise spin forever) and falls back to splitting on whitespace if ICU fails, so the window still has segments to offer.
app/src/main/java/com/osfans/trime/ime/window/BoardWindowManager.kt Creating and attaching a window is treated as a transaction: a failing onCreateView() leaves the current window in place, a failing onAttached() removes the half-inserted view and puts the keyboard back. currentWindow/currentView are assigned before onAttached() runs.
app/src/main/java/com/osfans/trime/ime/segments/SegmentsWindow.kt Tokenization moved to a worker thread and dropped if the window is gone by then; the composing preview is debounced by 150 ms and skipped when the joined text is unchanged, instead of one InputConnection round trip per touch move while dragging.
app/src/main/java/com/osfans/trime/ime/segments/SegmentsAdapter.kt Whitespace between segments is matched with Char.isWhitespace() instead of four explicit characters.

This does not fix the vendor ICU bug itself — that is not reproducible off the affected ROMs, and the log in #2230 is an ordinary logcat snapshot without a stack. What it does is turn a process death into a degraded but usable state: a failed tokenize still shows whitespace-split segments, and a failed window attach returns to the keyboard.

Code of conduct

Code style

Build pass

  • make debug

Manually test

  • Done

Code Review

  1. No wildcards import
  2. Manual build and test pass
  3. GitHub Action CI pass
  4. At least one contributor review and approve
  5. Merged clean without conflicts
  6. PR will be merged by rebase upstream base

Daily build

Login and download artifact at https://github.com/osfans/trime/actions

Additional Info

Verified on an API 35 x86_64 emulator with the debug package:

  1. Normal path: clipboard item → long press → Word segment → segments shown; tapping a segment writes a composing preview into the focused editor; copy returns to the keyboard and updates the clipboard; share opens the system chooser.
  2. Injected tokenizer failure: the window falls back to whitespace-split segments, logs Failed to tokenize N characters, splitting on whitespace, and the process keeps its PID.
  3. Injected onAttached() failure: Failed to attach …SegmentsWindow is followed immediately by Attach …KeyboardWindow, the keyboard comes back, and the PID is unchanged.
  4. Long-press drag across a long clip: one preview update per drag instead of one per touch move.

Notes:

  • The fallback splits on whitespace only, so a CJK clip with no spaces yields a single segment. That is intentional — a worse segmentation is better than no keyboard.
  • onTrimMemory-style retry is not involved: a window that fails to attach is retried on the next user action, not automatically.
  • The included SegmentsAdapter change is a correctness fix found while reading the surrounding code; it does not affect the crash.

Copilot AI left a comment

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.

🟡 Changes recommended

Approval is blocked by one critical token-offset bug and three additional retry, crash-recovery, and whitespace-preservation issues that remain unresolved.

4 open issues
What changed in this PR

This PR hardens the clipboard word-segmentation window so tokenizer or window-attachment failures no longer terminate the IME, while moving tokenization off the UI thread and reducing redundant composing-preview updates.

Changes:

  • Adds ICU boundary validation and whitespace fallback tokenization.
  • Makes window creation and attachment recoverable, restoring the keyboard after failures.
  • Adds asynchronous tokenization, debounced previews, and broader whitespace handling.
File Description
app/​src/​main/​java/​com/​osfans/​trime/​util/​NativeTokenizer.kt Adds ICU boundary validation, failure handling, and fallback segmentation.
app/​src/​main/​java/​com/​osfans/​trime/​ime/​window/​BoardWindowManager.kt Makes window creation and attachment recoverable with keyboard restoration after failures.
app/​src/​main/​java/​com/​osfans/​trime/​ime/​segments/​SegmentsWindow.kt Moves tokenization off the main thread and debounces composing-preview updates.
app/​src/​main/​java/​com/​osfans/​trime/​ime/​segments/​SegmentsAdapter.kt Uses Unicode-aware whitespace detection for segment separators.

Review effort: Lite

The segment window ran the ICU word break iterator and let anything it
threw escape into the window manager, which was in the middle of a switch.
A vendor ICU build that fails while walking a text, or a window whose attach
throws, ended the process there; Android does not restart an input method
after that, so the keyboard simply disappeared.

Tokenization now checks that ICU reported a boundary inside the text and
falls back to splitting on whitespace, so the window still offers something
to select. The window manager treats a failing attach as a failed switch:
it removes the half inserted view and puts the keyboard back.
Building the segments ran on the main thread while the window was being
attached, and every selection change wrote the joined text straight into the
target app's InputConnection. Dragging across a long clip produced one round
trip per touch move, on apps whose InputConnection is fragile to begin with.

Tokenization now runs on a worker thread and is dropped if the window is
gone by the time it finishes. The preview is debounced by 150 ms and skipped
when the joined text did not change. Whitespace between segments is matched
by character class instead of four explicit characters, which broke the
separators for the rest.
@WhiredPlanck
WhiredPlanck merged commit b1c2412 into osfans:develop Nov 17, 2026
5 checks passed
@WhiredPlanck
WhiredPlanck deleted the segment-crash-fix branch November 17, 2026 16:37
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.

剪贴板分词功能偶尔触发键盘消失

3 participants