Skip to content

fix(toxics/bandwidth): don't hang or panic when rate <= 0 - #764

Open
jaideeppyne wants to merge 1 commit into
Shopify:mainfrom
jaideeppyne:fix/bandwidth-nonpositive-rate
Open

fix(toxics/bandwidth): don't hang or panic when rate <= 0#764
jaideeppyne wants to merge 1 commit into
Shopify:mainfrom
jaideeppyne:fix/bandwidth-nonpositive-rate

Conversation

@jaideeppyne

Copy link
Copy Markdown

Problem

BandwidthToxic.Pipe already treats a non-positive rate as "no throttling" (if t.Rate <= 0 { sleep = 0 }), but the packet-splitting loop used t.Rate*100 as the split size unconditionally:

  • rate == 0 → split size is 0, so len(p.Data) > 0 stays true forever; the loop spins emitting empty chunks (p.Data[:0]) and never forwards the payload — the connection hangs.
  • rate < 0 → split size is negative; p.Data[:negative] panics with slice bounds out of range.

A bandwidth toxic created with rate: 0 (the zero value when the field is omitted) is enough to trigger the hang; there is no input validation rejecting rate <= 0.

Fix

Guard the split loop on t.Rate > 0 so a non-positive rate falls through to the normal unthrottled send path — matching the existing t.Rate <= 0 intent.

Tests

Added TestBandwidthToxicNonPositiveRate (rate_0, rate_-1), which fails before the change (empty forward / panic) and passes after. Full toxics package passes; gofmt/go vet clean.

BandwidthToxic.Pipe already treats a non-positive rate as "no throttling"
(sleep = 0), but the packet-splitting loop used t.Rate*100 as the split
size unconditionally. With rate == 0 that size is 0, so the loop spins
forever emitting empty chunks (p.Data[:0]) and never forwards the payload;
with rate < 0 the size is negative and p.Data[:negative] panics with
"slice bounds out of range".

Guard the loop on t.Rate > 0 so a non-positive rate falls through to the
normal (unthrottled) send path. Adds a regression test covering rate 0
(no hang) and rate -1 (no panic).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant