detached 47070ab

fix: harden async callbacks with URI check and timer generation counter

Add URI comparison in favicon GetOrFetch callback to prevent a stale
fetch from overwriting a newer navigation's favicon.

Add generation counter to ModalState so stale timer callbacks (from
time.Timer.Stop races) are ignored in startTimeoutLocked.
commit
47070ab664e00df9d2bc6b68c63ab3ddfab152cb
author
brice · · 2026-03-13 07:15 UTC
parent
ff5eb4f
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +22 -4
M internal/ui/coordinator/content.go +5 -0
@@ -1090,6 +1090,7 @@ func (c *ContentCoordinator) resolveCommittedFavicon(ctx context.Context, paneID
10901090 // Fall back to GetOrFetch (checks service cache, WebKit DB, then DuckDuckGo API)
10911091 // Capture current generation to guard against stale callbacks
10921092 gen := wv.Generation()
1093+ capturedURI := uri
10931094 c.faviconAdapter.GetOrFetch(ctx, uri, func(texture *gdk.Texture) {
10941095 // Skip nil results — a nil means "couldn't resolve", not "no favicon".
10951096 // Without this guard, a late nil callback can overwrite a good favicon
@@ -1105,6 +1106,10 @@ func (c *ContentCoordinator) resolveCommittedFavicon(ctx context.Context, paneID
11051106 if currentWV != wv {
11061107 return
11071108 }
1109+ // Verify the WebView hasn't navigated away since we started the fetch
1110+ if currentWV.URI() != capturedURI {
1111+ return
1112+ }
11081113 c.updateStackedFaviconForPane(ctx, paneID, texture)
11091114 })
11101115 }
@@ -1090,6 +1090,7 @@ func (c *ContentCoordinator) resolveCommittedFavicon(ctx context.Context, paneID
1090 // Fall back to GetOrFetch (checks service cache, WebKit DB, then DuckDuckGo API)1090 // Fall back to GetOrFetch (checks service cache, WebKit DB, then DuckDuckGo API)
1091 // Capture current generation to guard against stale callbacks1091 // Capture current generation to guard against stale callbacks
1092 gen := wv.Generation()1092 gen := wv.Generation()
1093+ capturedURI := uri
1093 c.faviconAdapter.GetOrFetch(ctx, uri, func(texture *gdk.Texture) {1094 c.faviconAdapter.GetOrFetch(ctx, uri, func(texture *gdk.Texture) {
1094 // Skip nil results — a nil means "couldn't resolve", not "no favicon".1095 // Skip nil results — a nil means "couldn't resolve", not "no favicon".
1095 // Without this guard, a late nil callback can overwrite a good favicon1096 // Without this guard, a late nil callback can overwrite a good favicon
@@ -1105,6 +1106,10 @@ func (c *ContentCoordinator) resolveCommittedFavicon(ctx context.Context, paneID
1105 if currentWV != wv {1106 if currentWV != wv {
1106 return1107 return
1107 }1108 }
1109+ // Verify the WebView hasn't navigated away since we started the fetch
1110+ if currentWV.URI() != capturedURI {
1111+ return
1112+ }
1108 c.updateStackedFaviconForPane(ctx, paneID, texture)1113 c.updateStackedFaviconForPane(ctx, paneID, texture)
1109 })1114 })
1110 }1115 }
M internal/ui/input/modal.go +17 -4
@@ -60,9 +60,10 @@ func (m Mode) DisplayName() string {
6060
6161 // ModalState manages the current input mode with optional timeout.
6262 type ModalState struct {
63- mode Mode
64- timeout time.Duration
65- timer *time.Timer
63+ mode Mode
64+ timeout time.Duration
65+ timer *time.Timer
66+ timerGen int64 // incremented each time a timer is started; used to ignore stale callbacks
6667
6768 // Callback for mode changes (called synchronously under lock).
6869 onModeChange func(from, to Mode)
@@ -215,9 +216,21 @@ func (m *ModalState) cancelTimerLocked() {
215216 // because onModeChange may make GTK calls (e.g., switching controller phase).
216217 // Must be called with m.mu held.
217218 func (m *ModalState) startTimeoutLocked(ctx context.Context, timeout time.Duration) {
219+ m.timerGen++
220+ gen := m.timerGen
218221 schedule := m.scheduleOnMainThread
219222 m.timer = time.AfterFunc(timeout, func() {
220- schedule(func() { m.ExitMode(ctx) })
223+ schedule(func() {
224+ // Ignore stale callback: time.Timer.Stop can race with the
225+ // timer firing, so a canceled timer's callback may still run.
226+ m.mu.RLock()
227+ stale := m.timerGen != gen
228+ m.mu.RUnlock()
229+ if stale {
230+ return
231+ }
232+ m.ExitMode(ctx)
233+ })
221234 })
222235 }
223236
@@ -60,9 +60,10 @@ func (m Mode) DisplayName() string {
60 60
61 // ModalState manages the current input mode with optional timeout.61 // ModalState manages the current input mode with optional timeout.
62 type ModalState struct {62 type ModalState struct {
63- mode Mode63+ mode Mode
64- timeout time.Duration64+ timeout time.Duration
65- timer *time.Timer65+ timer *time.Timer
66+ timerGen int64 // incremented each time a timer is started; used to ignore stale callbacks
66 67
67 // Callback for mode changes (called synchronously under lock).68 // Callback for mode changes (called synchronously under lock).
68 onModeChange func(from, to Mode)69 onModeChange func(from, to Mode)
@@ -215,9 +216,21 @@ func (m *ModalState) cancelTimerLocked() {
215 // because onModeChange may make GTK calls (e.g., switching controller phase).216 // because onModeChange may make GTK calls (e.g., switching controller phase).
216 // Must be called with m.mu held.217 // Must be called with m.mu held.
217 func (m *ModalState) startTimeoutLocked(ctx context.Context, timeout time.Duration) {218 func (m *ModalState) startTimeoutLocked(ctx context.Context, timeout time.Duration) {
219+ m.timerGen++
220+ gen := m.timerGen
218 schedule := m.scheduleOnMainThread221 schedule := m.scheduleOnMainThread
219 m.timer = time.AfterFunc(timeout, func() {222 m.timer = time.AfterFunc(timeout, func() {
220- schedule(func() { m.ExitMode(ctx) })223+ schedule(func() {
224+ // Ignore stale callback: time.Timer.Stop can race with the
225+ // timer firing, so a canceled timer's callback may still run.
226+ m.mu.RLock()
227+ stale := m.timerGen != gen
228+ m.mu.RUnlock()
229+ if stale {
230+ return
231+ }
232+ m.ExitMode(ctx)
233+ })
221 })234 })
222 }235 }
223 236

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close