Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gevent): run after-in-child hooks after reinit (#4070)
## Description Threads created too early in an application that uses gevent end up not running when the gevent hub reinit is executed after fork in the child process. This change ensures that we trigger the after-in-child fork hooks after the call to `gevent.hub.reinit` to ensure that threads created at any time will run as expected after fork. ### More details In its implementation, `gevent` wraps around `os.fork` and calls `gevent.hub.reinit` to re-initialise the state of the child process after fork. The `forksafe` hooks that are registered by the library end up running after the call to the OS `fork` syscall, but *before* `gevent.hub.reinit`, which causes the threads to not work as expected in the child process. Other frameworks, like `gunicorn` make their own call to `os.fork` and then call `gevent.hub.reinit` in the child process to obtain the same effect. This poses the same problem as in plain `gevent`. With this change, we register an import hook on `gevent.hub` to detect the possibility that `gevent.hub.reinit` might be called, and we patch this function to re-run the after-in-child fork hook to ensure that they run *after* the gevent hub has been reinitialised fully in the child process after fork. In particular, this ensures that threads are recreated at the right time and can then work as expected in child processes. ## Checklist - [x] Title must conform to [conventional commit](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional). - [x] Add additional sections for `feat` and `fix` pull requests. - [x] Ensure tests are passing for affected code. - [x] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] PR cannot be broken up into smaller PRs. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added for fixes and features, or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Backports are identified and tagged with Mergifyio. - [ ] Add to milestone.
- Loading branch information