Skip to content

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

Closed
cmb69 wants to merge 2 commits into
php:masterfrom
cmb69:cmb/gh9067
Closed

cmb69 wants to merge 2 commits into
php:masterfrom
cmb69:cmb/gh9067

Conversation

@cmb69

@cmb69 cmb69 commented Jul 20, 2022

Copy link
Copy Markdown
Member

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
Comment thread ext/random/random.c Resolved
@cmb69
cmb69 requested review from TimWolla and zeriyoshi July 20, 2022 17:47
@TimWolla

TimWolla commented Jul 20, 2022 •

Copy link
Copy Markdown
Member

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

cmb69 commented Jul 20, 2022

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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?

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

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.

Comment thread ext/random/random.c Resolved
@cmb69

cmb69 commented Jul 21, 2022

Copy link
Copy Markdown
Member Author

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