node-red-contrib-ocpp icon indicating copy to clipboard operation
node-red-contrib-ocpp copied to clipboard

cb_map handling is racy

Open nagisa opened this issue 1 year ago • 0 comments

After 2 weeks-ish of runtime with a real CB I have noticed that at some point I would no longer be able to send any messages to the CB via CB request JSON node due to {cbId} not connected. However at the same time the CS server node was producing MeterValues messages sent by that same cbId, clearly making it connected.

I did not have in-depth logging enabled, however a brief code review suggests that the following situation might have caused the observed issue:

  1. CB request JSON node determines the websocket to use through cb_map data structure;
  2. cb_map is inserted to, and removed from, as websocket connects or disconnects;
  3. It is possible that the following sequence of events occurs:
    1. CB connects to the OCPP server with connection 1 (thus populating cbId in the cb_map);
    2. Some time later CB connects to the OCPP server with connection 2 (thus replacing the cbId entry in the cb_map and removing the value for connection 1)
    3. The connection 1 is terminated, times out, etc. This causes cb_map.remove(cbId) thus removing the entry for connection 2.

In principle I don't see any reason why multiple connections with the same cbId couldn't be made, but this library would likely behave incorrectly in such scenarios.

nagisa avatar Jul 21 '24 20:07 nagisa