wkirschbaum / elixir-ts-mode

Elixir mode using Treesitter for fontification, navigation and indentation
GNU General Public License v3.0
60 stars 11 forks source link

emacs hangs and memory leaks with some specific heex codes #43

Open sezaru opened 8 months ago

sezaru commented 8 months ago

I've noticed that when I use elixir-ts-mode with elixir and heex tree-sitters with emacs, sometimes it will hang and start consuming all the system memory.

I was able to create an elixir file that is easy to reproduce the issue.

To reproduce it, first create an elixir file with the following code:

defmodule CoreWeb.Components.Feedback.Create.Stepper do
  def conditional_stepper_item(assigns) do
    ~H"""
    <.async_result :let={value} assign={@result}>
      <:loading>

      </:loading>
    </.async_result>
    """
  end

  def selected_stepper_item(assigns) do
    ~H"""
    <li>
      <span>
        <Icons.from_name name={@icon} solid class="w-3.5 h-3.5 text-gray-500 dark:text-gray-400" />
      </span>
      <h3 class="font-medium leading-tight"><%= @title %></h3>
      <p class="text-sm truncate"><%= @description %></p>
    </li>
    """
  end

  attr :title, :string, required: true
  attr :description, :string, required: true

  attr :icon, :atom, required: true

  attr :event, :string, default: nil

  attr :target, CID, required: true

  def stepper_item(assigns) do
    ~H"""
    <li class="mb-10 ps-6 pr-2 hover:bg-gray-200 rounded-lg cursor-pointer" phx-click={@event} phx-target={@target}>
      <span class="absolute flex items-center justify-center w-8 h-8 bg-gray-100 rounded-full -start-4 ring-4 ring-white dark:ring-gray-900 dark:bg-gray-700">
        <Icons.from_name name={@icon} solid class="w-3.5 h-3.5 text-gray-500 dark:text-gray-400" />
      </span>
      <h3 class="font-medium leading-tight"><%= @title %></h3>
      <p class="text-sm truncate"><%= @description %></p>
    </li>
    """
  end
end

Now, go to line 6 (right between the <:loading></:loading> tags and type (character by character) <.some_component a={.

When you reach the { character, emacs will freeze and it will start consuming all the RAM memory available until it crashes with OOM.

I also tested this with the latest emacs 30 git commit and I can reproduce the same issue.

wkirschbaum commented 8 months ago

Thanks for the report. This sounds like a treesitter issue, but will investigate on my side and report upstream if I can reproduce.

quartz55 commented 6 months ago

I've been occasionally getting the same symptoms but wasn't able to pin point what part was causing it other than it was while editing Elixir related files. I can reproduce @sezaru 's example on my machine every time: macOS 14.4, emacs 30.0.50 (w/ doomemacs). I can also try with minimal configuration using emacs -Q if you would find that useful

ssbb commented 3 months ago

I have exactly the same issue with some of the files in my projects. It does not happen with other treesitter-based editors like nvim or zed (I am using this editors to work with such files because it literally impossible to get work done this way). And it does not happen in other Treesitter modes like Python or TS.

Also I can reproduce with clean emacs -Q.

Sounds like something in Emacs/Treesitter integration?

ssbb commented 3 weeks ago

@casouri tracked it down to tree-sitter itself, so you can follow this issue: https://github.com/tree-sitter/tree-sitter/issues/3620