Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

chunk() never resets its size counter: past 500 KB every queued event is uploaded as its own request

Open Beginner friendly
#1,334 0 comments 0 reactions 0 assignees View on GitHub

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

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from segmentio/analytics-react-native

All issues in segmentio/analytics-react-native

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.