fix(runtime): saturate FlushingPolicyState counters to prevent add-overflow panic (#1359)#1413
Open
teddytennant wants to merge 1 commit into
Open
Conversation
…erflow panic (tracel-ai#1359) `FlushingPolicyState::register` accumulated its two u32 counters with `+=` on every staged `Bytes` allocation. Flushing is advisory, so between the consumer's flush/reset calls the counters can grow without bound; on a large workload `bytes_size += bytes.len() as u32` eventually exceeds `u32::MAX` and panics with "attempt to add with overflow" (release builds silently wrap and corrupt the flush decision). Use `saturating_add` for both counters, and convert the length with `u32::try_from(..).unwrap_or(u32::MAX)` so a single >= 4 GiB allocation saturates instead of truncating. Because `should_flush` compares with `>=`, saturating at `u32::MAX` never changes the flush decision. No public API change. Fixes tracel-ai#1359
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
FlushingPolicyState::registernow accumulates itsbytes_countandbytes_sizecounters withsaturating_addinstead of+=, and converts the allocation length withu32::try_from(bytes.len()).unwrap_or(u32::MAX)instead ofas u32.Why
PendingDropQueue::push()callsregister()on every stagedBytesallocation, growing the twou32counters. Flushing is advisory — the consumer decides when to callflush()/reset()based onshould_flush()— so between flushes the counters can grow without bound. On a large workload (reported in #1359 while training a resnet model on a 24 GB GPU; more data-loader workers reproduce it faster)self.bytes_size += bytes.len() as u32eventually exceedsu32::MAX:attempt to add with overflowatpolicy.rs, the site reported in the issue;saturating_addcan never panic or wrap. Becauseshould_flush()compares with>=, saturating atu32::MAXnever changes the flush decision — once at/over threshold a flush+reset follows. The saturatingusize -> u32conversion also fixes a latent truncation bug: a single allocation>= 4 GiBpreviously truncated to a small wrong value and could suppress a needed flush; it now counts asu32::MAX, guaranteeing a flush.No public API change (the
u32fields onFlushingPolicyare untouched), the defaultmax_bytes_sizeof 64 MiB means normal configs are unaffected, and no-std/alloc compatibility is preserved.Testing
Added
register_saturates_instead_of_overflowingto the existingpolicy_testsmodule. It stages an 8-byte allocation on a state whose counters are already at/nearu32::MAX. On the current code this panics withattempt to add with overflow(debug overflow-checks); with the fix the counters saturate andshould_flushreturnstrue.All 6 policy tests pass.
cargo fmt --checkandcargo clippy -p cubecl-runtime --all-targets -- -D warningsare clean.Fixes #1359