Repository navigation
Fix repeated Vim copies hanging the IDE on Wayland - #171
Conversation
Every default-register copy shelled out to a fresh wl-copy subprocess on Wayland, so holding `.` to repeat a Vim delete spawned one subprocess per repeat and hung the IDE. A copy that arrives while an OS write is in flight now coalesces into a single pending slot drained by one worker, so a burst costs at most one extra subprocess and the newest payload still wins. The in-memory shadow keeps every copy synchronously, so in-process registers are unaffected. Fixes unstablebuild#170 Signed-off-by: Santhi Prakash <[email protected]>
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
The root cause behind #170 is not the repeat (although it contributes to reproducing it), is that we're performing potentially blocking I/O on the event loop goroutine. The solution therefore, is not to coalesce subsequent copies, it's to move the I/O to a separate goroutine altogether. I propose the following design. Leave the // internal/text/async_clipboard.go
package text
type asyncRegister struct {
other clipboard.Register
}
type payload struct {
registerID string, data clipboard.Data
}
func NewAsyncClipboard(other clipboard.Register) clipboard.Register {
ret := &asyncRegister{other: other}
ret.ch = make(chan payload, 1)
go ret.startWorker()
return ret
}
// etc.Note how the channel is a buffered channel of size 1. This yields some very important semantics: the publisher of the channel can replace the payload when the worker is busy, effectively implementing last copy wins semantics, and even discarding worthless work in the case the worker is slow. func (r *asyncRegister) Copy(registerID string, data clipboard.Data) error {
p := payload{registerID: registerID, data: data}
for {
// try to publish normally
select {
case r.ch <- p:
return nil
default:
}
// if the worker is busy, last copy wins; consume it
// then publish
select {
case <-r.ch:
default:
}
}
}Taking advantage of the last copy wins semantics we get to something quite elegant, easy to test, and decoupled from whether the actual system clipboard implementation. Also: •No lock is needed. Every round of the loop either publishes or frees the slot, so concurrent callers can't make Copy block on the worker. One minor drawback from this approach, although I think it's acceptable: There's only one register slot, so a copy to register Working around that in a generic wrapper means special-casing the default register. That quietly ties the wrapper to what // NewAsyncSystemClipboard wraps a system clipboard to provide asynchronous copy semantics..
func NewAsyncSystemClipboard() clipboard.Register {
sys := NewSystemClipboard()
ret := &asyncRegister{other: sys}
ret.ch = make(chan payload, 1)
go ret.startWorker()
return ret
}So tightening down the behavior:
|
Review feedback on unstablebuild#170: the root cause is not the copy rate but blocking I/O on the event loop goroutine — systemClipboard.Copy shells out to wl-copy synchronously, so even the first write stalls input handling. Leave systemClipboard untouched and add asyncRegister, a clipboard.Register wrapper that queues only the default register onto a single-slot channel drained by a worker. Publishers replace the slot when the worker is busy, so last copy wins and a burst costs at most two OS writes. Named registers are forwarded synchronously (in-memory writes, in order), and Paste answers from the newest copy while a write is queued or in flight since the OS clipboard is not authoritative then. Refs unstablebuild#170 Signed-off-by: Santhi Prakash <[email protected]>
|
You're right — verified the claim before reworking: with a stub register whose
Verification:
One behavior change worth flagging: |
ernestrc
left a comment
There was a problem hiding this comment.
LGTM but I'll wait for @zhengqunkoo's feedback and testing before merging.
|
Heads-up on the red
|
…opy-burst Signed-off-by: Santhi Prakash <[email protected]>
…opy-burst Signed-off-by: Santhi Prakash <[email protected]>
…opy-burst Signed-off-by: Santhi Prakash <[email protected]>
Apologies, was hunkered down with another issue. On testing with deleting words on a line with However, after a few minutes (typing out this comment), redoing the test repeats the warm-up behavior: it lags again for a while. Perhaps this is an issue with how Rune interacts with the wl-clipboard. Despite this unresolved issue, the IDE no longer hangs indefinitely, so this PR is a big improvement over the previous state of things. |
Problem
Holding
.to repeat a Vimdwhangs the IDE on Wayland (#170).Root cause
systemClipboard.Copyperforms OS clipboard I/O synchronously on the eventloop goroutine:
sysclip.Copy→atotto/clipboard.WriteAll→exec wl-copyWait. Each invocation is a subprocess spawn plus a Wayland selectionround-trip, and
wl-copystays resident serving the selection. A single copycan block input handling; a held
.repeat turns that into an unbroken stall.Fix
New
internal/text/async_clipboard.gomoves OS writes off the caller'sgoroutine (review feedback —
systemClipboarditself is left untouched):asyncRegisteris a privateclipboard.Registerwrapper, constructed byNewAsyncSystemClipboard(), which builds the system clipboard internally sothe wrapper can assume register semantics.
drained by a worker goroutine. When the worker is busy the slot is consumed
and replaced — last copy wins, no locks, and a burst costs at most two OS
writes: the one in flight plus the newest payload.
in order; the slot only ever holds default-register payloads, so no
cross-register discard is possible.
Pastestays synchronous, but while a write is queued or in flight the OSclipboard is not authoritative, so it returns the newest copy (tracked
atomically alongside a pending counter) instead of reading stale content.
All three clipboard construction sites (
cmd/rune,cmd/rune-agent,clipboard: "system"config) now useNewAsyncSystemClipboard().Verification
TestAsyncClipboardCopyNeverBlocksOnOSWrite(new): 51 copies across anin-flight OS write all return without blocking, exactly 2 writes reach the
OS layer, newest payload last. Sabotage-checked: the same flow deadlocks
against the previous synchronous
systemClipboard— the very firstCopyblocks the caller behind the write.
TestAsyncClipboardForwardsNamedRegistersSynchronously: named registersreach the wrapped clipboard before
Copyreturns.TestAsyncClipboardPasteReadsNewestCopyWhileWriteInFlight: paste during anin-flight write returns the newest copy without touching the OS layer; once
settled, paste reads the OS clipboard again.
go test -race ./internal/text/ ./internal/extension/extutil/— okgo test -vet=off ./...underxvfb-run -a— green exceptinternal/llm/geminiTestModelsFallsBackOnMissingClient, which needs alive API call and fails identically without this change (env has real
Gemini keys set).
go vet/gofmtclean.Notes
Copycan no longer returnthe OS write error synchronously, and
Pastestill surfaces a broken ormissing provider (e.g. the "install wl-clipboard" hint) on read.