chunk() never resets its size counter: past 500 KB every queued event is uploaded as its own request
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- react-native, typescript
- Domain
- backend-api-design, performance
Research direction
The bug is in the chunk function in packages/core/src/util.ts. Start by reading the function and the sizeOf helper. The issue includes a reproduction case and a suggested fix. Test the fix by running existing tests for the chunking logic, or create a small test to verify the corrected behavior returns the expected 20 chunks instead of thousands. Check for any related tests in the repository.
Written by the indexing model from the issue text.
Description
Summary
chunk() in packages/core/src/util.ts never resets rollingKBSize. Once the queue's running size reaches maxKB (MAX_PAYLOAD_SIZE_IN_KB = 500), every later event goes into its own chunk. SegmentDestination.sendEvents then uploads all chunks at once with Promise.all, so a single flush sends one HTTP request per queued event.
if (maxKB !== undefined) {
rollingKBSize += sizeOf(item);
if (rollingKBSize >= maxKB) {
chunks[++currentChunk] = [item]; // rollingKBSize is never reset
return chunks;
}
}
Reproduction
chunk(events, 1000, 500) with 10,000 events of about 1 KB each returns 9,481 chunks: one of 520 events, then 9,480 of one event each. The expected result is 20 chunks of about 520.
Impact
It shows up when the queue grows past 500 KB, which happens when uploads keep failing (for example, a device whose DNS blocks *.segmentapis.com). The queue persists across launches. If cdn-settings.segment.com is unreachable too, there's no httpConfig, so no backoff and no maxTotalBackoffDuration pruning, and maxQueueSize isn't enforced anywhere. In production we saw one iOS session make about 9,700 upload attempts in 91 seconds, at a steady ~60 per second (URLSession's 6 connections per host).
Version: 2.24.1. Same code on master.
Suggested fix
Keep a size counter per chunk and reset it when a new chunk starts, and push chunks instead of assigning by index. That also fixes the sparse-array case in #1309.
export const chunk = <T>(array: T[], count: number, maxKB?: number): T[][] => {
if (!array.length || !count) return [];
const result: T[][] = [];
let current: T[] = [];
let currentKB = 0;
for (const item of array) {
const itemKB = maxKB !== undefined ? sizeOf(item) : 0;
const isFull = current.length >= count || (maxKB !== undefined && currentKB + itemKB >= maxKB);
if (current.length > 0 && isFull) {
result.push(current);
current = [];
currentKB = 0;
}
current.push(item);
currentKB += itemKB;
}
if (current.length > 0) result.push(current);
return result;
};
- Dominant language
- TypeScript
- Stars
- 383
- Forks
- 206
- Avg merge
- 14h 45m
- Merged PRs (30d)
- 11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from segmentio/analytics-react-native
-
bug investigate
Difficulty 1/5 Under an hour Newbie friendliness 72/100
segmentio/analytics-react-native#1145 ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 68/100
segmentio/analytics-react-native#1328 ·
-
bug investigate
Difficulty 3/5 1-2 days Newbie friendliness 78/100
segmentio/analytics-react-native#1309 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
segmentio/analytics-react-native#1124 · 3 comments ·
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 48/100
segmentio/analytics-react-native#1114 ·
All issues in segmentio/analytics-react-native
Similar issues
-
bug(cli): hapi doctor inline-media prints a fabricated B:\ helper-script path in packaged installs Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Crush Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
catppuccin/catppuccin#3125 ·
-
Add a SECURITY.md Open
Difficulty 1/5 Under an hour Newbie friendliness 90/100
ElementsProject/cln-application#167 · 1 comment · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Quantco/pnpm-licenses#17 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100