Skip to content

Avoid app_handler Extra Jump to C++#52

Merged
doctorlai-msrc merged 23 commits intomainfrom
zhihua/debugging
May 13, 2025
Merged

Avoid app_handler Extra Jump to C++#52
doctorlai-msrc merged 23 commits intomainfrom
zhihua/debugging

Conversation

@doctorlai-msrc
Copy link
Collaborator

@doctorlai-msrc doctorlai-msrc commented May 10, 2025

This PR closes #51

The current implementation of the app_handler callback in Python apps contains unnecessary code that is causing performance issues and random Python crashes. This was done this way because we wanted to reuse the C++ code jrtc_app.cpp

The call chain is:

  1. jrtc (C)
  2. jrtc_pythonapp_loader.c (Python sub-interpreter)
  3. jrtc_start_app (Python, user's code)
  4. jrtc_app.py (C++ wrapper)
  5. jrtc_app.cpp (C++)
  6. app_handler (Python, user's code).

In the app_handler() function (user's code), the following (memory allocation) will crash the JRTC randomly:

a = 10000 * [0]
for i in range(10000):
    a[i] = 100 * [1]

This may be due to the memory allocation where the lists are allocated in the stack intead of the heap.

This PR implements the jrtc_app.cpp in Python, so the unnecessary hop to C++ space is eliminated. The jrtc_app.py will now be in Python instead of wrapping C++ code.

With this PR, the python app won't randomly crash for above Python code.

@doctorlai-msrc doctorlai-msrc marked this pull request as draft May 10, 2025 11:49
@doctorlai-msrc doctorlai-msrc marked this pull request as ready for review May 12, 2025 13:38
class AppStateVars(ctypes.Structure):
_fields_ = [
("app", ctypes.POINTER(JrtcApp)),
("app", ctypes.py_object),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think the python apps should now have any references to ctypes, since this should now be python only

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't have Python type here as AppStateVars is ctypes.Structures. Otherwise, this have to be moved out somewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this doesnt need to be a ctypes.Strucutre does it ? It can be a simple Python dataclass, or even a dict

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

lib.jrtc_app_router_channel_send_output_msg.argtypes = [ctypes.POINTER(JrtcApp), c_int, c_void_p, c_int]
lib.jrtc_app_router_channel_send_output_msg.restype = c_int
# Callback type definition
JrtcAppHandler = ctypes.CFUNCTYPE(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pre my previous comment, do we need any ctypes stuff here ? I think we just want to pass python varables

@doctorlai-msrc doctorlai-msrc added this pull request to the merge queue May 13, 2025
Merged via the queue into main with commit fde20da May 13, 2025
14 checks passed
@doctorlai-msrc doctorlai-msrc deleted the zhihua/debugging branch May 13, 2025 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random Python Code Crashes

2 participants