Repository navigation
Fix clear cache in notifications maily digest #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ RSpec/DescribeClass: | |
| Exclude: | ||
| - spec/i18n_spec.rb | ||
| - "spec/system/**/*" | ||
| - "spec/lib/**/*" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
spec/lib/decidim/term_customizer/term_customizer_loader_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| describe "Decidim::TermCustomizer.loader" do | ||
| describe ".loader" do | ||
| it "is isolated per thread" do | ||
| loader_org1 = double("loader_org1") | ||
| loader_org2 = double("loader_org2") | ||
|
|
||
| thread1_loader = nil | ||
| thread2_loader = nil | ||
|
|
||
| t1 = Thread.new do | ||
| Decidim::TermCustomizer.loader = loader_org1 | ||
| sleep 0.05 | ||
| thread1_loader = Decidim::TermCustomizer.loader | ||
| end | ||
|
|
||
| t2 = Thread.new do | ||
| sleep 0.02 | ||
| Decidim::TermCustomizer.loader = loader_org2 | ||
| thread2_loader = Decidim::TermCustomizer.loader | ||
| end | ||
|
|
||
| t1.join | ||
| t2.join | ||
|
|
||
| expect(thread1_loader).to eq(loader_org1) | ||
| expect(thread2_loader).to eq(loader_org2) | ||
| end | ||
| end | ||
|
|
||
| describe "Decidim::TermCustomizer::I18nBackend" do | ||
| let(:backend) { Decidim::TermCustomizer::I18nBackend.new } | ||
|
|
||
| let(:translations_org1) { { en: { decidim: { test_key: "Org 1 translation" } } } } | ||
| let(:translations_org2) { { en: { decidim: { test_key: "Org 2 translation" } } } } | ||
|
|
||
| it "does not bleed translations between concurrent threads" do | ||
| loader_org1 = double("loader_org1", translations_hash: translations_org1) | ||
| loader_org2 = double("loader_org2", translations_hash: translations_org2) | ||
|
|
||
| result_thread1 = nil | ||
| result_thread2 = nil | ||
|
|
||
| t1 = Thread.new do | ||
| Decidim::TermCustomizer.loader = loader_org1 | ||
| sleep 0.05 | ||
| result_thread1 = backend.translations | ||
| end | ||
|
|
||
| t2 = Thread.new do | ||
| sleep 0.02 | ||
| Decidim::TermCustomizer.loader = loader_org2 | ||
| result_thread2 = backend.translations | ||
| end | ||
|
|
||
| t1.join | ||
| t2.join | ||
|
|
||
| expect(result_thread1).to eq(translations_org1) | ||
| expect(result_thread2).to eq(translations_org2) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Thread pooling can leak loaders between requests.
In production web servers with thread pools (Puma, Passenger), threads are reused across requests. Thread-local variables persist for the thread's lifetime, so the loader from one request can leak into subsequent requests on the same thread if the engine hook doesn't fire or fails.
Add explicit cleanup at request completion to prevent cross-request contamination:
Additionally, consider defensive nil handling in callers, as the loader may be nil in background jobs, console, or tests.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about after an active_job, should we have some similar defensive action ('perform_start.active_job')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 581
🏁 Script executed:
Length of output: 16025
@microstudiYes, adding cleanup after job execution is a good defensive measure. A couple of points worth noting:perform.active_job(the finish event), notperform_start.active_job(which is already used to set the loader). The engine already does:The cleanup counterpart would be:
Severity is lower than for web requests — because
perform_start.active_jobalready overwrites the loader at the beginning of every job, a stale loader from a previous job will be replaced before any translations are looked up. The residual risk is more about code that runs outside a job on a pooled thread (e.g., a fiber or spawned thread inside a job, or the thread being used in an unexpected context after the job finishes).Controller cleanup symmetry — since the same pattern is recommended for
process_action.action_controller, doing the same forperform.active_jobkeeps the two execution contexts consistent and makes the thread-local lifecycle explicit and bounded.So yes, it's worth adding the
perform.active_jobcleanup subscriber alongside the existingperform_start.active_jobsubscriber inengine.rb.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seem like a very unlikely case. Why is it different the case for active job? even with the web case, isn't the event "process_action.action_controller" going to be executed every new requests? independently of the thread processing it, so the loader is restarted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@microstudiYou're right, and the two cases are symmetric. My original "critical" label was overstated.Here's the accurate picture:
start_processing.action_controller(or similar) subscriber already sets a fresh loader at the beginning of every request. So when thread A picks up request N+1, the start hook fires first and overwrites whatever stale loader was left by request N — before any translation lookup can happen. No observable leak.perform_start.active_jobalready overwrites the loader at job start. A stale loader from a previous job on the same thread is replaced before any code in the new job sees it.So the
process_action.action_controllerandperform.active_jobcleanup hooks this PR adds are defensive hygiene, not a critical safety requirement. Their main benefits are:The leakage scenario would only matter if a translation lookup happened on a thread between the old request's teardown and the new request's start hook — which doesn't occur in normal Puma/Sidekiq usage.
Apologies for flagging this as critical; it's a nice-to-have cleanup, not a required fix.