Skip to content

fix(helper): handle negative byte counts in get_readable_size#6244

Open
JSap0914 wants to merge 1 commit into
jina-ai:masterfrom
JSap0914:fix-helper-readable-size-negative
Open

fix(helper): handle negative byte counts in get_readable_size#6244
JSap0914 wants to merge 1 commit into
jina-ai:masterfrom
JSap0914:fix-helper-readable-size-negative

Conversation

@JSap0914

Copy link
Copy Markdown

Goals:

  • Make get_readable_size produce human-readable units for negative byte counts instead of falling back to raw Bytes.

get_readable_size only scales values into KB/MB/GB when num_bytes >= 1024. Because the very first branch is if num_bytes < 1024, every negative value (which is always < 1024) is returned verbatim as Bytes, e.g. get_readable_size(-2 * 1024**2) returns '-2097152 Bytes' instead of '-2.0 MB'.

This actually surfaces in the profiler. jina/logging/profile.py reports a memory delta with get_readable_size(end_mem - start_mem), and that delta is negative whenever memory is freed, so freed-memory deltas are printed as a large raw-byte number rather than a readable size.

Fix:

  • Compute the size on abs(num_bytes) and re-apply the original sign to the formatted string. Positive inputs are unchanged.

Verification:

pytest tests/unit/test_helper.py::test_get_readable_size

Result: 9 passed. Added a parametrized regression test covering 0, positive Bytes/KB/MB/GB, and the negative Bytes/KB/MB/GB cases. Before the fix the four negative cases failed (e.g. assert '-2097152 Bytes' == '-2.0 MB'); after the fix they pass.

AI-assisted drafting; changes were reviewed and verified locally by a human before submission.

  • check and update documentation

Copilot AI review requested due to automatic review settings June 16, 2026 08:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants