From 4ca6c6be38bdf5454f60f127ca4556cbb65cefdb Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sat, 28 Nov 2026 11:54:54 -0500 Subject: [PATCH] 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 --- internal/recommendation/home.go | 114 ++++++++++++--------------- internal/recommendation/home_test.go | 22 ++++++ 2 files changed, 71 insertions(+), 65 deletions(-) create mode 100644 internal/recommendation/home_test.go diff --git a/internal/recommendation/home.go b/internal/recommendation/home.go index c61c11c5..726b9cbc 100644 --- a/internal/recommendation/home.go +++ b/internal/recommendation/home.go @@ -7,6 +7,7 @@ package recommendation import ( "context" "fmt" + "runtime/debug" "sync" "github.com/jackc/pgx/v5/pgtype" @@ -56,7 +57,7 @@ type HomePayload struct { 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 + // explicit-like eligibility, plus a daily-stable random fallback when // 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{}, } - wg.Add(7) - go func() { - defer wg.Done() - rows, err := q.ListRecentlyAddedAlbumsWithArtist(ctx, HomeRecentlyAddedLimit) - if err != nil { - fail("recently_added", err) - return - } - out.RecentlyAddedAlbums = rows - }() - go func() { - defer wg.Done() - rows, err := q.ListMostPlayedTracksForUser(ctx, dbq.ListMostPlayedTracksForUserParams{ + // 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() { + defer wg.Done() + defer func() { + if r := recover(); r != nil { + fail(stage, fmt.Errorf("panicked: %v\n%s", r, debug.Stack())) + } + }() + if err := load(); err != nil { + fail(stage, err) + } + }() + } + + 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, }) - if err != nil { - fail("most_played", err) - return - } - out.MostPlayedTracks = rows - }() - go func() { - defer wg.Done() - rows, err := q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{ + return err + }) + section("last_played", func() (err error) { + out.LastPlayedArtists, err = q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{ UserID: userID, Limit: HomeLastPlayedLimit, }) - if err != nil { - fail("last_played", err) - return - } - out.LastPlayedArtists = rows - }() - go func() { - defer wg.Done() - rows, err := loadRediscoverAlbums(ctx, q, userID) - if err != nil { - fail("rediscover_albums", err) - return - } - 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{ + return err + }) + section("rediscover_albums", func() (err error) { + out.RediscoverAlbums, err = loadRediscoverAlbums(ctx, q, userID) + return err + }) + section("rediscover_artists", func() (err error) { + out.RediscoverArtists, err = loadRediscoverArtists(ctx, q, userID) + return err + }) + section("you_might_like_albums", func() (err error) { + out.YouMightLikeAlbums, err = q.ListYouMightLikeAlbumsForUser(ctx, dbq.ListYouMightLikeAlbumsForUserParams{ UserID: userID, Limit: youMightLikeFetch, }) - if err != nil { - fail("you_might_like_albums", err) - return - } - out.YouMightLikeAlbums = rows - }() - go func() { - defer wg.Done() - rows, err := q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{ + return err + }) + section("you_might_like_artists", func() (err error) { + out.YouMightLikeArtists, err = q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{ UserID: userID, Limit: youMightLikeFetch, }) - if err != nil { - fail("you_might_like_artists", err) - return - } - out.YouMightLikeArtists = rows - }() + return err + }) wg.Wait() if firstErr != nil { diff --git a/internal/recommendation/home_test.go b/internal/recommendation/home_test.go new file mode 100644 index 00000000..70ed2b05 --- /dev/null +++ b/internal/recommendation/home_test.go @@ -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) + } +}