Skip to content

SystemStackError in method_completion_candidates due to circular alias resolution #3903

@abhiskush

Description

@abhiskush

Description

Description

Ruby LSP crashes with SystemStackError: stack level too deep when processing method completion requests in Rails projects that contain circular method aliases. The crash occurs during autocomplete (typing . after an object) and makes the LSP server completely unresponsive.

Environment

  • Ruby LSP Version: 0.26.4
  • Ruby LSP Rails Version: 0.4.8
  • Ruby Version: 3.3.3
  • Rails Version: 8.1.1
  • RuboCop Version: 1.82.1
  • Operating System: macOS
  • Editor: VS Code with Ruby LSP extension

Steps to Reproduce

  1. Create a Rails project with Ruby LSP 0.26.4 installed
  2. Open any Ruby file in VS Code
  3. Type . after any object to trigger autocomplete
  4. Ruby LSP server hangs and becomes unresponsive

Actual Behavior

  1. Ruby LSP server enters infinite recursion
  2. VS Code shows notification: "Getting code actions from 'Ruby LSP'" that runs indefinitely
  3. CPU usage spikes to 90-100% for the ruby-lsp process
  4. After ~10 seconds, crash with SystemStackError: stack level too deep
  5. LSP server becomes unresponsive and must be restarted

Error Log:

SystemStackError: stack level too deep
  from ruby_indexer/lib/ruby_indexer/entry.rb:65:in `parse_file_comments'
  from ruby_indexer/lib/ruby_indexer/entry.rb:65:in `comments'
  from ruby_indexer/lib/ruby_indexer/entry.rb:456:in `initialize'
  from ruby_indexer/lib/ruby_indexer/index.rb:1066:in `new'
  from ruby_indexer/lib/ruby_indexer/index.rb:1066:in `resolve_method_alias'
  from ruby_indexer/lib/ruby_indexer/index.rb:259:in `block in method_completion_candidates'
  from ruby_indexer/lib/ruby_indexer/index.rb:231:in `each'
  from ruby_indexer/lib/ruby_indexer/index.rb:231:in `each_with_object'
  from ruby_indexer/lib/ruby_indexer/index.rb:231:in `method_completion_candidates'
  ... 8 levels...
  from ruby_lsp/server.rb:936:in `text_document_completion'

Expected Behavior

  1. Ruby LSP should detect circular method aliases
  2. Autocomplete should work without hanging
  3. No stack overflow errors
  4. LSP server remains responsive

Root Cause Analysis

The bug occurs in the alias resolution logic within the method completion system. Here's the execution flow:

The following infinite loop occurs:

  1. method_completion_candidates (line 259) calls resolve_method_alias(entry, receiver_name, [])
  2. resolve_method_alias (line 1066) creates Entry::MethodAlias.new(target, entry)
  3. MethodAlias.initialize (line 456) calls target.comments
  4. comments method (line 65) calls Prism.parse_file_comments(path) which re-parses the file
  5. Re-parsing discovers the reverse alias, triggering another call to resolve_method_alias with empty seen_names again
  6. Loop continues infinitely until stack overflow

The Problem:

File: lib/ruby_indexer/lib/ruby_indexer/index.rb

# Line 259 - Inside method_completion_candidates
if entry.is_a?(Entry::UnresolvedMethodAlias)
  resolved_alias = resolve_method_alias(entry, receiver_name, [])
                                                              ^^
                                                        EMPTY ARRAY!
end

Workaround (For Users)

Until this is fixed, users can disable the completion feature to avoid the crash:

In .vscode/settings.json:

{
  "rubyLsp.enabledFeatures": {
    "completion": false
  }
}

This disables autocomplete but keeps other features like diagnostics, hover, and go-to-definition working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-more-infoMore information is needed to address this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions