### Description kemi python3: exception occur when import logging module <!-- Explain what you did, what you expected to happen, and what actually happened. -->
### Troubleshooting
#### Reproduction very simple to reproduce is add this line to python kemi file ``` import logging ```
#### Debugging Data
``` (paste your debugging data here) ```
#### Log Messages
``` 0(49) INFO: <core> [core/tcp_main.c:4983]: init_tcp(): using epoll_lt as the io watch method (auto detected) 0(49) WARNING: outbound [outbound_mod.c:121]: mod_init(): "stun" module is not loaded. STUN is required to use outbound with UDP. 0(49) INFO: <core> [core/kemi.c:105]: sr_kemi_core_info(): KAMAILIO PYTHON3 APPLICATION INSTANTIATE CLASS OBJECT 0(49) INFO: <core> [core/kemi.c:105]: sr_kemi_core_info(): KAMAILIO INITIATION 0(49) INFO: <core> [main.c:3030]: main(): processes (at least): 25 - shm size: 268435456 - pkg size: 16777216 0(49) INFO: <core> [core/udp_server.c:154]: probe_max_receive_buffer(): SO_RCVBUF is initially 212992 0(49) INFO: <core> [core/udp_server.c:206]: probe_max_receive_buffer(): SO_RCVBUF is finally 425984 Exception ignored in: <function _releaseLock at 0x7fb7ed1d3f28> Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 226, in _releaseLock _lock.release() RuntimeError: cannot release un-acquired lock Ignoring exception from logging atfork <_StderrHandler <stderr> (WARNING)> release method: cannot release un-acquired lock 1(50) INFO: <core> [core/kemi.c:105]: sr_kemi_core_info(): KAMAILIO CHILD PROCESS INITIALIZED 1 Exception ignored in: <function _releaseLock at 0x7fb7ed1d3f28> Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 226, in _releaseLock Exception ignored in: <function _releaseLock at 0x7fb7ed1d3f28> Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 226, in _releaseLock Exception ignored in: <function _releaseLock at 0x7fb7ed1d3f28> Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 226, in _releaseLock _lock.release() Exception ignored in: <function _releaseLock at 0x7fb7ed1d3f28> Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 226, in _releaseLock _lock.release() _lock.release() ```
#### SIP Traffic
<!-- If the issue is exposed by processing specific SIP messages, grab them with ngrep or save in a pcap file, then add them next, or attach to issue, or provide a link to download them (e.g., to a pastebin site). -->
``` (paste your sip traffic here) ```
### Possible Solutions
<!-- If you found a solution or workaround for the issue, describe it. Ideally, provide a pull request with a fix. -->
### Additional Information
* **Kamailio Version** - output of `kamailio -v`
Test on `5.5.2` and `5.4.7` both occur the issue
``` root@c011e17b1e00:/opt/siproxy# kamailio -v version: kamailio 5.5.2 (x86_64/linux) 55e232 flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLOCKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED ADAPTIVE_WAIT_LOOPS 1024, MAX_RECV_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB poll method support: poll, epoll_lt, epoll_et, sigio_rt, select. id: 55e232 compiled on 11:54:55 Oct 14 2021 with gcc 8.3.0 ```
``` root@sgcoordinator1:/opt# kamailio -v version: kamailio 5.4.5 (x86_64/linux) c46342 flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED ADAPTIVE_WAIT_LOOPS 1024, MAX_RECV_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB poll method support: poll, epoll_lt, epoll_et, sigio_rt, select. id: c46342 compiled on 07:26:04 Jul 23 2021 with gcc 8.3.0 ```
* **Operating System**: occur on debian or docker as well
``` Linux c011e17b1e00 5.10.47-linuxkit #1 SMP Sat Jul 3 21:51:47 UTC 2021 x86_64 GNU/Linux Linux machine 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 GNU/Linux ```
I am not familiar with app_python3 that much, maybe @amessina or @aalba6675 have time to check it.
Based on a quick search on the net, starting with python 3.7 the use of PyOS_BeforeFork() and other related functions listed at https://docs.python.org/3/c-api/sys.html may be required to use in apps that do fork() like Kamailio.
@hnimminh unfortunately I am not using app_python3 any longer but it looks like @miconda may be pointing in the right direction.
@hnimminh — curious, does your code use threads? https://bugs.python.org/issue6721
I am not able to reproduce with a simple kamailio script on Python 3.9 but I will look at adding the calls to handle forking.
@miconda is there a hook function in the main process to call when after all the children have be forked? Like mod_post() {/* this function is called in the main process when all the children have been forked* */}
I am trying to find a good place to insert a call to PyOS_AfterFork_Parent()
"Function to update some internal state after a process fork. This should be called from the parent process after calling fork() or any similar function that clones the current process, regardless of whether process cloning was successful. Only available on systems where fork() is defined."
@hnimminh diff for you to try
``` diff --git a/src/modules/app_python3/app_python3_mod.c b/src/modules/app_python3/app_python3_mod.c index 0ce93b71cd..168e1a0f0a 100644 --- a/src/modules/app_python3/app_python3_mod.c +++ b/src/modules/app_python3/app_python3_mod.c @@ -163,6 +163,9 @@ static int mod_init(void) LM_ERR("failed to load python script\n"); return -1; } +#if PY_VERSION_HEX >= 0x03070000 + PyOS_BeforeFork(); +#endif
pkg_free(dname_src); pkg_free(bname_src); ```
thanks @aalba6675 I did not implement anything yet except "import logging" Tried with your diff and issue is still same.
@hnimminh - there are some commits on master branch that so we now correctly adhere to Python >= 3.7 fork-handling.
Noted, I will do a test and and update soon!
Closing this one, it's about 4 weeks of inactivity and the corresponding commits are now in the repo. If still an issue, this one can be reopened or another one created.
Closed #2885.