@@ -22,10 +22,10 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
22
22
///
23
23
/// - On a manual call to [`reseed()`].
24
24
/// - After `clone()`, the clone will be reseeded on first use.
25
- /// - After a process is forked, the RNG in the child process is reseeded within
26
- /// the next few generated values, depending on the block size of the
27
- /// underlying PRNG. For ChaCha and Hc128 this is a maximum of
28
- /// 15 `u32` values before reseeding.
25
+ /// - When a process is forked on UNIX , the RNGs in both the parent and child
26
+ /// processes will be reseeded just before the next call to
27
+ /// [`BlockRngCore::generate`], i.e. "soon". For ChaCha and Hc128 this is a
28
+ /// maximum of fifteen `u32` values before reseeding.
29
29
/// - After the PRNG has generated a configurable number of random bytes.
30
30
///
31
31
/// # When should reseeding after a fixed number of generated bytes be used?
@@ -43,6 +43,12 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
43
43
/// Use [`ReseedingRng::new`] with a `threshold` of `0` to disable reseeding
44
44
/// after a fixed number of generated bytes.
45
45
///
46
+ /// # Limitations
47
+ ///
48
+ /// It is recommended that a `ReseedingRng` (including `ThreadRng`) not be used
49
+ /// from a fork handler.
50
+ /// Use `OsRng` or `getrandom`, or defer your use of the RNG until later.
51
+ ///
46
52
/// # Error handling
47
53
///
48
54
/// Although unlikely, reseeding the wrapped PRNG can fail. `ReseedingRng` will
@@ -310,8 +316,16 @@ mod fork {
310
316
311
317
pub fn register_fork_handler ( ) {
312
318
static REGISTER : Once = Once :: new ( ) ;
313
- REGISTER . call_once ( || unsafe {
314
- libc:: pthread_atfork ( None , None , Some ( fork_handler) ) ;
319
+ REGISTER . call_once ( || {
320
+ // Bump the counter before and after forking (see #1169):
321
+ let ret = unsafe { libc:: pthread_atfork (
322
+ Some ( fork_handler) ,
323
+ Some ( fork_handler) ,
324
+ Some ( fork_handler) ,
325
+ ) } ;
326
+ if ret != 0 {
327
+ panic ! ( "libc::pthread_atfork failed with code {}" , ret) ;
328
+ }
315
329
} ) ;
316
330
}
317
331
}
0 commit comments