Skip to content

Comments

Python gRPC interceptor that uses Shared Memory#6

Open
aaron-ni wants to merge 4 commits intoni:TestInterceptorsfrom
aaron-ni:TestInterceptors-Python
Open

Python gRPC interceptor that uses Shared Memory#6
aaron-ni wants to merge 4 commits intoni:TestInterceptorsfrom
aaron-ni:TestInterceptors-Python

Conversation

@aaron-ni
Copy link

This adds a functioning gRPC interceptor in Python that uses shared memory (SidebandStrategy::SHARED_MEMORY). This required a tweak to the C++ code to add a 4-byte (int32_t) length to the beginning of the sideband_memory space (after the 8 bytes for _writeReady and _readReady).

The top of SharedMemoryForwardingInterceptor.py also includes use_Python_shared_memory that allows toggling between using Python's multiprocessing.shared_memory (which is compatible with what we do) and calling into ni_grpc_sideband.dll directly. The latter is the default and probably preferred because we probably want to move all the memory offset management to C++ for performance and avoiding duplicating details of memory layout that can change over time.

Also, I noticed the C++ code was potentially accessing out-of-bounds memory - InitClientSidebandData and InitOwnerSidebandData were being created at 4096 bytes, but then the first 8 bytes were used for read and write signals and the sideband_memory pointer was incremented by 8. However subsequent memcpy calls still addressed 4096 bytes, meaning they addressed the 8 bytes after the end of what was allocated via Init*SidebandData. In testing this isn't likely to come up, but some memory protections might cause a crash, and of course sometimes prototypes become production code.

@aaron-ni
Copy link
Author

aaron-ni commented Jun 5, 2025

FYI @ccifra (I'm not able to add you as a reviewer directly)

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.

1 participant