Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threadsafe usage #51

Open
tombeynon opened this issue Jun 9, 2020 · 4 comments
Open

Threadsafe usage #51

tombeynon opened this issue Jun 9, 2020 · 4 comments

Comments

@tombeynon
Copy link

We were having a lot of issues with this gem causing hanging behaviour in a multi-threaded environment. V8 is the downstream cause of this behaviour.

We looked at #46 to try to solve it, but that PR exhibited similar issues.

We ended up solving by using a Singleton class to manage a mutex whenever you want to compile a template. An example below in case anyone else hits the same problem:

HandlebarsCompiler.instance.compile(layout_path, content_path, data)
class HandlebarsCompiler
  include Singleton

  def initialize
    @mutex = Mutex.new
  end

  def compile(layout_path, content_path, data)
    @mutex.synchronize do
      content_template = File.read(content_path)
      handlebars = Handlebars::Context.new
      handlebars.register_partial('content', content_template)
      html = handlebars.compile(File.read(layout_path))
      html.call(data)
    end
  end
end
@omenking
Copy link

Attempting this implementation I get:

V8::Error (deadlock; recursive locking):

I suppose I'll have to read up more on the ruby threading. 🤷‍♂️

I've been impacted by this issue for a few years now.

@omenking
Copy link

askcharlie has your implementation. I'll give it a go.

https://github.com/askcharlie/handlebars.rb/commit/f8259b4770f74fdcd3e3d21b7fc492b6b1380130

@omenking
Copy link

Revisiting this problem, still, hangs. Its a bummer a pure ruby implementation doesn't match the syntax found in this gem.

@katpadi
Copy link

katpadi commented Jun 26, 2021

We were having a lot of issues with this gem causing hanging behaviour in a multi-threaded environment. V8 is the downstream cause of this behaviour.

We looked at #46 to try to solve it, but that PR exhibited similar issues.

We ended up solving by using a Singleton class to manage a mutex whenever you want to compile a template. An example below in case anyone else hits the same problem:

HandlebarsCompiler.instance.compile(layout_path, content_path, data)
class HandlebarsCompiler
  include Singleton

  def initialize
    @mutex = Mutex.new
  end

  def compile(layout_path, content_path, data)
    @mutex.synchronize do
      content_template = File.read(content_path)
      handlebars = Handlebars::Context.new
      handlebars.register_partial('content', content_template)
      html = handlebars.compile(File.read(layout_path))
      html.call(data)
    end
  end
end

This looks like it's working. However, it seems like there's some memory leak when we're doing it this way as we are registering some handlebar methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants