detached 8edd742

fix: clean up main loop state on panic

commit
8edd742134b070b013cf8a28345e89f4a414c543
author
brice · · 2026-05-15 12:33 UTC
parent
ee1755a
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +30 -5
M internal/capi/stream_ops.go +5 -5
@@ -222,13 +222,13 @@ func (s *streamOpsImpl) RunMainLoop(loopPtr unsafe.Pointer) error {
222222 }
223223 s.runningLoops[loopPtr] = true
224224 s.mu.Unlock()
225+ defer func() {
226+ s.mu.Lock()
227+ delete(s.runningLoops, loopPtr)
228+ s.mu.Unlock()
229+ }()
225230
226231 ret := pw_main_loop_run(loopPtr)
227-
228- s.mu.Lock()
229- delete(s.runningLoops, loopPtr)
230- s.mu.Unlock()
231-
232232 if ret < 0 {
233233 return &PWError{Func: "pw_main_loop_run", Code: ret}
234234 }
@@ -222,13 +222,13 @@ func (s *streamOpsImpl) RunMainLoop(loopPtr unsafe.Pointer) error {
222 }222 }
223 s.runningLoops[loopPtr] = true223 s.runningLoops[loopPtr] = true
224 s.mu.Unlock()224 s.mu.Unlock()
225+ defer func() {
226+ s.mu.Lock()
227+ delete(s.runningLoops, loopPtr)
228+ s.mu.Unlock()
229+ }()
225 230
226 ret := pw_main_loop_run(loopPtr)231 ret := pw_main_loop_run(loopPtr)
227-
228- s.mu.Lock()
229- delete(s.runningLoops, loopPtr)
230- s.mu.Unlock()
231-
232 if ret < 0 {232 if ret < 0 {
233 return &PWError{Func: "pw_main_loop_run", Code: ret}233 return &PWError{Func: "pw_main_loop_run", Code: ret}
234 }234 }
M internal/capi/stream_ops_test.go +25 -0
@@ -564,6 +564,31 @@ func TestRunMainLoopReturnsNilOnSuccess(t *testing.T) {
564564 }
565565 }
566566
567+func TestRunMainLoopRemovesRunningLoopOnPanic(t *testing.T) {
568+ origRun := pw_main_loop_run
569+ t.Cleanup(func() { pw_main_loop_run = origRun })
570+
571+ pw_main_loop_run = func(loop unsafe.Pointer) int32 {
572+ panic("boom")
573+ }
574+
575+ ops := &streamOpsImpl{}
576+ fakeLoop := opaquePtr()
577+
578+ defer func() {
579+ if r := recover(); r == nil {
580+ t.Fatal("expected panic from pw_main_loop_run")
581+ }
582+ ops.mu.Lock()
583+ defer ops.mu.Unlock()
584+ if len(ops.runningLoops) != 0 {
585+ t.Fatalf("expected runningLoops to be cleaned up, got %d entries", len(ops.runningLoops))
586+ }
587+ }()
588+
589+ _ = ops.RunMainLoop(fakeLoop)
590+}
591+
567592 // TestCreatePlaybackStreamUsesPWLoopFromMainLoop verifies that CreatePlaybackStream
568593 // calls pw_main_loop_get_loop to obtain the pw_loop pointer and passes it to
569594 // pw_stream_new_simple (not the raw main loop pointer).
@@ -564,6 +564,31 @@ func TestRunMainLoopReturnsNilOnSuccess(t *testing.T) {
564 }564 }
565 }565 }
566 566
567+func TestRunMainLoopRemovesRunningLoopOnPanic(t *testing.T) {
568+ origRun := pw_main_loop_run
569+ t.Cleanup(func() { pw_main_loop_run = origRun })
570+
571+ pw_main_loop_run = func(loop unsafe.Pointer) int32 {
572+ panic("boom")
573+ }
574+
575+ ops := &streamOpsImpl{}
576+ fakeLoop := opaquePtr()
577+
578+ defer func() {
579+ if r := recover(); r == nil {
580+ t.Fatal("expected panic from pw_main_loop_run")
581+ }
582+ ops.mu.Lock()
583+ defer ops.mu.Unlock()
584+ if len(ops.runningLoops) != 0 {
585+ t.Fatalf("expected runningLoops to be cleaned up, got %d entries", len(ops.runningLoops))
586+ }
587+ }()
588+
589+ _ = ops.RunMainLoop(fakeLoop)
590+}
591+
567 // TestCreatePlaybackStreamUsesPWLoopFromMainLoop verifies that CreatePlaybackStream592 // TestCreatePlaybackStreamUsesPWLoopFromMainLoop verifies that CreatePlaybackStream
568 // calls pw_main_loop_get_loop to obtain the pw_loop pointer and passes it to593 // calls pw_main_loop_get_loop to obtain the pw_loop pointer and passes it to
569 // pw_stream_new_simple (not the raw main loop pointer).594 // pw_stream_new_simple (not the raw main loop pointer).

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 8

↑↓ moveenter openesc close