Skip to content

[BUG]: Rest Client : Isolate spawning freezes app for 1KB response #469

@zubaid10

Description

@zubaid10

Bug Description

The REST client has a 1KB threshold for using compute() to decode JSON, which causes severe performance
issues.

Impact

  • Any API response >1KB triggers isolate spawning
  • Isolate spawning takes 20-87 seconds on Android devices
  • UI completely freezes - users can't interact with app
  • App appears to crash

Reproduction

  1. Make any API call that returns >1KB response (most typical API responses)
  2. Observe 20-87 second freeze on Android
  3. UI is completely unresponsive during this time

Our Experience

We discovered this bug in production:

  • 1.5KB response took 87 seconds to decode on Android
  • Backend responded in 500ms, but frontend froze for 87 seconds
  • Issue was isolate spawning (20-87s) not JSON parsing (16ms)

Root Cause

Lines 158 and 170 in rest_client_base.dart:

if (stringBody.length > 1000) {  // Too low!
  return await compute(json.decode, stringBody); // 87 second freeze
}

Solution

Increase threshold to 100KB:
if (stringBody.length > 100000) {  // 100KB is optimal
  return await compute(json.decode, stringBody);
}

Why 100KB?

- JSON parsing <100KB takes <20ms on main thread (fast!)
- Isolate spawning takes 20-87 seconds (extremely slow on Android)
- 100KB → 20ms parsing is better than 1KB → 87s isolate spawn
- See: https://github.com/flutter/flutter/issues/91606
Testing

After fix:
- 1.5KB response decoded in 16ms (was 87,000ms)
- UI never freezes
- App is responsive immediately

Recommendation

Change both thresholds from 1000 to 100000 in rest_client_base.dart

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions