cuIpcOpenMemHandle return CUDA_ERROR_INVALID_CONTEXT

IPC is intended for interprocess communication. The mem handle is designed to be opened by another process. Why are you trying to open it in the process you created it in?

If you want to access the allocation from that original process, just use the cuda_devptr you have already allocated via cuMemAlloc. Even if the runtime API let you overwrite the pointer, you would have a memory leak.

It’s not clear you understand the general usage pattern here. Please study the cuda simpleIPC sample code.