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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user