Closed stoeckmann closed 8 months ago
The reason why try_with_RNG
is used here because it isn't only possible for the TLS access to fail if it's accessed concurrently. It's also possible to fail if it's accessed during a destructor, because the destructor might be run after TLS is deallocated. I don't want Rng::new
to panic during a destructor.
Ah, thanks for clarification!
On a second thought ...
Could you clarify how a concurrent TLS access can fail, given that it's a TLS so only one thread can access RNG
since it's different for every single thread? I know that it could happen if the same thread accesses RNG
twice, i.e. within a with_rng
call, but this does not occur here and the function is not public; not even to the crate.
If you worry about a panic during TLS deallocation, isn't this also true for destructors which call bool()
or any other function which effectively call with_rng
?
- Could you clarify how a concurrent TLS access can fail, given that it's a TLS so only one thread can access
RNG
since it's different for every single thread? I know that it could happen if the same thread accessesRNG
twice, i.e. within awith_rng
call, but this does not occur here and the function is not public; not even to the crate.
Sorry, I forgot to clarify, concurrent TLS access cannot fail. Only TLS deallocation can fail.
- If you worry about a panic during TLS deallocation, isn't this also true for destructors which call
bool()
or any other function which effectively callwith_rng
?
Because there really isn't anything else we can do. We can fall back to a fixed insecure seed for an RNG, but we can't return a fixed value for the other values.
Okay, that makes sense. I have compared the existing solution with other crates just to get a better understanding of the matter.
At first I was about to recommend a documentation adjustment, but then I think it's good as it is.
Thanks again for your feedback!
The local function
try_with_rng
in global_rng can be removed, because it is not possible to access the thread local storageRNG
twice with given functions.Based on my understanding of the code, this would require any given function to access
RNG
whileRNG
is in use. Since this would affect every otherwith_rng
call as well, this seems not possible.The
try_with
usage exists since initial commit. My idea of this PR is to simplify the code for reviewers, because it took quite some time for me to figure out that this fixed seed usage is never reached.At least if I'm correct. It would be great if someone could review.