500 error in discussions.json

I have a relatively large merge request in a local CE instance (320 comments, 10 commits, 385+ changes).

The MR and comments were originally made in GitLab 9.5, but it has since been upgraded to 11.4.5 (f5536c6).

When trying to view the comments the load of discussions.json eventually reports 500 after 57 seconds (I tried increasing the default timeout but that didn’t help). In the production log is the following:

Completed 500 Internal Server Error in 57069ms (ActiveRecord: 31.8ms)

NoMethodError (undefined method `index' for nil:NilClass):
  app/models/concerns/discussion_on_diff.rb:46:in `truncated_diff_lines'
  app/serializers/base_serializer.rb:16:in `represent'
  app/controllers/concerns/issuable_actions.rb:107:in `discussions'
  lib/gitlab/i18n.rb:53:in `with_locale'
  lib/gitlab/i18n.rb:59:in `with_user_locale'
  app/controllers/application_controller.rb:424:in `set_locale'
  lib/gitlab/middleware/multipart.rb:101:in `call'
  lib/gitlab/request_profiler/middleware.rb:14:in `call'
  lib/gitlab/middleware/go.rb:17:in `call'
  lib/gitlab/etag_caching/middleware.rb:11:in `call'
  lib/gitlab/middleware/read_only/controller.rb:38:in `call'
  lib/gitlab/middleware/read_only.rb:16:in `call'
  lib/gitlab/middleware/basic_health_check.rb:25:in `call'
  lib/gitlab/request_context.rb:18:in `call'
  lib/gitlab/metrics/requests_rack_middleware.rb:27:in `call'
  lib/gitlab/middleware/release_env.rb:10:in `call'

Any ideas to resolve this?

I don’t really understand Ruby or the database structure, but I found that the notes table has position values like this:

--- !ruby/object:Gitlab::Diff::Position
  :old_path: Logging/src/GenericLogger.h
  :new_path: Logging/src/GenericLogger.h
  :new_line: 16
  :base_sha: f33cb783dc5f4ee4ad2c779408e6cfd0e54a51da
  :start_sha: f33cb783dc5f4ee4ad2c779408e6cfd0e54a51da
  :head_sha: 3f6118b92b96deb03f3f5f269e896d874a0f034e

And I don’t think that this matches the format that Gitlab::Diff::File is expecting. Is there a way to “upgrade” these to the expected format without data loss?

In particular, as best I can tell, Gitlab::Diff::File::line_for_position is returning nil because the position_type is not set?

Also possibly of note is that the discussion notes do seem to load ok when accessed from the commit SHA page rather than a merge request page. But then you have to hunt them down within the individual diffs rather than seeing just the notes, so it’s less convenient.

We are suddenly seeing the same thing (with different line numbers) but only on a single MR – the others seem to be just fine.

Any ideas on how to “repair” the discussion?