leaf icon indicating copy to clipboard operation
leaf copied to clipboard

Remove `getenv()` which is not thread safe

Open myteril opened this issue 1 year ago • 3 comments

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [ ] Feature
  • [ ] Code style update
  • [ ] Refactor
  • [ ] Build-related changes
  • [x] Other, please describe below

Description

putenv() and getenv() functions are not thread-safe. You can read the warning directly from vlucas/phpdotenv here: vlucas/phpdotenv#putenv-and-getenv

These functions can easily break the core functionality of web sites even under a little stress. In the pull request, I have removed getenv() from _env(). I ran the tests and all have been passed. You can check it from the screenshot below:

image

However, some Leaf modules (such as db) still use getenv() to get the environment variables. This pull request will affect them and break their functionality. Before merging, getenv should be replaced with _env in these modules.

Does this PR introduce a breaking change?

  • [x] Yes
  • [ ] No

myteril avatar Oct 01 '24 11:10 myteril

@myteril can you check on the tests, thanks

mychidarko avatar Oct 01 '24 17:10 mychidarko

I checked the tests. I think the failed test case is useless after this change and needs to be removed. By this pull request, _env will use the information from .env files only, not all environment variables including system's (except the ones defined in $_ENV). It does not make any sense to compare its output with the output of getenv which gets information directly from system's environment variables.

myteril avatar Oct 02 '24 14:10 myteril

@myteril noted. I'll keep this open in the mean time. I've seen countless folks who heavily on getenv/setenv in their code, suddenly switching away from this may probably have the same impact it did on the tests. I'll like to get more info on how severely it will impact others before I merge this

mychidarko avatar Oct 15 '24 01:10 mychidarko

I am closing this for now, but will revisit it in a later version

mychidarko avatar Dec 07 '24 08:12 mychidarko