Skip to content

Fix 'Last post' property of threads not correctly re-set after spammers get deleted#2166

Merged
ffont merged 2 commits into
MTG:masterfrom
kashyap-harshit:master
Jul 22, 2026
Merged

Fix 'Last post' property of threads not correctly re-set after spammers get deleted#2166
ffont merged 2 commits into
MTG:masterfrom
kashyap-harshit:master

Conversation

@kashyap-harshit

@kashyap-harshit kashyap-harshit commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Issue(s)
#1575

Description

Fix "Last post" not re-set after spammers get deleted (#1575)

Problem

When a spammer is deleted from the DB, their posts are cascade-deleted along
with their account. This left threads and forums with a blank "last post" and
an inflated reply count on the forum listing pages.

Root cause: the post_delete signal handler that repairs these cached
fields tried to update the spammer's own profile first. Since the profile was
already deleted in the cascade, that line raised Profile.DoesNotExist, which
was silently caught, so the code never reached the part that re-points
last_post and fixes num_posts.

Changes

1. Fix the signal handler (forum/models.py)

  • Update the author's profile count with a queryset update() so it's a no-op
    (instead of an exception) when the profile is already gone. The thread/forum
    repairs now always run.
  • Clamp all num_posts decrements with Greatest(..., 0) so deletions can't
    crash on counts that are already out of sync.
  • Remove the now-unreachable except Profile.DoesNotExist branch.
  • Add regression tests covering both deletion paths (user.delete() and
    Profile.delete_user(delete_user_object_from_db=True)).

2. Repair already-broken data (update_last_thread_forum_posts command)
The fix above only prevents new corruption; rows already broken on production
stay broken. This extends the existing repair command to also recompute
num_posts (previously it only fixed last_post), and to scan all threads and
forums rather than only those with a null last_post. Run it once after
deploying to heal the historical data.

Testing

  • New tests fail on the old code and pass with the fix.
  • Full forum/ and accounts/tests/test_user.py suites pass.
  • Verified the repair command end-to-end: manually corrupted a thread/forum,
    ran the command, confirmed last_post and num_posts were restored.

Deployment steps:
After deploying, run the forum repair command once to fix threads/forums whose
last_post/num_posts were already corrupted by this bug before the fix:

python manage.py update_last_thread_forum_posts

(Run with --dry first to see how many rows would change without writing.)

@kashyap-harshit

Copy link
Copy Markdown
Contributor Author

@ffont This is ready for review. it fixes #1575 by correcting the signal handler and also adds a repair command for the already corrupted production data. Tests cover both deletion paths. Let me know if you'd like any changes. Thanks :)

@ffont

ffont commented Jul 22, 2026

Copy link
Copy Markdown
Member

Thanks a lot for the PR, I reviewed and looks good. I'll merge!

@ffont
ffont merged commit 512b83d into MTG:master Jul 22, 2026
2 checks passed
@kashyap-harshit

Copy link
Copy Markdown
Contributor Author

Thank you so much!
Also I am still interested on the DSP-related task. I would love to hear more about it :)

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