Repository navigation
[MOD-17631] Add safe global_config crate - #10915
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 39f00656e3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| pub fn get() -> ffi::RSConfig { | ||
| // SAFETY: `RSGlobalConfig` is a statically initialised C global that lives for the whole | ||
| // process, and `RSConfig` is `Copy`, so the value is read out without forming a reference. | ||
| unsafe { ffi::RSGlobalConfig } |
There was a problem hiding this comment.
Avoid whole-config unsynchronized reads
Because this copies the entire C RSConfig, every global_config::get().numericCompress/maxAggregateResults call now also performs an unsynchronized read of pointer fields such as defaultScorer. If a worker thread is building a numeric/aggregate iterator while another client runs FT.CONFIG SET DEFAULT_SCORER, set_default_scorer_config can free and rewrite that pointer while this safe function is reading it, expanding the previous field-specific race into unrelated settings and making safe Rust able to hit undefined behavior. Please keep the read scoped to the requested field, or lock/atomically snapshot the config instead of copying the whole struct.
AGENTS.md reference: AGENTS.md:L294-L295
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I agree with Codex here. Two things:
- We're unnecessarily copying the config here (Probably to prevent a data race?)
- But a data race can still happen when a mutable global static is accessed (or copied) without synchronization.
The C code is/was already UB, but we're carrying the UB over to safe Rust now as well. Safe Rust code should not contain any UB, else the premise of Rust goes out the window. I'm not sure what would be the best way to solve this, because this would likely mean ensuring the mutations on the C side should be fixed as well.
This problem comes up now much more than before now that we interface with existing C code more often (as I do with the Terms and Dict wrappers as well). Getting safe sound UB free abstractions around existing C code is nearly impossible. This is the annoying part of interfacing with UB filled C code in Rust and sadly I don't have the answer.
There was a problem hiding this comment.
I hear you. Would it solve anything with individual fields exposed instead of the entire config?
What I'm trying to do is just get rid of all the unsafe blocks reading config in different crates. Their safety comments greatly vary, as you can see in the diff tab. But I'm certainly open to the possibility that this isn't really possible.
There was a problem hiding this comment.
At least I think the copying doesn't add anything and returning a lifetimed ref would work as well, as they would have the same UB. Albeit a lifetimed ref could probably have a larger window for a data race to occur, in it's current use the window is still small.
There was a problem hiding this comment.
Changed the PR to just return individual fields instead.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #10915 +/- ##
==========================================
- Coverage 83.17% 82.19% -0.99%
==========================================
Files 611 636 +25
Lines 81136 82247 +1111
Branches 36374 37485 +1111
==========================================
+ Hits 67485 67599 +114
- Misses 13304 14301 +997
Partials 347 347
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bb7d69a702
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| pub fn $name() -> $ty { | ||
| // SAFETY: see this module's *Safety of these reads*. | ||
| unsafe { ffi::RSGlobalConfig.$field } |
There was a problem hiding this comment.
Do not mark racy config loads safe
Fresh evidence after the earlier whole-struct comment: this version removed the struct copy, but each new accessor is still a safe function that performs a non-atomic load from RSGlobalConfig. When a query/background worker calls numeric_compress() or default_scorer() while another client runs FT.CONFIG SET for the same field, the C setter writes the same storage concurrently; safe Rust code can then trigger undefined behavior from a data race. Please make these accessors unsafe with the race as a caller precondition, or route the fields through synchronization/atomics before exposing a safe API.
AGENTS.md reference: AGENTS.md:L294-L295
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
While I agree it's not perfect, it's not worse than what we currently have. It's basically a collecting of all the related unsafe blocks into one crate. As per the slack thread, it seems difficult to achieve anything better without redoing things on the C side.
There was a problem hiding this comment.
I agree with you.
Maybe we should have a 'safe, with known UB' tag on those methods :) But that is for the safety discussion.
|
Would you look at the scorer-related changes? @gdesmott |
daxhuiberts
left a comment
There was a problem hiding this comment.
One minor comment addition I think is worth adding.
| //! This crate exists to replace the C readers of the same fields rather than to change what | ||
| //! they do; closing the race needs the C side to store the fields atomically. |
There was a problem hiding this comment.
'... needs the C side to store the fields atomically' ...and both the C and Rust side to load them atomically.
|
I'm going to need |
Added in separately in #10924 just to try the stacked PR. 😆 |
Rust code that wants a RediSearch setting has to read the RSGlobalConfig extern static, which is unsafe at every call site. Six such reads had grown across four crates, each carrying its own safety argument, and those arguments disagreed with src/config.c: numericCompress, prioritizeIntersectUnionChildren and maxAggregateResults are all registered without REDISMODULE_CONFIG_IMMUTABLE, so CONFIG SET can change them while queries run, yet the comments claimed the fields were fixed at load time. The new crate holds the read in one place. It hands back RSConfig by value rather than a reference, because a &'static RSConfig would assert that nothing writes the static while it lives, which CONFIG SET violates from client threads. The copy is a plain unsynchronised load, matching what the C readers of the same fields do; it can tear against a concurrent write, and removing that race needs the C side to store the fields atomically. Returning the whole struct also lets a caller that needs several settings take one snapshot instead of one read per field, so an operation cannot observe a setting changing halfway through. query_eval_ffi::eval_config does exactly that. Co-Authored-By: Claude Opus 5 <[email protected]>
Review of the wrapper turned up three claims that overstate what a by-value snapshot buys. `get`'s SAFETY comment established liveness and the absence of a reference but not the third precondition for reading a `static mut`, that no other thread writes it — which is exactly the one that does not hold, as the doc comment directly above it already said. The comment now names the race instead of leaving the two in contradiction. For the same reason a snapshot cannot make an operation immune to a concurrent `CONFIG SET`: the struct copy is not atomic, so it narrows the window rather than closing it. `eval_config` additionally called itself the single point where the evaluator reads the global config, which `resolve_scorer` disproves two functions further down. Also mark `get` `#[inline]`, so that a caller wanting one field does not pay for an out-of-line call returning the whole struct in builds without LTO. Co-Authored-By: Claude Opus 5 <[email protected]>
`global_config::get()` returned the whole `RSConfig`. The copy is not atomic, so it did not make the fields it returned agree with one another, while copying a large struct and handing back owned pointers no caller had asked for — among them `defaultScorer`, which `FT.CONFIG SET DEFAULT_SCORER` frees. Replace it with one accessor per field, generated from a macro so a new field cannot be added without the shared safety argument covering it. `default_scorer` is written out rather than generated: it returns `Option<NonNull<c_char>>`, putting the unset case in the type instead of leaving every caller to null-check, and its doc carries the hazard no pointer type can express — the string can be freed while a caller still holds it. Co-Authored-By: Claude Opus 5 <[email protected]>
07c3561 to
611be9e
Compare
|
Looks good to me! |
|
@daxhuiberts If you're happy as well please approve. |



Describe the changes in the pull request
RSGlobalConfigextern static directly, which isunsafeat every call site. Ten such reads had grown across four crates, each carrying its own safety argument — and several of those arguments contradictedsrc/config.c, claiming fields were fixed at load time whenCONFIG SETcan in fact change them while queries run.global_configcrate owns the read, exposing one accessor per setting. The accessors are generated from a macro, so a new field inherits the shared safety argument instead of acquiring its own.Which additional issues this PR fixes
Main objects this PR modified
global_config— new crate inc_wrappers/, one accessor perRSGlobalConfigfield.query_eval_ffi::eval_config— was three separate unsafe reads, now resolves the settings an evaluation needs into its own snapshot.iterators_ffi— intersection and the geo/numeric inverted-index readers.reducers,redis_mock— one read each, migrated.Mark if applicable
Release Notes
Worth a reviewer's attention
There is deliberately no whole-struct accessor. Returning
RSConfigby value looks like it buys inter-field consistency, but the copy is not atomic, so it never did — and it hands every caller an owneddefaultScorerpointer thatCONFIG SET DEFAULT_SCORERcan free, whether or not the caller wanted that field. An operation that needs several settings to agree resolves them into its own snapshot up front, which is whateval_configdoes.The reads are unsynchronised, matching what the C readers of the same fields already do: a read can race with a
CONFIG SETwrite, and closing that needs the C side to store the fields atomically. The accessors are safe rather thanunsafeon that basis — they change no C behavior, and marking themunsafewould state a precondition no caller can uphold. The module docs say this outright rather than claiming a guarantee that does not hold.default_scoreris the one accessor written out by hand rather than generated. It returnsOption<NonNull<c_char>>, putting the unset case in the type instead of leaving every caller to null-check, and its doc carries the hazard no pointer type can express: the string can be freed while a caller still holds the pointer, so a caller that needs the name must copy it.