Search code examples
node.jsnpmraspberry-pi3

Node.js execution freezes after reboot


CONFIGURATION:

I'm building an FPV robot that can be controlled through a web browser locally on Wi-Fi.

I got the MVP up and running. I get a 640x480@20FPS with 240mS of latency video stream on the webbrowser and I get 7mS of latency on the controls.

I'm running a NODE.JS webserver on a raspberry pi 3B+. The operating system is Raspbian Stretch. I'm using Socket.IO to provide low latency bidirectional controls and Websockets to transport streaming packets.

PROBLEM:

After powering down the Raspberry, NODE.JS will sometime freeze upon execution. I get no errors and I have no feedback on why NODE is stuck. it requires a CTRL-C to shutdown.

pi@MazeRunner:~ $ node 2019-06-09-ffmpeg/node.js


If I release NODE.JS as separate process, I can keep using the RPI, but NODE is frozen in background.

pi@MazeRunner:~ $ node 2019-06-09-ffmpeg/node.js &  
[1] 778
pi@MazeRunner:~ $

DETAILS:

  1. Sometimes, NODE.JS is able to recover on its own after about 5 minutes of wait with no feedback. It just starts.
pi@MazeRunner:~ $ node 2019-06-09-ffmpeg/node.js
INFO: Server interface - enxb827eb23ca00 192.168.0.202
INFO: 192.168.0.202 listening to html requests on port 8080
INFO: /home/pi/2019-06-09-ffmpeg/index.html has been loaded into memory
INFO: /home/pi/2019-06-09-ffmpeg/style.css has been loaded into memory
INFO: /home/pi/2019-06-09-ffmpeg/jsmpeg.min.js has been loaded into memory
  1. Sometimes I'm able to restore NODE to working condition by initializing npm, but sometimes this command get stuck as well.
pi@MazeRunner:~ $ npm --init yes

  1. I'm always able to restore node by removing it with apt-get remove and reinstalling it with apt-get install, which obviously is not doable

  2. I tried looking for node.js or npm logs, but I had no luck in finding them

  3. Executing a shutdown before cutting power seems to make node.js much more likely to not freeze on start.

pi@MazeRunner:~ $ sudo shutdown now

QUESTION:

  1. Is there a way to have node.js emit detailed debug logs somewhere to debug the issues? There are no error messages neither on STDOUT nor in STDERR

  2. I'd like for the robot to be plug&play and to have it consistently working after abruptly cutting power. I can add a shutdown button on the browser page, but you can easily forget to use it. Shutdown by console defeats the purpose of having a browser control interface at all.

Is there a way to make raspbian stretch safe to shutdown abruptly?

Thanks for whatever help you can provide!


Solution

  • Your code may block the event-loop. Especially when dealing with websockets it's kinda dangerous cause loop-cycle may just wait till it's "ticked" and move on. In that case, there's no way to handle logs or anything. Node.js runs on a single thread, if your code blocks the event-loop your entire app will freeze.

    You can also check out this module https://github.com/naugtur/blocked-at

    References
    https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
    https://www.youtube.com/watch?v=8aGhZQkoFbQ
    https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c