fix(home): a section that panics fails the Home call instead of the server (#8124)

HomeData loads its seven sections in bare goroutines joined by a
WaitGroup. A panic in one of them was off the request's goroutine, out
of the HTTP recoverer's reach, and would stop the whole server. The
sections now run through one helper that recovers a panic into that
section's error, with the stack, so the call fails as a query error
fails it. They stay request-scoped: each uses the request's context and
the call waits for all of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 11:54:54 -05:00
co-authored by Claude Opus 5.5
parent c9d54b0e7e
commit 4ca6c6be38
2 changed files with 71 additions and 65 deletions
+45 -61
View File
@@ -7,6 +7,7 @@ package recommendation
import ( import (
"context" "context"
"fmt" "fmt"
"runtime/debug"
"sync" "sync"
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
@@ -56,7 +57,7 @@ type HomePayload struct {
YouMightLikeArtists []dbq.ListYouMightLikeArtistsForUserRow YouMightLikeArtists []dbq.ListYouMightLikeArtistsForUserRow
} }
// HomeData runs five queries in parallel and assembles the payload. // HomeData loads the page's sections in parallel and assembles the payload.
// Rediscover sections fan out to inner-limit results (track-derived + // Rediscover sections fan out to inner-limit results (track-derived +
// explicit-like eligibility, plus a daily-stable random fallback when // explicit-like eligibility, plus a daily-stable random fallback when
// the eligibility query is sparse), then the Go layer trims to // the eligibility query is sparse), then the Go layer trims to
@@ -90,78 +91,61 @@ func HomeData(ctx context.Context, pool *pgxpool.Pool, userID pgtype.UUID) (*Hom
YouMightLikeArtists: []dbq.ListYouMightLikeArtistsForUserRow{}, YouMightLikeArtists: []dbq.ListYouMightLikeArtistsForUserRow{},
} }
wg.Add(7) // section loads one part of the page in its own goroutine. A panic in
// one becomes that section's error, so the call fails the way a query
// error fails it: left unrecovered off the request's goroutine, it
// would stop the whole server, which the HTTP recoverer can't catch.
section := func(stage string, load func() error) {
wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
rows, err := q.ListRecentlyAddedAlbumsWithArtist(ctx, HomeRecentlyAddedLimit) defer func() {
if err != nil { if r := recover(); r != nil {
fail("recently_added", err) fail(stage, fmt.Errorf("panicked: %v\n%s", r, debug.Stack()))
return
} }
out.RecentlyAddedAlbums = rows
}() }()
go func() { if err := load(); err != nil {
defer wg.Done() fail(stage, err)
rows, err := q.ListMostPlayedTracksForUser(ctx, dbq.ListMostPlayedTracksForUserParams{ }
}()
}
section("recently_added", func() (err error) {
out.RecentlyAddedAlbums, err = q.ListRecentlyAddedAlbumsWithArtist(ctx, HomeRecentlyAddedLimit)
return err
})
section("most_played", func() (err error) {
out.MostPlayedTracks, err = q.ListMostPlayedTracksForUser(ctx, dbq.ListMostPlayedTracksForUserParams{
UserID: userID, Limit: HomeMostPlayedLimit, UserID: userID, Limit: HomeMostPlayedLimit,
}) })
if err != nil { return err
fail("most_played", err) })
return section("last_played", func() (err error) {
} out.LastPlayedArtists, err = q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{
out.MostPlayedTracks = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{
UserID: userID, Limit: HomeLastPlayedLimit, UserID: userID, Limit: HomeLastPlayedLimit,
}) })
if err != nil { return err
fail("last_played", err) })
return section("rediscover_albums", func() (err error) {
} out.RediscoverAlbums, err = loadRediscoverAlbums(ctx, q, userID)
out.LastPlayedArtists = rows return err
}() })
go func() { section("rediscover_artists", func() (err error) {
defer wg.Done() out.RediscoverArtists, err = loadRediscoverArtists(ctx, q, userID)
rows, err := loadRediscoverAlbums(ctx, q, userID) return err
if err != nil { })
fail("rediscover_albums", err) section("you_might_like_albums", func() (err error) {
return out.YouMightLikeAlbums, err = q.ListYouMightLikeAlbumsForUser(ctx, dbq.ListYouMightLikeAlbumsForUserParams{
}
out.RediscoverAlbums = rows
}()
go func() {
defer wg.Done()
rows, err := loadRediscoverArtists(ctx, q, userID)
if err != nil {
fail("rediscover_artists", err)
return
}
out.RediscoverArtists = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListYouMightLikeAlbumsForUser(ctx, dbq.ListYouMightLikeAlbumsForUserParams{
UserID: userID, Limit: youMightLikeFetch, UserID: userID, Limit: youMightLikeFetch,
}) })
if err != nil { return err
fail("you_might_like_albums", err) })
return section("you_might_like_artists", func() (err error) {
} out.YouMightLikeArtists, err = q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{
out.YouMightLikeAlbums = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{
UserID: userID, Limit: youMightLikeFetch, UserID: userID, Limit: youMightLikeFetch,
}) })
if err != nil { return err
fail("you_might_like_artists", err) })
return
}
out.YouMightLikeArtists = rows
}()
wg.Wait() wg.Wait()
if firstErr != nil { if firstErr != nil {
+22
View File
@@ -0,0 +1,22 @@
package recommendation
import (
"context"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgtype"
)
// A section that panics fails the call with its stage named, as a query
// error does, rather than stopping the server from a goroutine nobody
// recovers. With no pool, every section's first query panics.
func TestHomeData_APanickingSectionIsAnError(t *testing.T) {
_, err := HomeData(context.Background(), nil, pgtype.UUID{Valid: true})
if err == nil {
t.Fatal("HomeData with no pool: no error, want the panic as one")
}
if !strings.Contains(err.Error(), "panicked") {
t.Errorf("err = %v, want it to say the section panicked", err)
}
}