Hello,
Please help. I am using Kemi with the python3 module. I get random
TypeError exceptions thrown within the Kamailio code.
For example I have the following code:
event = str(KSR.pv.get("$hdr(Event)"))
log.logInfo(
"KamailioStateful.ksr_request_route_loopback(): " +
"Passing the request to the presence " +
"publish handler for event: " + event
)
rc = KSR.presence.handle_publish()
if rc != 1:
log.logError(
"KamailioStateful.ksr_request_route_loopback(): " +
"The presence publish handler returned " +
"an error code: " + str(rc)
)
This is what I see in the logs:
21(120) ERROR: PY3 {PUBLISH}: app_python3 [python_support.c:167]:
python_handle_exception(): apy_exec: ksr_request_route((null)): Unhandled
exception in the Python code:
TypeError: expected bytes, str found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/etc/kamailio/kamailio.py", line 91, in ksr_request_route
return ksf.ksr_request_route(msg, "Loopback")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/etc/kamailio/kamailio_stateful.py", line 100, in ksr_request_route
return self.ksr_request_route_loopback(msg)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/etc/kamailio/kamailio_stateful.py", line 240, in
ksr_request_route_loopback
rc = KSR.presence.handle_publish()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <built-in function handle_publish> returned a result with an
exception set
If I put the offending line within a Try/Except it hides it but I would
rather not see it not happen at all. Any Kemi Python3 users experiencing
something similar? What could be causing this?
Regards,
Michel Pelletier
Show replies by date
Hi,
I've found the cause of this error. It only happens when the call into Kamailio KSR
takes more time than the threshold set in 'latency_limit_action' of the main
config file. In my case, the value is set to 100000 and I can trigger the issue by
calling tls.reload through KSR.jsonrpc.exec(..) as follows:
jsreload = dict()
jsreload["jsonrpc"] = "2.0"
jsreload["method"] = "tls.reload"
jsreload["id"] = 1
KSR.jsonrpcs.exec(json.dumps(jsreload))
The problem is caused by a bug in the app_python3 module. In file apy_kemi.c, the code
that prints the message indicating the threshold has been exceeded is causing a TypeError
exception while trying to get the co_filename and co_name values from the python
interpreter.
To do so it uses PyBytes_AsString , which expects a Bytes value but receives a Unicode
value, thus causing the TypeError exception.
The fix is to use PyUnicode_AsUTF8 instead. I will by writing all the details in a bug
report. With the fix in place, we get this:
45(329) INFO: app_python3 [apy_kemi.c:377]: sr_apy_kemi_exec_func(): alert - action
KSR.jsonrpcs.exec(...) took too long [693547 ms] (file:/etc/kamailio/kamailio.py
func:ksr_rtimer_dodebug line:507)
instead of Null values for 'file:' and 'func:'. Plus, no more TypeError
exception.
Cheers,
Michel Pelletier
________________________________
From: Michel Pelletier via sr-users <sr-users(a)lists.kamailio.org>
Sent: Thursday, July 11, 2024 12:00 PM
To: Kamailio (SER) - Users Mailing List <sr-users(a)lists.kamailio.org>
Cc: Michel Pelletier <michelpelletier07(a)gmail.com>
Subject: [SR-Users] Kemi Python3 throws a TypeError
Hello,
Please help. I am using Kemi with the python3 module. I get random TypeError exceptions
thrown within the Kamailio code.
For example I have the following code:
event = str(KSR.pv.get("$hdr(Event)"))
log.logInfo(
"KamailioStateful.ksr_request_route_loopback(): " +
"Passing the request to the presence " +
"publish handler for event: " + event
)
rc = KSR.presence.handle_publish()
if rc != 1:
log.logError(
"KamailioStateful.ksr_request_route_loopback(): " +
"The presence publish handler returned " +
"an error code: " + str(rc)
)
This is what I see in the logs:
21(120) ERROR: PY3 {PUBLISH}: app_python3 [python_support.c:167]:
python_handle_exception(): apy_exec: ksr_request_route((null)): Unhandled exception in the
Python code:
TypeError: expected bytes, str found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/etc/kamailio/kamailio.py", line 91, in ksr_request_route
return ksf.ksr_request_route(msg, "Loopback")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/etc/kamailio/kamailio_stateful.py", line 100, in ksr_request_route
return self.ksr_request_route_loopback(msg)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/etc/kamailio/kamailio_stateful.py", line 240, in
ksr_request_route_loopback
rc = KSR.presence.handle_publish()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <built-in function handle_publish> returned a result with an exception
set
If I put the offending line within a Try/Except it hides it but I would rather not see it
not happen at all. Any Kemi Python3 users experiencing something similar? What could be
causing this?
Regards,
Michel Pelletier