diff --git a/lib/haml/filters.rb b/lib/haml/filters.rb index 07562627..92920868 100644 --- a/lib/haml/filters.rb +++ b/lib/haml/filters.rb @@ -263,6 +263,9 @@ RUBY # available if the {file:REFERENCE.md#suppress_eval-option `:suppress_eval`} # option is set to true. The Ruby code is evaluated in the same context as # the Haml template. + # + # The code in the filter has an exclusive lock (using `Thread.exlusive`) when + # it runs, to prevent issues from sharing the global `$stdout`. module Ruby include Base require 'stringio' @@ -272,11 +275,16 @@ RUBY return if compiler.options[:suppress_eval] compiler.instance_eval do push_silent <<-FIRST.gsub("\n", ';') + text + <<-LAST.gsub("\n", ';') - _haml_old_stdout = $stdout - $stdout = StringIO.new(_hamlout.buffer, 'a') + Thread.exclusive do + begin + _haml_old_stdout = $stdout + $stdout = StringIO.new(_hamlout.buffer, 'a') FIRST - _haml_old_stdout, $stdout = $stdout, _haml_old_stdout - _haml_old_stdout.close + ensure + _haml_old_stdout, $stdout = $stdout, _haml_old_stdout + _haml_old_stdout.close + end + end LAST end end