FlowFuse / device-agent

An agent to run FlowFuse managed instances of Node-RED on devices
Apache License 2.0
15 stars 8 forks source link

If NR process has exited, dont send or wait for sigxxx #252

Closed Steve-Mcl closed 3 months ago

Steve-Mcl commented 3 months ago

closes #251

Description

Skip the deferred stop if the NR process has already exited.

According to node docs:

subprocess.exitCode integer The subprocess.exitCode property indicates the exit code of the child process. If the child process is still running, the field will be null.

Therefore this PR adds a check for exit code. If null, it skips the deferred stop, performs the on-stop operations, clean up (unref and null) the process & resolves immediately.

Related Issue(s)

Checklist

Labels

Steve-Mcl commented 3 months ago

tested on widows by specifying a port in use. This causes NR to crash and the agent-launcher to attempt a restart until finally giving up. Once it detected a restart loop, CTRL-C was issued in the terminal & the agent now cleanly quits.

$ flowfuse-device-agent
Debugger attached.
[AGENT] 15/04/2024 15:07:40 [info] FlowFuse Device Agent
[AGENT] 15/04/2024 15:07:40 [info] ----------------------
[AGENT] 15/04/2024 15:07:40 [info] Agent starting...
[AGENT] 15/04/2024 15:07:40 [info] Version: 2.4.0
[AGENT] 15/04/2024 15:07:40 [info] Mode: Device Mode
[AGENT] 15/04/2024 15:07:40 [info] Device: 1qb0XzkbWD
[AGENT] 15/04/2024 15:07:40 [info] ForgeURL: https://forge.flowfuse.dev
[AGENT] 15/04/2024 15:07:40 [info] Configuration :-
[AGENT] 15/04/2024 15:07:40 [info]   * Application        : OaLQN0J9VB
[AGENT] 15/04/2024 15:07:40 [info]   * Snapshot           : none
[AGENT] 15/04/2024 15:07:40 [info]   * Settings           : 9fea89632e1821fc10fbe1f3538be376f5b7125e48a676878af9b88770b1daad
[AGENT] 15/04/2024 15:07:40 [info]   * Operation Mode     : autonomous
[AGENT] 15/04/2024 15:07:40 [info]   * Target State       : running
[AGENT] 15/04/2024 15:07:40 [info]   * Licensed           : yes
[AGENT] 15/04/2024 15:07:40 [info] Environment :-
[AGENT] 15/04/2024 15:07:40 [info]   * FF_DEVICE_ID       : 1qb0XzkbWD
[AGENT] 15/04/2024 15:07:40 [info]   * FF_DEVICE_NAME     : new clean device
[AGENT] 15/04/2024 15:07:40 [info]   * FF_DEVICE_TYPE     :
[AGENT] 15/04/2024 15:07:40 [info]   * FF_APPLICATION_ID  : OaLQN0J9VB
[AGENT] 15/04/2024 15:07:40 [info]   * FF_APPLICATION_NAME: Demo Application
[AGENT] 15/04/2024 15:07:40 [info]   * FF_SNAPSHOT_ID     : 0
[AGENT] 15/04/2024 15:07:40 [info]   * FF_SNAPSHOT_NAME   : None
[AGENT] 15/04/2024 15:07:40 [info] Starting MQTT heartbeat thread. Interval: 30s (±5s)
[AGENT] 15/04/2024 15:07:40 [info] MQTT connected
[AGENT] 15/04/2024 15:07:40 [info] Starting Node-RED
[NR] 15/04/2024 15:07:41 [info] 

Welcome to Node-RED
===================

[NR] 15/04/2024 15:07:41 [info] Node-RED version: v3.1.9
[NR] 15/04/2024 15:07:41 [info] Node.js  version: v18.20.2
[NR] 15/04/2024 15:07:41 [info] Windows_NT 10.0.22631 x64 LE
[NR] 15/04/2024 15:07:41 [info] Loading palette nodes

< SNIP FOR BREVITY >

[NR] 15/04/2024 15:07:42 [error] Unable to listen on http://127.0.0.1:1880/device-editor/
[NR] 15/04/2024 15:07:42 [error] Error: port in use
[AGENT] 15/04/2024 15:07:42 [info] Node-RED stopped unexpectedly - restarting
[AGENT] 15/04/2024 15:07:42 [info] Starting Node-RED
[NR] 15/04/2024 15:07:43 [info] 

Welcome to Node-RED
===================

[NR] 15/04/2024 15:07:43 [info] Node-RED version: v3.1.9
[NR] 15/04/2024 15:07:43 [info] Node.js  version: v18.20.2

< SNIP FOR BREVITY >

[NR] 15/04/2024 15:07:44 [error] Unable to listen on http://127.0.0.1:1880/device-editor/
[NR] 15/04/2024 15:07:44 [error] Error: port in use
[AGENT] 15/04/2024 15:07:44 [info] Node-RED stopped unexpectedly - restarting
[AGENT] 15/04/2024 15:07:44 [info] Starting Node-RED
[NR] 15/04/2024 15:07:45 [info] 

< SNIP FOR BREVITY >

Welcome to Node-RED
===================

[NR] 15/04/2024 15:07:49 [info] Node-RED version: v3.1.9
[NR] 15/04/2024 15:07:49 [info] Node.js  version: v18.20.2
[NR] 15/04/2024 15:07:49 [info] Windows_NT 10.0.22631 x64 LE
[NR] 15/04/2024 15:07:50 [info] Loading palette nodes

< SNIP FOR BREVITY >

[NR] 15/04/2024 15:07:50 [error] Unable to listen on http://127.0.0.1:1880/device-editor/
[NR] 15/04/2024 15:07:50 [error] Error: port in use
[AGENT] 15/04/2024 15:07:50 [info] Node-RED restart loop detected - stopping

< CTRL-C PRESSED >

SIGINT received
[AGENT] 15/04/2024 15:08:02 [info] Stopping Node-RED. Reason: shutdown
[AGENT] 15/04/2024 15:08:02 [info] Node-RED Stopped
[AGENT] 15/04/2024 15:08:03 [info] Stopping MQTT heartbeat thread
[AGENT] 15/04/2024 15:08:03 [info] Closing MQTT connection
Waiting for the debugger to disconnect...

$ 
hardillb commented 3 months ago

Tested on Linux, see same behaviour and this fix appears to solve problem