Skip to content

Commit a2a870a

Browse files
authored
Merge pull request rails#51447 from Shopify/backtrace-cleaner-dup
Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone
2 parents 8e46af8 + cc0f0f9 commit a2a870a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone
2+
3+
Previously the copy would still share the internal silencers and filters array,
4+
causing state to leak.
5+
6+
*Jean Boussier*
7+
18
* Updating Astana with Western Kazakhstan TZInfo identifier
29

310
*Damian Nelson*

activesupport/lib/active_support/backtrace_cleaner.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def remove_filters!
110110
private
111111
FORMATTED_GEMS_PATTERN = /\A[^\/]+ \([\w.]+\) /
112112

113+
def initialize_copy(_other)
114+
@filters = @filters.dup
115+
@silencers = @silencers.dup
116+
end
117+
113118
def add_gem_filter
114119
gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) }
115120
return if gems_paths.empty?

activesupport/test/clean_backtrace_test.rb renamed to activesupport/test/backtrace_cleaner_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def setup
2222
test "backtrace should contain unaltered lines if they don't match a filter" do
2323
assert_equal "/my/other_prefix/my/class.rb", @bc.clean([ "/my/other_prefix/my/class.rb" ]).first
2424
end
25+
26+
test "#dup also copy filters" do
27+
copy = @bc.dup
28+
@bc.add_filter { |line| line.gsub("/other/prefix/", "") }
29+
30+
assert_equal "my/class.rb", @bc.clean(["/other/prefix/my/class.rb"]).first
31+
assert_equal "/other/prefix/my/class.rb", copy.clean(["/other/prefix/my/class.rb"]).first
32+
end
2533
end
2634

2735
class BacktraceCleanerSilencerTest < ActiveSupport::TestCase
@@ -40,6 +48,14 @@ def setup
4048
@bc.remove_silencers!
4149
assert_equal ["/mongrel/stuff.rb"], @bc.clean(["/mongrel/stuff.rb"])
4250
end
51+
52+
test "#dup also copy silencers" do
53+
copy = @bc.dup
54+
55+
@bc.add_silencer { |line| line.include?("puma") }
56+
assert_equal [], @bc.clean(["/puma/stuff.rb"])
57+
assert_equal ["/puma/stuff.rb"], copy.clean(["/puma/stuff.rb"])
58+
end
4359
end
4460

4561
class BacktraceCleanerMultipleSilencersTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)