Add thread-safety to HostMatcher and Tree insertion operations#44
Add thread-safety to HostMatcher and Tree insertion operations#44anfragment merged 5 commits intomasterfrom
Conversation
WalkthroughThe PR addresses thread-safety issues in HostMatcher and Tree by relocating hostmatch to an internal package, adding mutex synchronization for concurrent slice access, and updating all import paths to reflect this structural change. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@spellcascade Publishing v1.3.0 after we merge this. |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/ruletree/ruletree.go (1)
14-18:⚠️ Potential issue | 🟡 MinorDoc comment is inconsistent with PR objectives.
The comment states "Insert and Compact must not run concurrently with Get", but the PR objectives from issue
#41aim to make these operations safe for concurrent use. If the intention is to keep this limitation, the PR description should be updated. Otherwise, the implementation needs to guardGet()as well.
🤖 Fix all issues with AI agents
In `@internal/ruletree/ruletree.go`:
- Around line 40-44: The code has a data race because Insert appends to
t.generic under insertMu while Get reads t.generic without locking; change
insertMu from sync.Mutex to sync.RWMutex (or add a new RWMutex protecting
t.generic) and update Insert to use the write lock and Get to use a read lock
when accessing t.generic (i.e., call RLock()/RUnlock() around the slice read in
Get and Lock()/Unlock() in Insert) so concurrent Get() calls can safely read
while Insert() writes are exclusive.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
internal/asset/cosmetic/injector.gointernal/asset/cssrule/injector.gointernal/asset/extendedcss/injector.gointernal/asset/jsrule/injector.gointernal/asset/scriptlet/injector.gointernal/hostmatch/hostmatcher.gointernal/hostmatch/hostmatcher_test.gointernal/hostmatch/triestore.gointernal/ruletree/ruletree.go
🧰 Additional context used
🧬 Code graph analysis (1)
internal/hostmatch/hostmatcher_test.go (1)
internal/hostmatch/hostmatcher.go (1)
NewHostMatcher(29-34)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Lint Go (windows-latest)
🔇 Additional comments (11)
internal/hostmatch/hostmatcher.go (3)
54-56: LGTM! Wildcard exception handling aligns with primary rules.The automatic wildcard prefixing for exception patterns mirrors the behavior for primary rules (lines 61-63), ensuring consistent subdomain matching. This addresses the correctness fix mentioned in the PR objectives.
69-90: LGTM!The locking for
genericExceptionsis correctly implemented, and the wildcard prefixing ensures consistent subdomain matching behavior.
92-97: No issue:trieStorehas its own internalsync.RWMutex(defined at line 54 of triestore.go). BothAdd()andGet()methods properly acquire locks, so allprimaryStoreandexceptionStoreoperations are thread-safe. ThemuinHostMatcheris correctly scoped to protect only thegenericandgenericExceptionsslices. There is no data race.internal/asset/cosmetic/injector.go (1)
10-10: LGTM!Import path correctly updated to the internal package location.
internal/asset/extendedcss/injector.go (1)
13-13: LGTM!Import path correctly updated to the internal package location.
internal/asset/jsrule/injector.go (1)
9-9: LGTM!Import path correctly updated to the internal package location.
internal/asset/scriptlet/injector.go (1)
10-10: LGTM!Import path correctly updated to the internal package location.
internal/asset/cssrule/injector.go (1)
10-10: LGTM!Import path correctly updated to the internal package location.
internal/hostmatch/hostmatcher_test.go (3)
6-6: LGTM!Import path correctly updated to reflect the package relocation to
internal/hostmatch.
126-140: LGTM!This test correctly validates the correctness fix for generic exceptions. It ensures that a generic exception rule (empty pattern) properly neutralizes a generic primary rule, which directly addresses the bug mentioned in the PR objectives.
142-153: LGTM!Good addition testing that tilde exceptions propagate correctly to deeper subdomain hierarchies. This complements the existing test at lines 97-111 which only tests direct subdomains and siblings.
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
What does this PR do?
HostMatcherandTreeslices with mutexesHostMatchercorrectness by appending generic exceptions to the correct slicehostmatcherpackageinternal, as it should not be used publiclyHow did you verify your code works?
New and existing unit tests.
What are the relevant issues?
Closes #41