Fix data races in preeditor access across D-Bus/worker threads#605
Open
Narcolepsyy wants to merge 1 commit into
Open
Fix data races in preeditor access across D-Bus/worker threads#605Narcolepsyy wants to merge 1 commit into
Narcolepsyy wants to merge 1 commit into
Conversation
IBus dispatches each D-Bus method on its own goroutine (go conn.handleCall), so Reset/FocusIn/FocusOut run concurrently with the keyPressChan worker goroutine. Both mutate the shared bamboo preeditor, producing data races. - Guard preeditor mutations in FocusOut, Reset, bsProcessKeyEvent and keyPressHandler with the engine mutex. - resetBuffer: move the getRawKeyLen() empty-buffer guard inside the lock in both branches. It previously read the preeditor unlocked on the FocusIn -> checkWmClass -> resetBuffer path, racing the worker (caught by TestConcurrentResetAndKeyPress under -race). - Reset: drain keyPressChan with a non-blocking select instead of len()>0 + <-. Other goroutines also receive from the channel, so the blocking form could hang the D-Bus thread on an empty channel. - SendBackSpace: clamp n to [1, maxBackSpacePerCycle] and reset the fake-backspace counter after each cycle so a dropped synthetic backspace can't leave a permanent offset. - Add tests covering Reset/FocusIn/FocusOut buffer semantics per input mode, getOffsetRunes, and a -race concurrency stress test.
ec60076 to
2ed93ff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
IBus dispatches each incoming D-Bus method on its own goroutine (
go conn.handleCallin godbus). That meansReset,FocusIn, andFocusOutrun concurrently with thekeyPressChanworker goroutine (keyPressCapturing). Both paths mutate the sharedbamboopreeditor, which is not safe for concurrent use — a data race that can corrupt the buffer and produce wrong output (e.g.oof→ôfinstead ofồ, stale words after focus changes).Changes
FocusOut,Reset,bsProcessKeyEvent, andkeyPressHandler.resetBuffer: move thegetRawKeyLen()empty-buffer guard inside the lock in both branches. It previously read the preeditor unlocked on theFocusIn → checkWmClass → resetBufferpath, racing the worker goroutine.Reset: drainkeyPressChanwith a non-blockingselect/defaultloop instead oflen()>0+<-. Other goroutines also receive from the channel, so the blocking form could hang the D-Bus thread on an empty channel (TOCTOU).SendBackSpace: clampnto[1, maxBackSpacePerCycle]and reset the fake-backspace counter after each send cycle, so a dropped synthetic backspace can't leave a permanent offset.Reset/FocusOutinPreeditIMonly; backspace modes preserve the buffer (apps callResetaggressively mid-word).Testing
TestConcurrentResetAndKeyPressstresses Reset/FocusIn/FocusOut against the worker goroutine under-race.keyPressChandrain,getOffsetRunes, and fake-backspace reset.-race.