Skip to content

Fix: Prevent notification count overflow when memory is low #5230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

anvithagowda098
Copy link

Phabricator:
https://phabricator.wikimedia.org/T385130

Notes

  • Introduced a safe upper limit (Int.max / 10) to prevent integer overflow.
  • Implemented a fallback mechanism:
  • If fetching the unread count fails, it uses the last known valid value instead of displaying an incorrect count.
  • Modified updateCacheWithCurrentUnreadNotificationsCount() to handle failures carefully.

Test Steps

  1. Open the Wikipedia app and navigate to the profile screen.
  2. Fill up the device storage to simulate low-memory conditions (or use Xcode’s Simulate Memory Warning feature).
  3. Check the notification count. It should correctly display unread notifications. No extreme values (e.g., 9223372036854775807) should appear.

@mazevedofs mazevedofs self-requested a review March 24, 2025 15:12
@mazevedofs mazevedofs self-assigned this Mar 24, 2025
Copy link
Collaborator

@mazevedofs mazevedofs left a comment

Choose a reason for hiding this comment

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

Hi @anvithagowda098,
Thanks for your contribution. I have a few suggestions to improve your current approach:

  1. The current use of Int.max / 10 to limit the notification count masks the underlying memory issue. Since a realistic notification count for Wikimedia projects is much lower, displaying such a cap may hide the fact that there's an overflow or an error in the count computation when fetching this info.
  2. Updating previousUnreadCount with the fetched count regardless of its correctness means that if the number is incorrect (e.g., due to overflow in this case), it will be stored and used as a fallback. We should update and cache it only if the count is correct.



Tips for approaching this issue:

  • After fetching the count, verify if it is within a realistic threshold (for instance, less than 1000). If the count exceeds this value, it likely indicates an underlying problem.

  • If the count is abnormally high, log an error or try to re-check it. This will help identify and diagnose the root cause.

  • Only update previousUnreadCount if the count passes the validation check. Otherwise, continue using the last known good value.

  • Take a closer look at modelController.numberOfUnreadNotifications() to determine if the overflow is due to low memory conditions, a fetching error, or an NSNumber casting issue.

Let me know if you have any questions or need any help.

@anvithagowda098
Copy link
Author

@mazevedofs Thanks for the feedback! I’ve made the requested changes.

Copy link
Collaborator

@mazevedofs mazevedofs left a comment

Choose a reason for hiding this comment

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

While the current solution probably prevents erroneous display values, it still doesn’t address the underlying issue causing the overflow. If low memory conditions are the source, I believe it’s worth investigating why we get this int overflow to solve the root cause.

@anvithagowda098
Copy link
Author

Thanks for the thorough review @mazevedofs !
I have made all the suggested changes.
I’d really like to dig deeper and understand what’s causing the inflated notification count in the first place.
If you have any ideas on how I could start investigating, I’d really appreciate your guidance. I want to make sure we’re not just masking a bigger problem.

@mazevedofs
Copy link
Collaborator

Thanks for the thorough review @mazevedofs ! I have made all the suggested changes. I’d really like to dig deeper and understand what’s causing the inflated notification count in the first place. If you have any ideas on how I could start investigating, I’d really appreciate your guidance. I want to make sure we’re not just masking a bigger problem.

Hi @anvithagowda098. I found this tutorial on how to simulate the low memory environment on simulator https://www.avanderlee.com/debugging/no-space-left-on-device/
Let me know if this is helpful.

@anvithagowda098
Copy link
Author

@mazevedofs I've made requested changes

@mazevedofs
Copy link
Collaborator

@mazevedofs I've made requested changes

Hi @anvithagowda098! Thanks for addressing the changes! Were you able to investigate what caused the integer overflow in the first place? Did simulating a low-memory device help? Let me know if you were able to reproduce the bug in the first place - I understand it's a difficult scenario to simulate.

@anvithagowda098
Copy link
Author

@mazevedofs I have my university exams going on currently
I will try to do it as soon as possible

@anvithagowda098
Copy link
Author

@mazevedofs Hey! I was able to successfully simulate a low memory environment with the help of the tutorial you provided. I couldn't reproduce the issue though. Either it's already resolved or doesn't occur under this condition. That said, this patch ensures that incorrect values are not displayed, offering a safeguard against similar scenarios.

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

Successfully merging this pull request may close these issues.

2 participants