Repository navigation
fix(chat): prune dead session programs and synchronize registry access (#3201) #4384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| ) | ||
|
|
||
| // fakeProg records every message sent to it. It satisfies sender without | ||
| // running a real terminal program. | ||
| type fakeProg struct { | ||
| mu sync.Mutex | ||
| msgs []tea.Msg | ||
| } | ||
|
|
||
| func (f *fakeProg) Send(msg tea.Msg) { | ||
| f.mu.Lock() | ||
| defer f.mu.Unlock() | ||
| f.msgs = append(f.msgs, msg) | ||
| } | ||
|
|
||
| func (f *fakeProg) count() int { | ||
| f.mu.Lock() | ||
| defer f.mu.Unlock() | ||
| return len(f.msgs) | ||
| } | ||
|
|
||
| func waitFor(t *testing.T, what string, cond func() bool) { | ||
| t.Helper() | ||
| for i := 0; i < 200; i++ { | ||
| if cond() { | ||
| return | ||
| } | ||
| time.Sleep(5 * time.Millisecond) | ||
| } | ||
| t.Fatalf("timed out waiting for %s", what) | ||
| } | ||
|
|
||
| func TestSendReachesOnlyLivePrograms(t *testing.T) { | ||
| a := &app{} | ||
| live, dead := &fakeProg{}, &fakeProg{} | ||
|
|
||
| a.mu.Lock() | ||
| a.progs = append(a.progs, live, dead) | ||
| a.mu.Unlock() | ||
|
|
||
| a.send(chatMsg{id: "u1", text: "hello"}) | ||
| waitFor(t, "initial fan-out", func() bool { | ||
| return live.count() == 1 && dead.count() == 1 | ||
| }) | ||
|
|
||
| a.remove(dead) | ||
| if n := len(a.progs); n != 1 { | ||
| t.Fatalf("registry has %d programs after remove, want 1", n) | ||
| } | ||
|
|
||
| a.send(chatMsg{id: "u1", text: "again"}) | ||
| waitFor(t, "second fan-out", func() bool { return live.count() == 2 }) | ||
| if got := dead.count(); got != 1 { | ||
| t.Fatalf("removed program received %d messages, want 1", got) | ||
| } | ||
| } | ||
|
|
||
| func TestRemoveUnknownIsNoOp(t *testing.T) { | ||
| a := &app{} | ||
| kept := &fakeProg{} | ||
| a.progs = append(a.progs, kept) | ||
|
|
||
| a.remove(&fakeProg{}) | ||
| if n := len(a.progs); n != 1 { | ||
| t.Fatalf("registry has %d programs after removing unknown, want 1", n) | ||
| } | ||
| } | ||
|
|
||
| func TestRegistryConcurrentRegisterSendRemove(t *testing.T) { | ||
| a := &app{} | ||
| var wg sync.WaitGroup | ||
| for i := 0; i < 16; i++ { | ||
| wg.Add(1) | ||
| go func(i int) { | ||
| defer wg.Done() | ||
| p := &fakeProg{} | ||
| a.mu.Lock() | ||
| a.progs = append(a.progs, p) | ||
| a.mu.Unlock() | ||
| for j := 0; j < 25; j++ { | ||
| a.send(chatMsg{id: fmt.Sprint(i), text: "m"}) | ||
| } | ||
| a.remove(p) | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| a.mu.RLock() | ||
| n := len(a.progs) | ||
| a.mu.RUnlock() | ||
| if n != 0 { | ||
| t.Fatalf("registry has %d programs after concurrent use, want 0", n) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this goroutine runs before Wish starts the program,
p.Wait()blocks forever: in the pinned Bubble Tea v0.25.0 implementation,finishedis initialized byRun, notNewProgram, andWaitreceives directly from that channel. The Wish middleware calls this handler beforep.Run(), so there is no initialization ordering guarantee. An early waiter receives from a nil channel permanently, leaking the new goroutine and preventing that session from ever being deregistered; concurrent initialization also introduces a data race. Tie removal to a lifecycle hook with guaranteed startup ordering or to the return ofRun.Useful? React with 👍 / 👎.