eclipse-paho / eclipse-paho/paho.mqtt.javascript
Silent fail on exceeding localStorage quota
- Dominant language
- JavaScript
- Stars
- 1.2k
- Forks
- 466
- PR merge metrics
- No merged PRs in 30d
Description
I saw this "Persistent storage maximum size reached" message in the JS console of a Firefox profile of mine from which a site of ours using Paho mysteriously hadn't been working correctly for some time.
```
MQTT closed abnormally: AMQJS0005E Internal error. Error Message: Persistent storage maximum size reached, Stack trace: ClientImpl.prototype.store@https://mysite.mydomain/cm/javascript/lib/mqttws31.js:1046:3
ClientImpl.prototype._requires_ack@https://mysite.mydomain/cm/javascript/lib/mqttws31.js:1123:4
ClientImpl.prototype.send@https://mysite.mydomain/cm/javascript/lib/mqttws31.js:922:4
Client/this.send@https://mysite.mydomain/cm/javascript/lib/mqttws31.js:1930:5
```
Inspecting the localStorage object in the console indeed reveals it being of ~5 mega [code units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) in length, approximately at the [reported 10 MiB limit](https://www.html5rocks.com/en/tutorials/offline/quota-research/#toc-firefox) of firefox assuming 16 bit code units:
```
JSON.stringify(localStorage).length
5438801
```
Curiously the doubled 10 MiB limit of [the 5 MiB W3C recommandation](https://www.w3.org/TR/webstorage/#disk-space) of Firefox seems to be caused by a unit mismatch. Indeed the [config item clearly defines a 5*1024 KiB = 5120 KiB limit](https://github.com/mozilla/gecko-dev/blob/master/dom/storage/LocalStorageManager.cpp#L28) but in use [the comparison seems to be done against a length measured in 16 bit units](https://github.com/mozilla/gecko-dev/blob/master/dom/storage/LocalStorageCache.cpp#L218).
Looking a bit at the Paho source the localStorage doesn't seem to be very thoroughly managed. Any quota exception thrown [when storing the message using setItem() wouldn't be catched](https://github.com/eclipse/paho.mqtt.javascript/blob/master/src/paho-mqtt.js#L1115) nor does there seem to be any cleaning done using removeItem() except when a reply from the peer [has been successfully received in _handleMessage()](https://github.com/eclipse/paho.mqtt.javascript/blob/master/src/paho-mqtt.js#L1255) (transmissions with no replies ie. packet loss or disconnects will stay until otherwise cleaned).
Contributor guide
Assessment
This issue has not been assessed yet.