Commit 51f4297
Bug RHOAIENG-39096: Fix race condition when deleting projects quickly (#546)
## Summary
Fixes a race condition that occurred when deleting multiple projects in
quick succession.
## Problem
When deleting two projects rapidly:
1. First deletion invalidates `projectKeys.lists()`
2. This triggers a refetch of the project list from backend
3. Second deletion happens before the refetch completes
4. State becomes inconsistent, causing "project not found" errors
## Solution
Implemented optimistic updates with proper error handling in the
`useDeleteProject` hook:
- **Optimistic updates**: Projects are immediately removed from the
cache before the API call completes
- **Cancel in-flight queries**: Prevents race conditions by canceling
any ongoing refetches
- **Proper rollback**: Restores previous state on errors (except 404s,
which are fine)
- **Handles both response types**: Works with paginated and legacy list
query responses
- **Updates totalCount**: Correctly decrements count in paginated
responses
- **Idempotent deletion**: Silently ignores "not found" errors (project
already deleted)
## Testing
- ✅ Frontend builds successfully with no TypeScript errors
- ✅ Linting passes without issues
- ✅ Projects are removed from UI immediately on deletion
- ✅ Multiple rapid deletions no longer cause race conditions
- ✅ Error handling properly restores state on non-404 failures
## Files Changed
- `components/frontend/src/services/queries/use-projects.ts`
## Related Issue
https://issues.redhat.com/browse/RHOAIENG-39096
Co-authored-by: Ambient Code <ambient-code@users.noreply.github.com>
Co-authored-by: Claude (claude-sonnet-4-5) <noreply@anthropic.com>1 parent fea1fee commit 51f4297
1 file changed
Lines changed: 57 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
101 | 104 | | |
102 | 105 | | |
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
107 | 162 | | |
108 | | - | |
| 163 | + | |
109 | 164 | | |
110 | | - | |
111 | | - | |
| 165 | + | |
112 | 166 | | |
113 | 167 | | |
114 | 168 | | |
| |||
0 commit comments