Brewskey / spark-server

An API compatible open source server for interacting with devices speaking the spark-protocol
https://www.particle.io/
GNU Affero General Public License v3.0
54 stars 27 forks source link

Server crashes when calling function on device which is being OTA flashed. #264

Closed DaliborFarny closed 6 years ago

DaliborFarny commented 6 years ago

Hello,

we are using your fork of Spark server for production, thank you for taking the effort to develop it.

Right now, I am creating a script to flash a device with new FW. The flow is this:

  1. Send OTA update with system firmware part 1 binary - the device starts update
  2. periodically (every 5s) call function on the device and wait until it becomes online.
  3. Send OTA with system firmware part 2. ...

Normally (using CLI), it takes around 120 seconds to flash system-firmware-part-1. The described process causes the server instance to crash after 30-40 seconds (the device flashes orange at that moment - handshake problem).

When I increase the period of calling the function to 30s, it usually goes through without crashing the server. When I start checking after 50s, with 5s period, it also goes through.

The device is on 0.6.2 and new firmware 0.6.3 is being flashed..

Log from the server:

[2018-05-17T08:04:31.030Z] ERROR: main.js/17977 on d1000-1975: uncaughtException Error: Got unexpected null or undefined at nullthrows (/usr/local/bin/spark-server/node_modules/nullthrows/nullthrows.js:9:9) at responseListener (/usr/local/bin/spark-server/node_modules/spark-protocol/dist/lib/EventPublisher.js:134:61) at EventPublisher. (/usr/local/bin/spark-server/node_modules/spark-protocol/dist/lib/EventPublisher.js:206:11) at EventPublisher.g (events.js:292:16) at emitOne (events.js:101:20) at EventPublisher.emit (events.js:188:7) at /usr/local/bin/spark-server/node_modules/spark-protocol/dist/lib/EventPublisher.js:229:22 at Array.forEach (native) at EventPublisher._this._emitWithPrefix (/usr/local/bin/spark-server/node_modules/spark-protocol/dist/lib/EventPublisher.js:228:10) at Immediate. (/usr/local/bin/spark-server/node_modules/spark-protocol/dist/lib/EventPublisher.js:119:15) at runCallback (timers.js:672:20) at tryOnImmediate (timers.js:645:5) at processImmediate [as _immediateCallback] (timers.js:617:5) error: Forever detected script exited with code: 1 error: Script restart attempt #51

AntonPuko commented 6 years ago

Hi, try with the last dev version. Take notice that your function call in that time returns null or undefined, idk if its your desired behaviour but its probably a side effect of that you're calling it during firmware update.

Why don't you use ping request instead calling the function though?

AntonPuko commented 6 years ago

Also, you probably don't need the script at all, the server should handle 2 parts system firmware updates for you, /cc @jlkalberer

jlkalberer commented 6 years ago

@DaliborFarny - thanks for the report.

As @AntonPuko said spark-server will handle OTA updates. If your device is 0.6.2 and you flash a 0.6.3 application firmware, it will automatically update your device.

The only thing is that we don't automatically download the newer binaries whenever Particle pushes a new version. You'd need to run npm run update-firmware


As for the bug here, you would probably still run into it when updating application firmware. Let us know if this is now fixed.

DaliborFarny commented 6 years ago

Thank you for quick response @jlkalberer @AntonPuko ,

I will make changes to our code - will use auto updates and ping, I had no idea about these two features - I've been using Spark-server since 2015, these functions are not implemented here.

I will let you know.

Many thanks for developing this fork and for sharing it with others!

DaliborFarny commented 6 years ago

I can confirm that code compiled against 0.6.3 which is flashed into device running 0.6.2 triggers the auto update of firmware. However, it seems that the bootloader, which is now (since 0.7.0) in third file is not flashed and the device ends up in safe mode. Manually flashing bootloader solves it. I dont see bootloader in binaries - I run npm run postinstall, not npm run update-firmware.

Is there a way how to configure it to use 0.6.3? I want to stay with this firmware before testing 0.7.0 which is reported as less stable than 0.6.3. I tried to replace binaries, but the app is still looking for 0.7.0. I could not find any config or database entry mentioning "0.7.0"..

Thanks!

npm run postinstall

spark-server@0.1.1 postinstall /usr/local/bin/spark-server update-firmware

Downloading cc3000-patch-programmer.bin... Downloading cc3000-patch-programmer_1_14.bin... Downloading core_doctor.bin... Downloading core_tinker.bin... Downloading deep_update_2014_06.bin... Downloading doctor.ino... Downloading electron_doctor.bin... Downloading electron_tinker.bin... Downloading p1_doctor.bin... Downloading p1_tinker.bin... Downloading photon_doctor.bin... Downloading photon_tinker.bin... Downloading tinker-usb-debugging-v0.4.8-rc.6-electron.bin... Downloading voodoospark.bin... Downloading system-part1-0.7.0-photon.bin... Downloading system-part2-0.7.0-photon.bin... Downloading system-part1-0.7.0-p1.bin... Downloading system-part2-0.7.0-p1.bin... Downloading system-part1-0.7.0-electron.bin... Downloading system-part2-0.7.0-electron.bin... Downloading system-part3-0.7.0-electron.bin...

Updated settings

Completed Sync

jlkalberer commented 6 years ago

@DaliborFarny - I'm working on updating update-firmware-binaries so that it downloads all the older firmwares. I'll let you know when it's ready.

jlkalberer commented 6 years ago

Ok, I did a pretty large update over the weekend.

  1. I updated the main readme to include a section about a .env file. You'll need to set one up in order ot properly download the files from github
  2. We now download most of the firmware + bootloader files. These will be used for system OTA updates
  3. OTA will only update to the version that is required by the application firmware.

Please test and let me know if it's working for you. This was a pretty large change and I only tested with my photon.

DaliborFarny commented 6 years ago

@jlkalberer Thank you for all your work, I will try it in few days and let you know. I sent you email to support at brewskey.com by the way (want to donate some bucks to development)..