A 500 error occurs when loading summary pages for some users due to a NoMethodError in the user_counts method of UserSummary when a user referenced in user_actions has been deleted. DiscourseMetrics reported the issue, and sam identified the root cause, noting that the error arises from corrupted data where acting_user_id in user_actions points to non-existent users. JanJoost confirmed experiencing the same issue. sam proposed two solutions: fixing the data via a weekly job (Option 1) or modifying the code to handle corrupted data (Option 2). codinghorror suggested choosing the easiest option, leading vinothkannans to implement the latter by modifying the code to safely handle missing user records via commit a16bb32865af313e11e7799c8383bde8fdeab856.
I’m seeing a 500 error loading some users’ summary pages:
NoMethodError (undefined method `attributes' for nil:NilClass) /var/www/discourse/app/models/user_summary.rb:196:in `block in user_counts'
and
/var/www/discourse/app/models/user_summary.rb:196:in `block in user_counts'
/var/www/discourse/app/models/user_summary.rb:194:in `map'
/var/www/discourse/app/models/user_summary.rb:194:in `user_counts'
/var/www/discourse/app/models/user_summary.rb:80:in `most_liked_users'
/var/www/discourse/vendor/bundle/ruby/2.5.0/gems/active_model_serializers-
0.8.4/lib/active_model/serializer.rb:122:in `block (2 levels) in associate'
...
I haven’t been able to ascertain if there are commonalities between the user profiles this happens to, but perhaps it has to do with which users are most liked by that user.
it "is robust enough to handle bad data" do
UserActionCreator.enable
liked_post = create_post
user = Fabricate(:user)
PostAction.act(user, liked_post, PostActionType.types[:like])
users = UserSummary.new(user, Guardian.new).most_liked_users
expect(users.map(&:id)).to eq([liked_post.user_id])
# really we should not be corrupting stuff like this
# but in production dbs this can happens sometimes I guess
liked_post.user.delete
users = UserSummary.new(user, Guardian.new).most_liked_users
expect(users).to eq([])
end
But you should not be doing #delete on users and leaving a mess around.
Simplest workaround is hunting for all the acting_user_id in user_actions table that do not have records in users table and nuking properly.
Since I have not seen this in our logs across 1000 sites I am somewhat reluctant to patch the code to allow for this corruption.