fix(tasteprofile): create the taste profile row race-free #211

Merged
midas merged 1 commit from fix/taste-profile-race into main 2026-12-01 22:33:59 +01:00
Owner

Cause

TasteProfileService created a user's taste profile as find-then-insert (findByUserId(...) ?: TasteProfile(userId) followed by save). When two transactions work on the same profile before it exists, both see no row and both insert. The second insert hits the unique index on taste_profiles(user_id), and GlobalExceptionHandler maps the DataIntegrityViolationException to 409.

In Phase3IntegrationTest.test17_getTasteProfile the two writers are:

  1. The TasteProfileInvalidatedEvent listener (onTasteProfileInvalidated -> recalculate), triggered by the three rating POSTs.
  2. GET /api/taste-profile, which calls recalculate itself when no row exists yet.

These overlap even under the test profile. TestAsyncConfig is meant to make @Async synchronous, but as far as I can tell from the code, @Async does not end up on its SyncTaskExecutor. Spring resolves the default async executor with getBean(TaskExecutor.class), and the @Primary taskScheduler bean (a ThreadPoolTaskScheduler, which is also a TaskExecutor) wins that lookup. The listener therefore runs on the test-scheduling- thread while the test already sends its GET. The existing test comments (@Async

## Cause `TasteProfileService` created a user's taste profile as find-then-insert (`findByUserId(...) ?: TasteProfile(userId)` followed by `save`). When two transactions work on the same profile before it exists, both see no row and both insert. The second insert hits the unique index on `taste_profiles(user_id)`, and `GlobalExceptionHandler` maps the `DataIntegrityViolationException` to 409. In `Phase3IntegrationTest.test17_getTasteProfile` the two writers are: 1. The `TasteProfileInvalidatedEvent` listener (`onTasteProfileInvalidated` -> `recalculate`), triggered by the three rating POSTs. 2. `GET /api/taste-profile`, which calls `recalculate` itself when no row exists yet. These overlap even under the `test` profile. `TestAsyncConfig` is meant to make `@Async` synchronous, but as far as I can tell from the code, `@Async` does not end up on its `SyncTaskExecutor`. Spring resolves the default async executor with `getBean(TaskExecutor.class)`, and the `@Primary` `taskScheduler` bean (a `ThreadPoolTaskScheduler`, which is also a `TaskExecutor`) wins that lookup. The listener therefore runs on the `test-scheduling-` thread while the test already sends its GET. The existing test comments (@Async
fix(tasteprofile): create the taste profile row race-free (#207)
All checks were successful
Build and Publish / mcp (push) Successful in 9s
Build and Publish / frontend (push) Successful in 15s
Build and Publish / backend (push) Successful in 3m22s
Build and Publish / publish (push) Has been skipped
c41bf80f58
Creating a user's taste profile was find-then-insert, so two transactions
working on the same new profile (a GET computing it while the
TasteProfileInvalidatedEvent listener recomputes it, or two requests) could
both insert and the loser hit the unique index on taste_profiles(user_id),
which GlobalExceptionHandler reports as 409.

The row is now created with INSERT ... ON CONFLICT DO NOTHING inside the
caller's transaction and then re-read, so whichever transaction commits
first wins and the others update that same row. Used by recalculate,
updatePreferredLanguages and updateExcludedRegions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
midas merged commit 861049d44d into main 2026-12-01 22:33:59 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
midas/podradar!211
No description provided.