Commit d5e039f
Expire snapshot mutability issue (#2430)
<!--
Thanks for opening a pull request!
-->
<!-- In the case this PR will resolve an issue, please replace
${GITHUB_ISSUE_ID} below with the actual Github issue id. -->
Closes #2409, and partially closes #2427
# Rationale for this change
This PR fixes a critical thread safety issue in the `ExpireSnapshots`
class where concurrent snapshot expiration operations on different
tables would share snapshot IDs, causing operations to fail with
"snapshot does not exist" errors.
**Root Cause:**
The `ExpireSnapshots` class had class-level attributes
(`_snapshot_ids_to_expire`, `_updates`, `_requirements`) that were
shared across all instances. When multiple threads created different
`ExpireSnapshots` instances, they all shared the same underlying `set()`
object for tracking snapshot IDs.
**Impact:**
- Thread 1: `table1.expire_snapshots().by_id(1001)` adds `1001` to
shared set
- Thread 2: `table2.expire_snapshots().by_id(2001)` adds `2001` to same
shared set
- Result: Both threads have `{1001, 2001}` and try to expire snapshot
`1001` from `table2`, causing failure
**Solution:**
Moved the shared class-level attributes to instance-level attributes in
the `__init__` method, ensuring each `ExpireSnapshots` instance has its
own isolated state.
## Are these changes tested?
> 📢 🔥 Big shout-out to @QlikFrederic, as the testing methodology was
largely derived from the testing and analysis done by the user! 🔥 📢
Yes, comprehensive test coverage has been added:
- **`test_thread_safety_fix()`** - Verifies that different
ExpireSnapshots instances have separate snapshot sets
- **`test_concurrent_operations()`** - Tests concurrent operations don't
contaminate each other
- **`test_concurrent_different_tables_expiration()`** - Reproduces the
exact scenario from GitHub issue #2409
- **`test_concurrent_same_table_different_snapshots()`** - Tests
concurrent operations on the same table
- **`test_cross_table_snapshot_id_isolation()`** - Validates no
cross-contamination of snapshot IDs between tables
- **`test_batch_expire_snapshots()`** - Tests batch expiration
operations in threaded environments
All existing tests continue to pass, ensuring no regression in
functionality.
## Are there any user-facing changes?
**No breaking changes.** The public API remains identical:
- All existing `ExpireSnapshots` methods work the same way
- Method signatures are unchanged
- Behavior is identical except for the thread safety fix
**Behavioral improvement:**
- Concurrent `expire_snapshots()` operations on different tables now
work correctly
- No more "snapshot does not exist" errors when using ExpireSnapshots in
multi-threaded environments
This is a pure bug fix with no user-facing API changes.
---------
Co-authored-by: Fokko Driesprong <[email protected]>1 parent 2624100 commit d5e039f
File tree
2 files changed
+69
-6
lines changed- pyiceberg/table/update
- tests/table
2 files changed
+69
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
924 | 924 | | |
925 | 925 | | |
926 | 926 | | |
927 | | - | |
928 | | - | |
929 | | - | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
930 | 936 | | |
931 | 937 | | |
932 | 938 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
143 | 146 | | |
144 | 147 | | |
145 | 148 | | |
146 | | - | |
| 149 | + | |
147 | 150 | | |
148 | 151 | | |
149 | 152 | | |
| |||
223 | 226 | | |
224 | 227 | | |
225 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
0 commit comments