Links to broken uploads in topics lead to 500 errors with 2.5.0

סיכום מבוסס בינה מלאכותית

In a Discourse support thread titled “Links to broken uploads in topics lead to 500 errors with 2.5.0,” sukria reports that after upgrading to Discourse 2.5.0, logged-in users encountered 500 errors due to broken S3 upload links—caused by an accidentally deleted S3 bucket. Anonymous users were unaffected. The issue stemmed from Discourse attempting to process non-existent files, triggering NoMethodError: undefined method 'path' for nil:NilClass in base_store.rb and upload.rb. sukria confirmed the issue persisted even without plugins and temporarily patched it by forcing local? to true. sam initially suspected plugins, but sukria ruled them out. sam recommended deleting broken upload records from the database, which sukria found risky without guidance. ParrotDeveloper reported a similar issue, resolved by rerunning the wizard. david confirmed the bug and noted a fix in the latest tests-passed branch, making Discourse more resilient to broken uploads—though cleanup is still advised. sukria confirmed the fix worked after rebuilding against tests-passed and requested an easy method to remove dead image links from topics. The community agrees that while the immediate crash is patched, a proper cleanup tool or SQL script for orphaned upload references is still needed.

I run a Discourse site and I wish to start by saying how much I love this software. Thanks to all the team for the great work, this is an awesome tool.

After a 2.5.0 upgrade, I went into big troubles. Here is the recap and the workaround I did. Maybe this will lead to a much better fix in future versions.

Some of the topics we have in our database contain broken uploads. We used the “S3 uploads” feature and deleted the bucket by mistake. Yes, that’s a very stupid thing to do, but, well, we did! The result was that all the topics images were lost.

Except from the fact we had lost many images, everything was fine, Discourse was showing broken links to non existing pictures, but all was good.

But when we wanted to upgrade to 2.5.0, it went crazy: all logged-in users got 500 errors, while anonymous visitors were able to see the website.

After investigating, I realized Discourse was dying on “No such file” errors. It was trying to download them it seems. I had to force the local? method to true in /var/www/discourse/app/models/upload.rb and that did the trick, but I’m a bit worried about the next upgrades…

This is clearly something new, as previous upgrades did not break.

By the way, is there any method I can follow to remove all the dead image links we have in our topics?

Thanks.

We got the opposite behavior : logged in user is good, not logged in are unable to connect and get a error 500

Can you share one full backtrace of such error?
Check your /logs page.

This to me very much sounds like an incompatible plugin, what unofficial plugins are you running? Have you tried rebuilding without them?

Sure, in my log/production.log, I had the following exception (when Discourse was accessing the empty S3 bucket (which I recreated, hoping it would fix the problem).

Started GET “/” for 86.246.127.170 at 2020-05-16 14:29:06 +0000
Processing by ListController#latest as HTML
Creating scope :open. Overwriting existing method Poll.open.
Completed 500 Internal Server Error in 3638ms (ActiveRecord: 0.0ms | Allocations: 135090)
NoMethodError (undefined method path' for nil:NilClass) /var/www/discourse/lib/file_store/base_store.rb:150:in cache_file’

Hope that helps.

Thanks, that might be it. I indeed have two plugins installed: Discourse Adsense and discourse-chat-integration, I’ll try a rebuild without them.

I can confirm this is not caused by a plugin : I reproduced the same bug with a fresh install + a restore of my last backup and no plugins installed unless docker_manager.

The first stacktrace I get is :

Completed 500 Internal Server Error in 4169ms (ActiveRecord: 0.0ms | Allocations: 72058)
NoMethodError (undefined method path' for nil:NilClass) /var/www/discourse/lib/file_store/base_store.rb:150:in cache_file’

I patched it this way:

def cache_file(file, filename)
path = get_cache_path_for(filename)
dir = File.dirname(path)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

  if file.nil?
    return
  end

  FileUtils.cp(file.path, path)

Which lead to a second error (the one I mentioned in my initial post):

Started GET “/” for 86.246.127.170 at 2020-05-18 07:37:40 +0000
Processing by CategoriesController#index as HTML
Completed 500 Internal Server Error in 4342ms (ActiveRecord: 0.0ms | Allocations: 60478)
NoMethodError (undefined method path' for nil:NilClass) /var/www/discourse/app/models/upload.rb:193:in fix_dimensions!’

Which I fixed by forcing local? to true:

def local?
return true
!(url =~ /^(https?:)?///)
end

Have you moved your files to or from s3 at some point in time?

Yes, I did. But then, the bucket got deleted. And I switched back to local storage (with dead image links in previous topics, obviously).

I think the best bet here is for you to hard nuke all the bad Upload rows from your database.

If you have no upload, don’t keep it there.

Yes, I agree, but I’m afraid on running such a nuke by hand on the DB. Any suggestions for doing it “properly”? Or should I do it really “by hand” ?

Also, I have the feeling this change of behavior from Discourse is a bit worrying. It means, as far as I can tell, that if at some point, there’s a dead image link somewhere, the app is going to crash.

It was not the case before. I would suggest to make it more robust in such situations.

Very close to what we have :

got it “fixed” by rerunning the wizard setup!

Yes this looks like the same issue. @sukria if you update to the latest tests-passed I have added a fix which makes things more robust for broken uploads (although, you should still try to clean up the broken upload references)

I am also interested in an easy way to clean those broken links as well :wink:

Thanks @david, I confirm a fresh rebuild against tests-passed worked out of the box. Thanks!
Any suggestion on a proper/easy way to get rid of those dead links in topics?