]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commit
Fix connector status race condition in UI server cache during startup (#1548)
authorCopilot <198982749+Copilot@users.noreply.github.com>
Wed, 8 Oct 2025 15:03:39 +0000 (17:03 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Oct 2025 15:03:39 +0000 (17:03 +0200)
commitb7aafd95431f61c160a2dea485a487c8b046629b
tree4dc6dae952d62e61979dab6386487a23d8685966
parentdcbccf54c29487ff42b8d6a3dcaa6c339c9d4ddb
Fix connector status race condition in UI server cache during startup (#1548)

* Initial plan

* Fix connector status race condition by setting status before sending request

Set connector status optimistically before sending the StatusNotification
request to ensure the updated event (emitted in the request handler's
finally block) contains the current status. Revert status on error.

This prevents the UI cache from being populated with stale connector
status during startup when multiple status notifications are sent
concurrently.

Fixes race condition where:
1. StatusNotification sent
2. Response received
3. 'updated' event emitted with OLD status (in finally block)
4. Connector status set to NEW status
5. UI cache now has outdated data

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* refactor: avoid non-null assertions in sendAndSetConnectorStatus

Replace non-null assertions (!) with null checks and early return to comply
with updated coding guidelines. Add error logging when connector status is null.

Changes:
- Check connectorStatus for null before accessing properties
- Store connectorStatus reference to avoid repeated calls
- Add error log and early return for non-existing connector
- Remove all eslint-disable comments for no-non-null-assertion

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* revert: remove incorrect fix to sendAndSetConnectorStatus

The previous fix addressed OCPP server communication, but the issue is about
UI server cache updates via worker messages. Reverting to investigate the
correct location for the fix.

The race condition is in how worker messages are sent to the main thread,
not in the OCPP StatusNotification timing.

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* fix: prevent race condition in UI cache during connector initialization

Add skipUpdatedEvent parameter to RequestParams to suppress automatic
updated events during connector status initialization loop. Emit single
updated event after all connectors are initialized.

This fixes the race condition where multiple partial ChargingStationData
messages were sent to the main thread during startup, potentially causing
the UI server cache to contain stale or incomplete connector status data.

Changes:
- Add skipUpdatedEvent to RequestParams interface
- Update OCPPRequestService to check skipUpdatedEvent before emitting
- Add params parameter to sendAndSetConnectorStatus
- Use skipUpdatedEvent in startMessageSequence for all connector inits
- Emit single updated event after initialization loop completes

The fix ensures UI cache receives complete, consistent charging station
data after all connectors are properly initialized, eliminating the race
condition caused by out-of-order or partial state updates.

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* revert: remove incorrect fix that broke status notification lifecycle

Previous fix suppressed updated events during initialization, which prevented
the correct chronological sending of status notifications to OCPP server.

The actual issue is in how the UI server cache handles out-of-order worker
messages, not in when updated events are emitted.

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* fix: prevent UI cache corruption from out-of-order worker messages

Add timestamp to ChargingStationData and update workerEventUpdated to only
update cache if incoming message is newer than cached data.

This prevents the UI server cache from being overwritten with stale data
when worker messages arrive out of chronological order during startup.

Changes:
- Add timestamp field to ChargingStationData interface
- Set timestamp in buildChargingStationDataPayload using Date.now()
- Update Bootstrap.workerEventUpdated to compare timestamps
- Only update cache if incoming data is newer or cache is empty

The fix ensures the UI cache always contains the most recent charging
station state, even when multiple updated messages are sent during
initialization and arrive out of order.

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* refactor: encapsulate chargingStations Map with getter/setter methods

Make chargingStations Map private in AbstractUIServer and add public
getter/setter methods to encapsulate access. Move timestamp comparison
logic into the setter method for centralized cache update control.

Changes:
- Make chargingStations Map private in AbstractUIServer
- Add getChargingStationData() getter for single entry
- Add listChargingStationData() to return all entries as array
- Add setChargingStationData() with timestamp comparison logic
- Add deleteChargingStationData() for deletion
- Add hasChargingStationData() for existence check
- Add getChargingStationsCount() for size
- Update Bootstrap event handlers to use setter methods
- Update AbstractUIService to use getter methods

This encapsulation ensures all cache updates go through the timestamp
comparison logic, preventing corruption from out-of-order messages.

Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
* [autofix.ci] apply automated fixes

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jerome-benoit <6537332+jerome-benoit@users.noreply.github.com>
Co-authored-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
src/charging-station/Bootstrap.ts
src/charging-station/ui-server/AbstractUIServer.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/types/ChargingStationWorker.ts
src/utils/MessageChannelUtils.ts