Skip to content
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

Test2::Plugin::SRand breaks rand under fork. #935

Open
demerphq opened this issue May 3, 2023 · 1 comment
Open

Test2::Plugin::SRand breaks rand under fork. #935

demerphq opened this issue May 3, 2023 · 1 comment

Comments

@demerphq
Copy link
Contributor

demerphq commented May 3, 2023

Test2::Plugin::SRand assumes that it can set srand() to get deterministic testing. This breaks the behavior of rand() and fork(). As of 5.37.x there is a new env var which should be used instead PERL_RAND_SEED, which behaves correctly with fork.

$ PERL_RAND_SEED=1234 ./perl -le'fork; print rand() for 1..3;' | sort
0.21453485716799
0.338195834114355
0.378420907809538
0.740876929442496
0.839252316250594
0.842944087068648
$ PERL_RAND_SEED=1234 ./perl -le'fork; print rand() for 1..3;' | sort
0.21453485716799
0.338195834114355
0.378420907809538
0.740876929442496
0.839252316250594
0.842944087068648

versus this:

$ ./perl -le'srand(1234); fork; print rand() for 1..3;' | sort
0.21453485716799
0.21453485716799
0.338195834114355
0.338195834114355
0.740876929442496
0.740876929442496
$ ./perl -le'srand(1234); fork; print rand() for 1..3;' | sort
0.21453485716799
0.21453485716799
0.338195834114355
0.338195834114355
0.740876929442496
0.740876929442496

notice that calling srand() explicitly results in the child processes getting the same srand() state, whereas using PERL_RAND_SEED is deterministic but ensures each child process has its own seed.

I'd say that Test2::Plugin::SRand breaks anything that uses fork and rand() which does not itself use srand() explicitly.

@exodist
Copy link
Member

exodist commented May 22, 2023

The "broken" behavior is exactly as expected and if I am not mistaken documented. It may not be ideal in 5.37.x and above, and that should be documented as well now.

Apart from documenting the "better" solution, what fix would you propose?

@exodist exodist transferred this issue from Test-More/Test2-Suite Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants