Repository navigation
fix(opcua): reconnect, alarm and companion specification fixes - #783
Open
mfaferek93 wants to merge 12 commits into
Open
mfaferek93 wants to merge 12 commits into
mfaferek93 wants to merge 12 commits into
Conversation
…ll one The ConditionId select clause has no BrowsePath. It went out as a null array (length -1), and a Siemens S7-1500 answers that with a null ConditionId, so every native alarm was dropped as a non-condition event. It now goes out as an empty array (length 0).
The scan lambda runs on the sweep's worker threads. Two first probes could both see the string empty and assign it at once, which ThreadSanitizer reports as a race. A mutex around the check and the assignment fixes it.
open62541 1.3 security policy contexts keep a pointer to the logger of the config they were built in. That config was a local of the connect call, so the log line on closing a secured channel read a dead stack frame, and a dropped link crashed the gateway (SIGSEGV in UA_ChannelModule_Delete_Context).
x-plc-status gains last_error, the OPC UA status name of the last failure: a refused server certificate, an ERR from a server that refuses the client's certificate, a refused user, a timeout. open62541 1.3 ends a refused channel with BadConnectionClosed and only logs why, so the reason is taken from the client's certificate check and from the server's ERR message.
…n policy says so A channel left open by a refused ActivateSession read as connected through open62541pp's isConnected(), so x-plc-status said connected and every read failed. Only an activated session counts now. A server that offers no token policy for the configured user is reported as BadIdentityTokenInvalid instead of open62541's BadInternalError. Built on the secured config lifetime fix.
A companion specification fixes BrowseNames and namespace URIs, not NodeIds or namespace indices. A node map entry may now give a browse_path from the Objects folder (namespace aliases, * for a vendor-named instance) instead of a node_id. It is resolved on every new session, so a server that renumbers its namespaces after a restart is followed to the new NodeIds.
fault_enum takes ok_values, so a state machine register with many healthy states does not raise its catch-all. A detection entry can be gated on another polled point: inhibit_when keeps a fault from being raised while that point reads one of the listed values, only_when evaluates the rule only then.
The value conversion only handles scalars, and getScalarCopy throws BadVariantAccess on an array of a matching type, which no read path caught. auto_browse on a server with array variables (recipe tables, alarm lists) took the gateway down. A non-scalar read is now not good, so auto_browse skips the node and polling or a subscription drops it.
Part 9 brackets the refresh replay on every event item of the subscription, and each item only carries its own notifier's conditions. With two alarm sources the second RefreshEnd reconciled every tracked condition against its own burst and cleared the first source's live alarms. The replayed set and the reconcile are now kept per source, and an End without a Start is ignored.
isType<T>() is true for an array of T, so getScalarCopy threw BadVariantAccess when an event field (Severity, Message, ConditionName, an associated value) or a condition property read in the read-based replay was an array: the notification was dropped, or the throw left the poll thread. Every scalar read in those paths now checks isScalar() first.
ReportFault and ClearFault are separate services, so an alarm that came and went inside one publishing interval could reach fault_manager clear first and stay raised for good. Calls for one code now go out one at a time, each after the previous was answered (an unanswered call holds its code for at most 5 s).
plugins.opcua.auto_browse.infer_writable: false, set on its own beside a curated node map, enabled the walk because "enabled" defaulted to true for any object. Without "enabled" only a walk knob turns it on now. Also accept match_message in the event_alarms mapping key check: it was parsed but reported as an unknown key.
This was referenced Nov 9, 2026
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved NodeId remapping collisions can leave browse-path entries bound to stale IDs and reading the wrong variables after namespace renumbering.
5 open issues
Resolve swapped NodeIds against final browse-path mappings · New Expire queued fault lanes so the final clear is dispatched · New Validate namespace prefix length before converting it · New Avoid connection-lock blocking in the PLC status handler · New Recreate publishers when explicit browse-path mappings replace entries · New
What changed in this PR
This PR strengthens the OPC UA plugin’s connection recovery and alarm handling, while adding companion-specification addressing and machine-state-aware fault detection.
Changes:
- Preserve secured-client configuration lifetime, report connection failures through
last_error, and reject non-scalar values safely. - Add namespace-aware browse paths, multiple healthy enum values, and state gates.
- Reconcile native alarms per source, sequence fault reports and clears, and expand regression coverage.
Paths below are relative to src/ros2_medkit_plugins/ros2_medkit_opcua/ unless otherwise shown.
| File | Description |
|---|---|
test/test_opcua_poller.cpp |
Tests per-source refresh and array event fields. |
test/test_opcua_plugin.cpp |
Tests auto-browse defaults and synchronizes shared test state. |
test/test_opcua_client.cpp |
Tests error reporting and empty ConditionId browse paths. |
test/test_opcua_client_reconnect.cpp |
Adds secured-channel reconnect regression coverage. |
test/test_node_map.cpp |
Tests healthy-state lists and gate validation. |
test/test_fault_code_lanes.cpp |
Tests per-code dispatch ordering and stale responses. |
test/test_browse_path.cpp |
Tests path parsing, resolution, precedence, and remapping. |
test/integration/test_opcua_state_gates.test.py |
Exercises gated faults through a running gateway. |
test/integration/test_opcua_secured.test.py |
Verifies authentication and certificate failure reporting. |
test/fixtures/test_alarm_server/test_alarm_server.cpp |
Adds state registers and an array fixture. |
src/opcua_poller.cpp |
Applies state gates and per-source alarm reconciliation. |
src/opcua_plugin.cpp |
Integrates path resolution, status errors, and ordered dispatch. |
src/opcua_client.cpp |
Fixes secured-client lifetime, session checks, and scalar handling. |
src/node_map.cpp |
Parses and resolves browse paths and state gates. |
src/address_space_browser.cpp |
Adds bounded, namespace-aware path traversal. |
README.md |
Documents status errors and new configuration options. |
include/ros2_medkit_opcua/opcua_poller.hpp |
Defines per-source refresh tracking and test access. |
include/ros2_medkit_opcua/opcua_plugin.hpp |
Declares path resolution and fault-dispatch lanes. |
include/ros2_medkit_opcua/opcua_client.hpp |
Exposes error status and namespace lookup. |
include/ros2_medkit_opcua/node_map.hpp |
Defines browse-path entries, resolution results, and gates. |
include/ros2_medkit_opcua/fault_code_lanes.hpp |
Introduces per-fault-code service-call sequencing. |
include/ros2_medkit_opcua/address_space_browser.hpp |
Declares namespace lookup and path resolution interfaces. |
CMakeLists.txt |
Registers new tests and certificate-generation support. |
src/ros2_medkit_fault_detection/test/test_fault_detection.cpp |
Tests multiple healthy enum states. |
src/ros2_medkit_fault_detection/include/ros2_medkit_fault_detection/fault_detection.hpp |
Adds healthy-value lists to shared enum detection. |
Review effort: Balanced
Comment on lines
+1538
to
+1542
| if (node_id_index_.count(moved_to) > 0) { | ||
| RCLCPP_WARN(rclcpp::get_logger("opcua.node_map"), | ||
| "browse_path '%s' now resolves to %s, which another entry maps - keeping %s", | ||
| entry.browse_path_str.c_str(), moved_to.c_str(), entry.node_id_str.c_str()); | ||
| continue; |
Comment on lines
+52
to
+54
| if (lane.busy && now - lane.since < stale_after_) { | ||
| lane.queue.push_back(std::move(send)); | ||
| return; |
Comment on lines
+1492
to
+1498
| const auto index = std::stoul(prefix); | ||
| if (prefix.size() > 5 || index > 65535) { | ||
| error = "step " + std::to_string(i + 1) + ": namespace index " + prefix + " is out of range"; | ||
| out.clear(); | ||
| return false; | ||
| } | ||
| element.namespace_index = static_cast<uint16_t>(index); |
| j["write_capable"] = MEDKIT_OPCUA_READ_ONLY == 0; | ||
| j["poll_count"] = snap.poll_count; | ||
| j["error_count"] = snap.error_count; | ||
| j["last_error"] = client_->last_error(); |
Comment on lines
+1994
to
+1996
| if (resolution.joined > 0) { | ||
| create_value_publishers(); | ||
| } |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Twelve changes to the OPC UA plugin, one commit each, in the order below. They were open as six separate pull requests (#777, #778, #779, #780, #781, #782). Together they make the plugin survive a dropped secured link, say why it is down, address nodes by companion specification browse paths, gate detection rules on machine state, and keep native alarms correct with several alarm sources.
1. Empty BrowsePath in the ConditionId clause (was #777)
What. The event filter sent with every Alarms & Conditions subscription has a ConditionId select clause with no BrowsePath (
ConditionType,AttributeId=NodeId). The plugin leftbrowsePathas a NULL pointer, which open62541 encodes as a null array (length -1). The clause now carries an empty array (length 0):make_event_filtersetsbrowsePathtoUA_EMPTY_ARRAY_SENTINELwhen the path is empty. It moves to adetailnamespace so a test can call it.Why. A Siemens S7-1500 answers a null BrowsePath with a null ConditionId. The plugin drops every event with a null ConditionId as a non-condition event, so no native
Program_Alarmever became a fault:How tested.
EventFilterTest.ConditionIdClauseSendsEmptyNotNullBrowsePath: encodes the clause, decodes it and checks it decodes as an empty array, not a null one. It fails on main (browsePathdecodes as NULL) and passes with the fix.2. Lock the shared log check in the announce-before-sweep test (was #777)
What.
DiscoverEndpoint.TheScanIsAnnouncedBeforeTheSweepRunsgets a mutex around the check and the assignment of a shared string.Why. The scan lambda runs on the sweep's worker threads. Two first probes could both see the string empty and assign it at once, which ThreadSanitizer reports as a data race. It only shows when two probes overlap, so it is rare.
How tested. A short sleep between the check and the assignment makes the race show on every TSan run. With the mutex and the same sleep, 10 of 10 runs are clean.
3. A secured client's config lives as long as the client (was #778)
What. The
UA_ClientConfiga secured client is built from lives on the heap inOpcuaClient::Impl(declared beforeclient, so it is destroyed after it). It is replaced only once the client built from the next one is in place. After the move it holds a default config, so the policy context logs through a valid stdout logger.Why. A gateway connected to a secured OPC UA server (Basic256Sha256, SignAndEncrypt) crashed with SIGSEGV when the link dropped:
In open62541 1.3 (bundled by open62541pp v0.16), each security policy context stores a pointer to the logger of the
UA_ClientConfigit was built in.apply_security_configbuilt that config as a localopcua::ClientConfigand moved it into the client. The existing 1.3 workaround re-pointssecurityPolicies[i].logger, but the policy context keeps its own copy of the pointer (ua_openssl_basic256sha256.c,cc->policyContext->logger). Onceconnect()returned, that pointer led into a dead stack frame. Closing a secured channel logs through it, so the crash came whenever that stack memory had been reused before the next link drop.How tested.
SecuredOpcuaClientReconnect.ChannelClosesAfterConnectReturned: secured connect totest_alarm_server --securebehind the link proxy, overwrite the dead stack, cut the link, pump until the channel closes, reconnect, disconnect. Without the fix it dies deterministically with SIGBUS at0xa5a5a5a5a5a5a5a5inUA_ChannelModule_Delete_Context, the stack seen in the field. With the fix it passes.4. x-plc-status says why the link to the server is down (was #779)
What.
GET /components/{id}/x-plc-statusgainslast_error: the OPC UA status name of the last failure of the link.BadCertificateUntrusted(the server's certificate is not in the trust list), the status of the server's ERR message such asBadSecurityChecksFailed(the server does not trust the client's certificate),BadUserAccessDenied(a refused user),BadTimeout/BadConnectionClosed(nothing answers)BadConfigurationErrorwhen the client's own certificate, key or security settings could not be appliedIt is cleared by a connect that succeeds. It has its own lock, so the route never waits behind a connect in progress.
Why. A client of the gateway saw
connected: falseand nothing else. A missing trust entry on either side, a wrong user and a server that is off all looked the same. open62541 1.3 ends a refused channel withBadConnectionClosed(ua_client.creceiveResponse) and only logs why.connectStatusholds the same code. So the client puts two taps on its own config: one in front of the certificate check of the server's certificate, and one in front of the logger that matches the exact format strings ofprocessERRResponse. Both forward to the original plugin.How tested.
test_opcua_client: a refused port names aBad...status that is not a certificate one, a contradictory security config givesBadConfigurationError, a new client has none.test_opcua_secured(real open62541 server, Basic256Sha256 SignAndEncrypt): wrong password givesconnected == false,last_error == BadUserAccessDenied. Server certificate not in the gateway's trust list givesconnected == false,last_error == BadCertificateUntrustedand no value served (without the taps this readsBadConnectionClosed). A second server that does not trust the gateway's certificate giveslast_error == BadSecurityChecksFailed.5. A refused session is not a connection, and a missing token policy says so (was #779)
What. Only an activated session counts as connected. A server that offers no token policy for the configured user is reported as
BadIdentityTokenInvalid(for example anonymous on a server that wants a user) instead of open62541'sBadInternalError.Why. A channel left open by a refused ActivateSession read as connected through open62541pp's
isConnected(). The fast path ofconnect()returned true,x-plc-statussaidconnected: trueand every read failed. With this check the client rebuilds on every retry of a refused session, which crashed before change 3 (SIGBUS inUA_ChannelModule_Aes128Sha256RsaOaep_Delete_Context, logging through a policy context's logger pointer to a dead stack frame). That is why change 3 comes first.How tested.
test_opcua_secured: anonymous gateway, server without an anonymous policy givesconnected == false,last_error == BadIdentityTokenInvalid. Without the session check this readsconnected: truewith every read failing.6. Node map entries addressed by browse path (was #780)
What. A node map entry can give a
browse_pathinstead of anode_id:Why. Companion specifications (OPC 40010 Robotics, Machinery, PackML, ...) fix the BrowseNames and the namespace URI of their nodes. They do not fix NodeIds, and the namespace index is the server's choice, which can change when it restarts. A map written against one server's NodeIds only fits that server. A browse path written against the specification fits every server that implements it.
How.
<alias>:<BrowseName>with aliases from a new top-levelnamespaces:map,<index>:<BrowseName>, or*for any child (an instance the vendor names). The last step must be a Variable.entries()until a session resolves it, so the poller never subscribes a null NodeId. It is resolved after connect, before auto_browse and before the subscribe, and again fromon_connectedon every new session: a path that was missing is retried, and a node that moved (namespaces renumbered) is re-keyed, publisher included.*, children are tried in browse order with backtracking, first full match wins, capped at 2000 children per path.How tested.
test_browse_path: parsing and its refusals, wildcard walk with backtracking, namespace matched by URI and not by index, last step must be a Variable, node budget, waiting entries, replacing an auto-browsed entry, following a node that moved on the next session, fault-code uniqueness, one address per entry.Not in this change.
auto_alarmsfolds the SourceNode NodeId string, index included, into the derived fault code, so the same condition gets another code after the namespaces are renumbered.7. State registers with several healthy values, and gates (was #781)
What.
fault_enum.ok_values: more healthy values besideok_value.inhibit_when/only_whenon any detection entry, gated on another polled point of the map:inhibit_when: no fault is raised while the gate point reads one ofvalues(changeover, setup, maintenance mode). A fault raised before keeps its state until its own value clears it.only_when: the rule is evaluated only while the gate matches, otherwise its faults clear (a stop reason that stays latched after the machine runs again).nodes:, names the entry itself, orvaluesis not a non-empty integer list. An unread gate point gates nothing.browse_pathentry (change 6). The gate point itself must be anode_identry, because a browse path entry has no NodeId until a session resolves it. The load checks the gates of both kinds of entry.Why. A state machine register (ISA-TR88 state numbers, Machinery item state) has many healthy states. Without the list each of them raises the catch-all. Many machine faults only mean something in some modes of the machine.
How tested.
test_fault_detection:OkValuesAreHealthyStates.test_node_map: parsing ofok_valuesand both gates, rejection of a gate on a missing point, on itself, and malformed lists.test_browse_path:AGateOnABrowsePathEntryJoinsWithIt(the gate survives the load and the resolve),RejectsAGateWhosePointIsMissingOrAddressedByBrowsePath.test_opcua_state_gates(real gateway, fault manager and fixture server): healthy states, aborted in production raises, a raised fault survives an inhibiting mode, aborted during an inhibiting mode is not raised and is raised once the mode ends, catch-all,only_whenignores a latched reason while running and raises it while stopped, planned reason raises nothing. Against the plugin without this change it fails 7 of its expectations.8. Array values are skipped instead of terminating the gateway (was #781)
What. A non-scalar value is now a bad read, so
auto_browseskips the node and polling or a subscription drops it.Why. A read of an array value terminated the gateway.
variant_to_valuehandles scalars only andgetScalarCopythrowsBadVariantAccesson an array of a matching type, which no read path caught.auto_browseagainst any server with array variables (seen on the umati sample server) disabled the plugin inset_contextor terminated the process on a later walk.How tested. The fixture server gets three Int32 registers and a read-only Int32 array (
Recipe.Steps). Without the fix,test_opcua_read_onlyandtest_opcua_state_gatesfail with the plugin disabled (Plugin 'opcua' threw during set_context(): Variant is not a scalar). With it both pass.9. ConditionRefresh reconciled per event monitored item (was #782)
What. The replayed set and the reconcile are kept per alarm source, and a RefreshEnd without its RefreshStart reconciles nothing.
Why. Part 9 brackets the refresh replay on every event item of the subscription, and each item carries only its own notifier's conditions. With two
event_alarmssources, the second item's RefreshEnd reconciled every tracked condition against its own burst and cleared the first source's live alarms after every reconnect.How tested.
test_opcua_poller:ConditionRefreshTest.EachSourceReconcilesOnlyItsOwnConditions(two sources, refresh bracketed on both, then one source alone, then a stray End). With the old reconcile it fails ({ "B_ALARM", "A_ALARM" }cleared, want{ "A_ALARM" }).10. An array event field no longer drops the notification (was #782)
What. Every scalar read in the event path and in the read-based condition replay checks
isScalar()first.Why. Change 8 makes an array value a bad read in the read path. The event path had the same
isType<T>()+getScalarCopypattern: an array Severity, Message, ConditionName or associated value threw out ofon_event, and the notification was dropped. Same in the read-based condition replay.How tested.
test_opcua_poller:EventFieldsTest.AnArrayFieldDoesNotDropTheEvent. It fails without the event-path guard.11. Report and clear of one fault code kept in order (was #782)
What. Calls for one fault code go out one at a time, each after the previous was answered (
FaultCodeLanes). Other codes do not wait, and an unanswered call holds its code for at most 5 s.Why. ReportFault and ClearFault are separate services, so an alarm that came and went inside one publishing interval could reach fault_manager clear first and stay raised for good (seen:
Fault cleared: Xfollowed byNew fault reported: X).How tested.
test_fault_code_lanes(no ROS): order kept for one code, other codes do not wait, several calls in a row, stale call released and its late answer ignored, synchronous answers.12. A read-only guard alone no longer turns auto_browse on (was #782)
What. Without
enabled, only a walk knob (root_nodes,max_depth, ...) turns auto_browse on.match_messageis accepted by theevent_alarmsmapping key check.Why. The object form defaulted
enabledto true, soauto_browse.infer_writable: falseset beside a curated node map turned the walk on.match_messagewas parsed but logged as an unknown key and "ignored".How tested.
test_opcua_plugin:InferWritableAloneKeepsTheWalkOff,WalkKnobWithoutEnabledTurnsTheWalkOn.Testing
All runs on the combined branch, in
ros:jazzy-ros-base(arm64), read-only build unless noted.ros2_medkit_fault_detectionandros2_medkit_opcuabuilt at each of the 12 commits, tests included.--packages-up-to ros2_medkit_opcua ros2_medkit_fault_manager,ROS2_MEDKIT_OPCUA_SECURE_REQUIRE=1),colcon testwith linters:ros2_medkit_opcua: ctest 31/31 (gtest, the reconnect suite, all launch integration tests, clang_format, copyright, cppcheck, flake8, lint_cmake, pep257, xmllint).test_alarm_server_smokeruns with asyncua installed.ros2_medkit_fault_detection: ctest 7/7.colcon test-result: 639 tests, 0 errors, 0 failures, 39 skipped (cppcheck cases, which ament skips on this distro).test_opcua_securedwith its fourlast_errorcases,test_opcua_state_gates) pass.-DCMAKE_BUILD_TYPE=RelWithDebInfo -DSANITIZER=asan,ubsan, timeouts x3,MEDKIT_TEST_TIME_SCALE=3, the CIASAN_OPTIONS/UBSAN_OPTIONS):ros2_medkit_opcuactest 24/24,ros2_medkit_fault_detection2/2. 446 + 17 test cases, 0 failures, no sanitizer report.-DSANITIZER=tsan,ctest -j1, the repository suppressions, timeouts x3):ros2_medkit_opcuactest 24/24,ros2_medkit_fault_detection2/2. 446 + 17 test cases, 0 failures, no ThreadSanitizer warning.run_alarm_tests.sh): read-only 25 checks and write-capable 23 checks, all scenarios passed (lifecycle, shelving, disabled alarm, status word, fault enum, reconnect after server restart).run_integration_tests.sh, read-only image): 29/29.run-clang-tidy,-header-filter='.*ros2_medkit.*') on the 21 changed C++ files: no finding on the lines this branch adds or changes.