detached 744685d

fix(extensions): close ext.commands / ext.tools / lastFrameTime races

CI's `go test -race` flagged two races introduced over the recent
extension work:

1. ext.commands / ext.tools were appended to from the read-loop
   goroutine without a lock, while Commands() / Tools() / HasCommand
   read them under m.mu. Same for the toolIndex pre-check. Fix:
   take m.mu around the appends and the index dedup so writers and
   readers serialise on the same lock.

2. assumeReadyAfterIdle read ext.lastFrameTime once on entry without
   the per-extension lock (the read inside the loop already had it).
   Fix: take ext.mu for the initial snapshot too.

Verified locally with `go test -race ./...`; all packages pass.
The corresponding CI run for the scratchpad-persistence commit
failed for exactly these two races on linux + macos.
commit
744685d68da4f7d535ea976391b4efa9db92a774
author
patriceckhart · · 2026-04-19 13:27 UTC
parent
463c62d
signature
unsigned
refs
tag: v0.0.26
1 file · +8 -2
M internal/agent/extensions/manager.go +8 -2
@@ -448,7 +448,9 @@ func (m *Manager) spawn(ctx context.Context, ext *Extension) error {
448448 const readyIdleWindow = 250 * time.Millisecond
449449
450450 func (m *Manager) assumeReadyAfterIdle(ext *Extension) {
451+ ext.mu.Lock()
451452 last := ext.lastFrameTime
453+ ext.mu.Unlock()
452454 for {
453455 select {
454456 case <-ext.readyCh:
@@ -507,8 +509,12 @@ func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
507509 case "register_command":
508510 var rc extproto.RegisterCommandFromExt
509511 if err := json.Unmarshal(line, &rc); err == nil {
510- ext.commands = append(ext.commands, rc)
512+ // Both ext.commands and m.commandIndex are read by
513+ // the public Commands() / HasCommand() helpers under
514+ // m.mu, so the writes have to take the same lock to
515+ // keep the race detector happy.
511516 m.mu.Lock()
517+ ext.commands = append(ext.commands, rc)
512518 if _, exists := m.commandIndex[rc.Name]; !exists {
513519 m.commandIndex[rc.Name] = ext
514520 }
@@ -529,8 +535,8 @@ func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
529535 continue
530536 }
531537 }
532- ext.tools = append(ext.tools, rt)
533538 m.mu.Lock()
539+ ext.tools = append(ext.tools, rt)
534540 if _, exists := m.toolIndex[rt.Name]; !exists {
535541 m.toolIndex[rt.Name] = ext
536542 }
@@ -448,7 +448,9 @@ func (m *Manager) spawn(ctx context.Context, ext *Extension) error {
448 const readyIdleWindow = 250 * time.Millisecond448 const readyIdleWindow = 250 * time.Millisecond
449 449
450 func (m *Manager) assumeReadyAfterIdle(ext *Extension) {450 func (m *Manager) assumeReadyAfterIdle(ext *Extension) {
451+ ext.mu.Lock()
451 last := ext.lastFrameTime452 last := ext.lastFrameTime
453+ ext.mu.Unlock()
452 for {454 for {
453 select {455 select {
454 case <-ext.readyCh:456 case <-ext.readyCh:
@@ -507,8 +509,12 @@ func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
507 case "register_command":509 case "register_command":
508 var rc extproto.RegisterCommandFromExt510 var rc extproto.RegisterCommandFromExt
509 if err := json.Unmarshal(line, &rc); err == nil {511 if err := json.Unmarshal(line, &rc); err == nil {
510- ext.commands = append(ext.commands, rc)512+ // Both ext.commands and m.commandIndex are read by
513+ // the public Commands() / HasCommand() helpers under
514+ // m.mu, so the writes have to take the same lock to
515+ // keep the race detector happy.
511 m.mu.Lock()516 m.mu.Lock()
517+ ext.commands = append(ext.commands, rc)
512 if _, exists := m.commandIndex[rc.Name]; !exists {518 if _, exists := m.commandIndex[rc.Name]; !exists {
513 m.commandIndex[rc.Name] = ext519 m.commandIndex[rc.Name] = ext
514 }520 }
@@ -529,8 +535,8 @@ func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
529 continue535 continue
530 }536 }
531 }537 }
532- ext.tools = append(ext.tools, rt)
533 m.mu.Lock()538 m.mu.Lock()
539+ ext.tools = append(ext.tools, rt)
534 if _, exists := m.toolIndex[rt.Name]; !exists {540 if _, exists := m.toolIndex[rt.Name]; !exists {
535 m.toolIndex[rt.Name] = ext541 m.toolIndex[rt.Name] = ext
536 }542 }

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 1

Tags 476

↑↓ moveenter openesc close