<!-- Kamailio Pull Request Template -->
<!-- IMPORTANT: - for detailed contributing guidelines, read: https://github.com/kamailio/kamailio/blob/master/.github/CONTRIBUTING.md - pull requests must be done to master branch, unless they are backports of fixes from master branch to a stable branch - backports to stable branches must be done with 'git cherry-pick -x ...' - code is contributed under BSD for core and main components (tm, sl, auth, tls) - code is contributed GPLv2 or a compatible license for the other components - GPL code is contributed with OpenSSL licensing exception -->
#### Pre-Submission Checklist <!-- Go over all points below, and after creating the PR, tick all the checkboxes that apply --> <!-- All points should be verified, otherwise, read the CONTRIBUTING guidelines from above--> <!-- If you're unsure about any of these, don't hesitate to ask on sr-dev mailing list --> - [X] Commit message has the format required by CONTRIBUTING guide - [X] Commits are split per component (core, individual modules, libs, utils, ...) - [X] Each component has a single commit (if not, squash them into one commit) - [X] No commits to README files for modules (changes must be done to docbook files in `doc/` subfolder, the README file is autogenerated)
#### Type Of Change - [ ] Small bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds new functionality) - [ ] Breaking change (fix or feature that would change existing functionality)
#### Checklist: <!-- Go over all points below, and after creating the PR, tick the checkboxes that apply --> - [ ] PR should be backported to stable branches - [X] Tested changes locally - [X] Related to issue #2285
#### Description This PR provides a hook to all into modules when all `fork_process()` is completed. This is to that the module in the main process can perform post-fork cleanup.
You can view, comment on, or merge this pull request online at:
https://github.com/kamailio/kamailio/pull/2919
-- Commit Summary --
* <a href="https://github.com/kamailio/kamailio/pull/2919/commits/cfc8f6373d3dd4cdb09f5e77fd797aff29b47190">core: add a module hook for post-fork cleanup</a>
-- File Changes --
M src/core/sr_module.h (1) M src/main.c (7)
-- Patch Links --
https://github.com/kamailio/kamailio/pull/2919.patch https://github.com/kamailio/kamailio/pull/2919.diff
A user of this hook would be the app_python3 module: it needs to know when all the child processes are forked, then the main process would perform internal state cleanup.
See the description of `PyOS_AfterFork_Parent`: https://docs.python.org/3/c-api/sys.html
@aalba6675 pushed 1 commit.
69b5bb39c7c47bb1970335c7fb0da813a7747d73 core: add a module hook for post-fork cleanup
How this feature will be used in app_python3 - this gives a hook to call `PyOS_AfterFork_Parent`: ``` /** * */ static pid_t main_pid = 0; /* record pid_t of main process */
static int child_init(int rank) { if(rank==PROC_INIT) { /* * this is called before any process is forked * so the Python internal state handler * should be called now. */ #if PY_VERSION_HEX >= 0x03070000 PyOS_BeforeFork() ; #endif main_pid = getpid(); return 0; } if(rank==PROC_POST_FORK) { /* * this is called after all processes are forked * so the Python internal state handler * should be called. */ #if PY_VERSION_HEX >= 0x03070000 PyOS_AfterFork_Parent() ; #endif return 0; } _apy_process_rank = rank;
/* * If non-daemonize we are called in the main context * with rank = PROC_SIPINIT */ if (main_pid != getpid()) { /* we really are a child process */ #if PY_VERSION_HEX >= 0x03070000 PyOS_AfterFork_Child(); #else PyOS_AfterFork(); #endif } if (cfg_child_init()) { return -1; } return apy_init_script(rank); } ```
Introduction of another execution of init_child() with a different rank impacts all the modules, it may have side effects for modules that handle only the ranks expected so far. Therefore, even the patch is small, has to be analysed the impact.
An alternative would be to add another core event with arming in the mod_register().
I will think a bit about and see what would be the best.
Unfortunately I found code snippets like this:
``` static int child_init(int rank) { int i, j; int len; xhttp_rpc_mod_cmds_t *cmds; /* rpc_export_t *rpc_e; */
if(rank==PROC_MAIN || rank==PROC_TCP_MAIN) return 0; /* do nothing for the main process */ ```
So some modules are "aware" of which ranks refer to the main process.
@miconda — have you had the chance to consider a better solution to this issue? I am thinking of going through the various modules and see how they check for rank:
/* ???? have an escape for a module if the module needs to do * does something for PROC_INIT, PROC_MAIN and rank > 0 (and small negative integers PROC_TCP etc) * then do nothing and return here ???? */ if (rank < PROC_MAIN_ONLY) return 0; // PROC_MAIN_ONLY == -128
Then to apply this PR I could set
PROC_POST_FORK = -129 // other new ranks can be created with values < -128 and all modules would not be affected
@aalba6675 - had a busy week, I thought of introducing a mechanism that should allow modules to specify in mod_init() if they want to execute special types of ranks for child_init(), so it will not impact other modules at all (because I remember when PROC_INIT was introduced, we discovered some side effect long time after). I hope to find some time for it this week.
I pushed the commit 7f89e4cc5c84b720928c2936c9e2e0630aebec9e for this purpose.
You have to execute `ksr_module_set_flag(KSRMOD_FLAG_POSTCHILDINIT)` inside `mod_init()` and then handle rank `PROC_POSTCHILDINIT` in `child_init()`.
Hoping it offers what is needed for this one.
Thank! Closing with d2c22d2.
Closed #2919.