500 error when posting a note to an MR diff

Summary

A user encountered a 500 error when posting a new comment to a diff for an existing MR. After this error, attempting to view the MR results in a 500 error.

Relevant logs and/or screenshots

gitlab-rails/production.log: (user-specific information has been replaced, but message text is intact).

Started POST "/groupname/projectname/notes?html=true" for 103.23.18.44 at 2018-07-16 11:58:43 +1200
Processing by Projects::NotesController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "view"=>"parallel", "line_type"=>"new", "merge_request_diff_head_sha"=>"aa5e7b3811753dbeea64b49dd3ad8379cb10dcf1", "in_reply_to_discussion_id"=>"
", "note_project_id"=>"", "target_type"=>"merge_request", "target_id"=>"2717", "note"=>{"noteable_type"=>"MergeRequest", "noteable_id"=>"2717", "commit_id"=>"d40bfe0e9c86dea81cb717e076a016062bd1d61b", "type"=>
"DiffNote", "line_code"=>"3071fa8fbb802efe5a64bc87759514a6bbe5c5b9_160_158", "position"=>"{\"base_sha\":\"e73a815d1c800046e7c8a5f8a71155d2471f1495\",\"start_sha\":\"e73a815d1c800046e7c8a5f8a71155d2471f1495\",\
"head_sha\":\"d40bfe0e9c86dea81cb717e076a016062bd1d61b\",\"old_path\":\"path/to/source\",\"new_path\":\"path/to/source\",\"position_type\":\"text\",\"old_line\":160,\"new_line\":158}", "note"=>"so, where is `simmr-ns` coming from now?"}, "html"=>"true", "namespace_id"=>"groupname", "project_id"=>"projectname"}
Completed 500 Internal Server Error in 67ms (ActiveRecord: 6.3ms)

NoMethodError (undefined method `index' for nil:NilClass):
  lib/gitlab/diff/file.rb:81:in `diff_hunk'
  app/models/diff_note.rb:65:in `create_diff_file'
  config/initializers/forbid_sidekiq_in_transactions.rb:49:in `block in committed!'
  config/initializers/forbid_sidekiq_in_transactions.rb:11:in `skipping_transaction_check'
  config/initializers/forbid_sidekiq_in_transactions.rb:49:in `committed!'
  app/services/notes/create_service.rb:34:in `execute'
  app/controllers/concerns/notes_actions.rb:41:in `create'
  lib/gitlab/i18n.rb:50:in `with_locale'
  lib/gitlab/i18n.rb:56:in `with_user_locale'
  app/controllers/application_controller.rb:362:in `set_locale'
  lib/gitlab/middleware/multipart.rb:95: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:28:in `call'
  lib/gitlab/middleware/read_only.rb:16: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'

After encountering this error, GET requests for the MR fail too:

Started GET "/groupname/projectname/merge_requests/1273/diffs?commit_id=d40bfe0e9c86dea81cb717e076a016062bd1d61b&w=1" for 103.23.18.44 at 2018-07-16 11:58:48 +1200
Processing by Projects::MergeRequestsController#show as HTML
  Parameters: {"commit_id"=>"d40bfe0e9c86dea81cb717e076a016062bd1d61b", "w"=>"1", "tab"=>"diffs", "namespace_id"=>"groupname", "project_id"=>"projectname", "id"=>"1273"}
Completed 500 Internal Server Error in 844ms (ActiveRecord: 39.2ms)

ActionView::Template::Error (undefined method `meta?' for nil:NilClass):
    1: - diff_file = discussion.diff_file
    2: - blob = discussion.blob
    3: - discussions = { discussion.original_line_code => [discussion] }
    4: - diff_file_class = diff_file.text? ? 'text-file' : 'js-image-file'
    5: - diff_data = {}
    6: - expanded = discussion.expanded? || local_assigns.fetch(:expanded, nil)
  lib/gitlab/diff/file.rb:54:in `line_code'
  app/models/diff_note.rb:85:in `original_line_code'
  app/models/concerns/discussion_on_diff.rb:8:in `original_line_code'
  app/views/discussions/_diff_with_notes.html.haml:3:in `_app_views_discussions__diff_with_notes_html_haml___1934583260500465353_70166110670440'
  app/views/discussions/_discussion.html.haml:52:in `_app_views_discussions__discussion_html_haml___3300946306980297002_70166168804540'
  app/views/shared/notes/_notes.html.haml:6:in `block in _app_views_shared_notes__notes_html_haml__4216686508215865599_70166119321020'
  app/views/shared/notes/_notes.html.haml:2:in `each'
  app/views/shared/notes/_notes.html.haml:2:in `_app_views_shared_notes__notes_html_haml__4216686508215865599_70166119321020'
  app/views/shared/notes/_notes_with_form.html.haml:6:in `_app_views_shared_notes__notes_with_form_html_haml__3828359774013864509_70166118991480'
  app/views/projects/merge_requests/_discussion.html.haml:11:in `_app_views_projects_merge_requests__discussion_html_haml__809004878834630127_70166118771760'
  app/views/projects/merge_requests/show.html.haml:80:in `_app_views_projects_merge_requests_show_html_haml___3096220450810035097_70166161581240'
  app/controllers/projects/merge_requests_controller.rb:52:in `block (2 levels) in show'
  app/controllers/projects/merge_requests_controller.rb:34:in `show'
  lib/gitlab/i18n.rb:50:in `with_locale'
  lib/gitlab/i18n.rb:56:in `with_user_locale'
  app/controllers/application_controller.rb:362:in `set_locale'
  lib/gitlab/middleware/multipart.rb:95: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:28:in `call'
  lib/gitlab/middleware/read_only.rb:16: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'

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
System information
System:		Ubuntu 16.04
Current User:	git
Using RVM:	no
Ruby Version:	2.4.4p296
Gem Version:	2.7.6
Bundler Version:1.16.2
Rake Version:	12.3.1
Redis Version:	3.2.11
Git Version:	2.17.1
Sidekiq Version:5.1.3
Go Version:	unknown

GitLab information Version: 11.0.3 Revision: aa62075 Directory: /opt/gitlab/embedded/service/gitlab-rails DB Adapter: postgresql URL: https://git.montoux.com HTTP Clone URL: https://git.montoux.com/some-group/some-project.git SSH Clone URL: ssh://git@git.montoux.com:34/some-group/some-project.git Using LDAP: no Using Omniauth: no

GitLab Shell Version: 7.1.4 Repository storage paths:

  • default: /var/opt/gitlab/git-data/repositories Hooks: /opt/gitlab/embedded/service/gitlab-shell/hooks Git: /opt/gitlab/embedded/bin/git

Results of GitLab application Check

Expand for output related to the GitLab application check
Checking GitLab Shell ...

GitLab Shell version >= 7.1.4 ? ... OK (7.1.4) Repo base directory exists? default... yes Repo storage directories are symlinks? default... no Repo paths owned by git:root, or git:git? default... yes Repo paths access is drwxrws---? default... yes hooks directories in repos are links: ... 2/3 ... ok 2/4 ... ok 2/5 ... ok 2/6 ... ok 6/9 ... ok 9/10 ... ok 11/11 ... ok 6/12 ... ok 13/13 ... ok 13/14 ... ok 16/16 ... ok 13/17 ... ok 13/18 ... ok 13/19 ... ok 20/21 ... ok 20/22 ... ok 20/23 ... ok 20/24 ... ok 13/35 ... ok 20/36 ... ok 20/37 ... ok 13/38 ... ok 13/39 ... ok 13/40 ... ok 2/41 ... ok 13/42 ... ok 13/43 ... ok 13/44 ... ok 13/45 ... ok 13/47 ... ok 13/48 ... ok 13/50 ... ok 13/51 ... ok 13/52 ... ok 21/53 ... ok 20/54 ... ok 20/55 ... ok 20/56 ... ok 20/57 ... ok 20/58 ... ok 13/59 ... ok 21/60 ... ok 2/61 ... ok 13/62 ... ok 13/64 ... ok 13/65 ... ok 13/67 ... ok 13/68 ... ok 13/69 ... ok 2/70 ... ok 13/71 ... ok 28/73 ... ok 28/74 ... ok 13/75 ... ok 29/76 ... ok 28/77 ... ok 13/78 ... ok 32/79 ... ok 20/80 ... ok 32/81 ... ok 32/82 ... ok 32/83 ... ok 32/84 ... ok 13/87 ... ok 13/88 ... ok 20/89 ... ok 25/90 ... repository is empty 20/91 ... ok 13/92 ... ok 13/93 ... ok Running /opt/gitlab/embedded/service/gitlab-shell/bin/check Check GitLab API access: OK Redis available via internal API: OK

Access to /var/opt/gitlab/.ssh/authorized_keys: OK gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Reply by email is disabled in config/gitlab.yml Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Git configured correctly? ... yes Database config exists? ... yes All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config up to date? ... yes Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory exists? ... yes Uploads directory has correct permissions? ... yes Uploads directory tmp has correct permissions? ... yes Init script exists? ... skipped (omnibus-gitlab has no init script) Init script up-to-date? ... skipped (omnibus-gitlab has no init script) Projects have namespace: ... 2/3 ... yes 2/4 ... yes 2/5 ... yes 2/6 ... yes 6/9 ... yes 9/10 ... yes 11/11 ... yes 6/12 ... yes 13/13 ... yes 13/14 ... yes 16/16 ... yes 13/17 ... yes 13/18 ... yes 13/19 ... yes 20/21 ... yes 20/22 ... yes 20/23 ... yes 20/24 ... yes 13/35 ... yes 20/36 ... yes 20/37 ... yes 13/38 ... yes 13/39 ... yes 13/40 ... yes 2/41 ... yes 13/42 ... yes 13/43 ... yes 13/44 ... yes 13/45 ... yes 13/47 ... yes 13/48 ... yes 13/50 ... yes 13/51 ... yes 13/52 ... yes 21/53 ... yes 20/54 ... yes 20/55 ... yes 20/56 ... yes 20/57 ... yes 20/58 ... yes 13/59 ... yes 21/60 ... yes 2/61 ... yes 13/62 ... yes 13/64 ... yes 13/65 ... yes 13/67 ... yes 13/68 ... yes 13/69 ... yes 2/70 ... yes 13/71 ... yes 28/73 ... yes 28/74 ... yes 13/75 ... yes 29/76 ... yes 28/77 ... yes 13/78 ... yes 32/79 ... yes 20/80 ... yes 32/81 ... yes 32/82 ... yes 32/83 ... yes 32/84 ... yes 13/87 ... yes 13/88 ... yes 20/89 ... yes 25/90 ... yes 20/91 ... yes 13/92 ... yes 13/93 ... yes Redis version >= 2.8.0? ... yes Ruby version >= 2.3.5 ? ... yes (2.4.4) Git version >= 2.9.5 ? ... yes (2.17.1) Git user has default SSH configuration? ... yes Active users: ... 15

Checking GitLab ... Finished

Possible fixes

Possibly relevant issues: