Commit 4346ab0
Fix process-global memory leak in folded_declaration_cache
In long-lived processes (e.g., Sidekiq workers rendering emails via
Premailer), the process-global `@folded_declaration_cache` on
`CssParser::Parser` accumulates entries from every CSS parse operation
and is never cleared, causing unbounded memory growth.
The class-level `@folded_declaration_cache` (and its `attr_reader`) on
`CssParser::Parser` was defined as a class instance variable that
persists for the lifetime of the process. While `reset!` (called in
`initialize`) creates a per-instance `@folded_declaration_cache`, the
class-level variable and accessor remained, leaking the global
reference.
Similarly, `CssParser.merge` set a module-level
`@folded_declaration_cache = {}` on every call, which also persisted
at the module level.
This commit:
- Removes the class-level `@folded_declaration_cache` and its
`class << self; attr_reader` from `CssParser::Parser`
- Removes the module-level `@folded_declaration_cache` assignment
in `CssParser.merge`
- The per-instance cache (set in `reset!`) is already properly scoped
and gets garbage collected with the `Parser` instance
- Adds tests verifying the cache is per-instance and not class-level
Amp-Thread-ID: https://ampcode.com/threads/T-019c8b1f-829d-70cd-83c5-3409a635e616
Co-authored-by: Amp <amp@ampcode.com>1 parent 2ef7dca commit 4346ab0
3 files changed
Lines changed: 42 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
56 | | - | |
57 | 55 | | |
58 | 56 | | |
59 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 34 | | |
41 | 35 | | |
42 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments