Skip to content

Fix GH-9067: random extension is not thread safe #9070

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

Closed
wants to merge 2 commits into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Jul 20, 2022

For thread-safety, we need to initialize global variables in GINIT (or
RINIT), but not in MINIT.

For thread-safety, we need to initialize global variables in GINIT (or
RINIT), but not in MINIT.
@cmb69 cmb69 linked an issue Jul 20, 2022 that may be closed by this pull request
@cmb69 cmb69 requested review from zeriyoshi and TimWolla July 20, 2022 17:47
@TimWolla
Copy link
Member

TimWolla commented Jul 20, 2022

Thank you for tackling the issue. As this part of the engine is not something I have interacted with before: Do you have a reference of when example the various "callbacks" in question are executed?

Is MINIT once and GINIT once per thread?

@cmb69
Copy link
Member Author

cmb69 commented Jul 20, 2022

Is MINIT once and GINIT once per thread?

Indeed. See https://www.phpinternalsbook.com/php7/extensions_design/php_lifecycle.html?highlight=lifecycle#globals-initialization-ginit for details.

@TimWolla
Copy link
Member

Thanks, I have read through that and also through the explanation for globals.

I'm a little confused, because according to the link the GINIT runs before MINIT. I would have expected the reverse order (first MINIT then GINIT). But if I understand the issue and inner workings correctly:

  • random_globals is a thread-local variable
  • Because MINIT() only runs once (likely for the very first thread?), the state in random_globals will not be correctly set up for any additional thread?

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my understanding of the PHP engine this is the correct fix. Each thread should have its own copy of the state.

@cmb69
Copy link
Member Author

cmb69 commented Jul 21, 2022

random_globals is a thread-local variable

Basically, yes. (Actually, it is a parameter which is an alias to the thread locals; elsewhere they're access via RANDOM_G.)

  • Because MINIT() only runs once (likely for the very first thread?), the state in random_globals will not be correctly set up for any additional thread?

Right. MINIT only runs for the very first thread, so the thread locals of other threads need to be initialized elsewhere (GINIT or RINIT).

ext/random cannot be built shared, so `COMPILE_DL_RANDOM` is never
defined.
@cmb69 cmb69 closed this in 8487d8f Jul 21, 2022
@cmb69 cmb69 deleted the cmb/gh9067 branch July 21, 2022 10:54
TimWolla added a commit to TimWolla/php-src that referenced this pull request Jul 23, 2022
TimWolla added a commit that referenced this pull request Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

random extension is not thread safe
3 participants