Replay re-check on the zero-seat path (same-student race)
Browse files at this commit8ae063d38762d7e7e88ec4411436d7605d8b767cParent: 8a76f89- fix
- replay re-check on the zero-seat path (same-student race)
2 changed files +40 -0
- .sqlx/query-d64689d8966396fe6d88d2a9d4000e9a08f3e2cdb91cc6fddd14e506e05e0ed6.json +24 -0
- src/db/meal_booking.rs +16 -0
.sqlx/query-d64689d8966396fe6d88d2a9d4000e9a08f3e2cdb91cc6fddd14e506e05e0ed6.jsonadded+24 -0
| @@ -0,0 +1,24 @@ | |||
| 1 | +{ | ||
| 2 | + "db_name": "PostgreSQL", | ||
| 3 | + "query": "SELECT 1 AS held FROM meal_booking\n WHERE menu = $1 AND student = $2 AND status = 'booked'", | ||
| 4 | + "describe": { | ||
| 5 | + "columns": [ | ||
| 6 | + { | ||
| 7 | + "ordinal": 0, | ||
| 8 | + "name": "held", | ||
| 9 | + "type_info": "Int4", | ||
| 10 | + "origin": "Expression" | ||
| 11 | + } | ||
| 12 | + ], | ||
| 13 | + "parameters": { | ||
| 14 | + "Left": [ | ||
| 15 | + "Text", | ||
| 16 | + "Uuid" | ||
| 17 | + ] | ||
| 18 | + }, | ||
| 19 | + "nullable": [ | ||
| 20 | + null | ||
| 21 | + ] | ||
| 22 | + }, | ||
| 23 | + "hash": "d64689d8966396fe6d88d2a9d4000e9a08f3e2cdb91cc6fddd14e506e05e0ed6" | ||
| 24 | +} | ||
src/db/meal_booking.rsmodified+16 -0
| @@ -185,6 +185,22 @@ pub(crate) async fn claim_and_place( | |||
| 185 | .fetch_optional(&mut *tx) | 185 | .fetch_optional(&mut *tx) |
| 186 | .await?; | 186 | .await?; |
| 187 | if seat.is_none() { | 187 | if seat.is_none() { |
| 188 | + // A racer whose held-check ran before the winner's row landed and | ||
| 189 | + // then queued on the menu row lock wakes to a full menu with its | ||
| 190 | + // own booking already placed — that is the replay, not a refusal. | ||
| 191 | + // (Each statement sees a fresh committed snapshot, so this read | ||
| 192 | + // observes the winner's row.) | ||
| 193 | + let won_elsewhere = sqlx::query!( | ||
| 194 | + "SELECT 1 AS held FROM meal_booking | ||
| 195 | + WHERE menu = $1 AND student = $2 AND status = 'booked'", | ||
| 196 | + menu_key, | ||
| 197 | + student, | ||
| 198 | + ) | ||
| 199 | + .fetch_optional(&mut *tx) | ||
| 200 | + .await?; | ||
| 201 | + if won_elsewhere.is_some() { | ||
| 202 | + return Ok(Claimed::Duplicate); | ||
| 203 | + } | ||
| 188 | return Ok(Claimed::Full); | 204 | return Ok(Claimed::Full); |
| 189 | } | 205 | } |
| 190 | // Revive a cancelled row only while it still stands at the attempt | 206 | // Revive a cancelled row only while it still stands at the attempt |