fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
943 stars 89 forks source link

child_process.exec doesn't call callback when using pigpio in my project #126

Closed J-Cat closed 3 years ago

J-Cat commented 3 years ago

I switched to pigpio (vs onoff) to fix the issues I was having with skipped steps in the rotary encoder (which worked wonderfully by the way!), however, now, all my calls to child_process.exec never enter the callback.

If I swap back to using onoff, it's fine, but as soon as I use pigpio (using interrupts), it stops working.

Edit 2: Below work-around is irrelevant, see comments at bottom of thread.

Edit: A quick update here, I was able to "work-around" the issue by capturing the stdout.on('close') and/or the stderr.on('close') events of the child process. eg.

const cp = exec(cmd);

let error: Error | undefined;
cp.on('error', err => { 
  error = err;
});
let stderr: string | undefined;
cp.stderr?.on('data', data => {
  stderr = !stderr ? data :`${stderr}\n${data}`;
});
cp.stdout?.on('data', data => {
  stdout = !stdout ? data : `${stdout}\n${data}`;
});

await new Promise<void>(resolve => {
  cp.stdout?.on('close', () => {
    resolve();
    cp.kill();
  });
});

return { error, stderr, stdout };
fivdi commented 3 years ago

Can you post a short but complete JavaScript program that can be used to reproduce the problem please?

J-Cat commented 3 years ago

Can you post a short but complete JavaScript program that can be used to reproduce the problem please?

Just did this, but no point in posting as it worked! So there is something else ... it is very strange that everything else being the same (except swapping onoff for pigpio), and it works properly with one and not the other. This is a quite large project though, so there is obviously something else causing the issue.

I'd spend more time on it, but the "work-around" (although I don't love it), solves the issue 100% and there are no other issues in the application that I know of (and I've been running it 24x7 for months

Thanks for the response!

fivdi commented 3 years ago

I wonder if the issue is related to signals. The pigpio C library makes fairly extensive usage of signals. The documentation for the pigpio Node.js module mentions this topic in the description of the initialization() method.

J-Cat commented 3 years ago

Yes, this was 100% the issue. I added a call to initialize() and terminate() at the start of the app and on cleanup respectively, and now everything works exactly as expected.

Thank you very much for this! (this was bothering me not knowing what the root cause of the issue was and instead just having a work-around)