eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.14k stars 468 forks source link

Silent fail on exceeding localStorage quota #119

Open mikini opened 6 years ago

mikini commented 6 years ago

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 in length, approximately at the reported 10 MiB limit of firefox assuming 16 bit code units:

JSON.stringify(localStorage).length
5438801

Curiously the doubled 10 MiB limit of the 5 MiB W3C recommandation of Firefox seems to be caused by a unit mismatch. Indeed the config item clearly defines a 5*1024 KiB = 5120 KiB limit but in use the comparison seems to be done against a length measured in 16 bit units.

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 nor does there seem to be any cleaning done using removeItem() except when a reply from the peer has been successfully received in _handleMessage() (transmissions with no replies ie. packet loss or disconnects will stay until otherwise cleaned).