Search code examples
c++boostnode-addon-api

node addon with cluster get process id returns same id for all forked process's


Edit

GetCurrentProcessId() and getpid() return different values... but boost does not.

Original question

I am writing a node addon to add a local native cache to be used with an express server when running as a cluster, to have a common cache. I am using boost's message_queue for IPC between the process's, and need to uniquely identify the process sending the requests. Boost provides boost::interprocess::ipcdetail::get_current_process_id to get the current process id, but the same process id is returned in the main process and the child process's. I think I am right in saying that child process's have their own unique ID as well. So what exactly is going on here:

Repo (it is a minimal reproducible): https://github.com/t348575/cluster-mem-shared

The output

00007FF95BDC2310
00007FF95BDC2310
00007FF95BDC2310
00007FF95BDC2310

Sample js test file

require('dotenv').config();
const cluster = require('cluster');
const cache = new (require('./dist/index')).ClusterMemShared(process.env.MODE);
if (cluster.isMaster) {
    cluster.fork();
    cluster.fork();
    cluster.fork();
}

I print this in the constructor of the class that c++ returns to js

std::cout << bip::ipcdetail::get_current_process_id << std::endl;

Solution

  • Like I said, that's an undocumented implementation detail.

    However, if this is really your code:

    std::cout << bip::ipcdetail::get_current_process_id << std::endl;
    

    Then the obvious fix is to call the function instead of printing its address:

    std::cout << bip::ipcdetail::get_current_process_id() << std::endl;
    

    Note the operator()