Skip to content

fix(opcua): keep a secured client's config alive as long as the client - #778

Closed
mfaferek93 wants to merge 1 commit into
mainfrom
fix/opcua-secured-config-outlives-client
Closed

mfaferek93 wants to merge 1 commit into
mainfrom
fix/opcua-secured-config-outlives-client

Conversation

@mfaferek93

Copy link
Copy Markdown
Collaborator

What

A gateway connected to a secured OPC UA server (Basic256Sha256, SignAndEncrypt) crashed with SIGSEGV when the link dropped:

#0  0x005b205d32353138 in ?? ()
#1  UA_LOG_INFO.constprop.0 ()
#2  UA_ChannelModule_Delete_Context ()
#3  UA_SecureChannel_close ()
#4  closeSecureChannel ()
#5  receiveResponse ()
#6  UA_Client_run_iterate ()
...
#9  ros2_medkit_gateway::OpcuaPoller::event_pump_loop()

In open62541 1.3 (bundled by open62541pp v0.16), each security policy context stores a pointer to the logger of the UA_ClientConfig it was built in. apply_security_config built that config as a local opcua::ClientConfig and moved it into the client. The existing 1.3 workaround re-points securityPolicies[i].logger, but the policy context keeps its own copy of the pointer (ua_openssl_basic256sha256.c, cc->policyContext->logger). Once connect() 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.

Fix

The config lives on the heap in OpcuaClient::Impl (declared before client, so it is destroyed after it) and 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.

Testing

  • New SecuredOpcuaClientReconnect.ChannelClosesAfterConnectReturned: secured connect to test_alarm_server --secure behind 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 at 0xa5a5a5a5a5a5a5a5 in UA_ChannelModule_Delete_Context, the stack seen in the field. With the fix it passes (3 of 3 runs).
  • colcon test --packages-select ros2_medkit_opcua: all tests pass, clang-format clean.
  • Field: the crash came twice in a few minutes on an aarch64 gateway reading a secured server through a link that went down and up. Same backtrace from the core dump.

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).
Copilot AI balanced review requested due to automatic review settings November 8, 2026 23:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change addressing the secured OPC UA client’s logger lifetime.

No open issues 🎉

What changed in this PR

Fixes a secured OPC UA client crash when a connection drops by keeping the configuration’s logger alive for the lifetime of the client that references it.

Changes:

  • Retains the security configuration on the heap and preserves its lifetime during client replacement and destruction.
  • Adds a regression test covering secured connection loss, reconnect, and explicit disconnect after stack memory is overwritten.
  • Provides the reconnect test with the certificate-generation script path.
File Description
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​test/​test_opcua_client_reconnect.cpp Adds a secured-server fixture and logger-lifetime regression test.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​src/​opcua_client.cpp Keeps the security configuration alive until its associated client is destroyed.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​CMakeLists.txt Exposes the certificate-generation script path to the reconnect test.

Review effort: Balanced

@mfaferek93

Copy link
Copy Markdown
Collaborator Author

Superseded by #783, which carries this change as its own commit.

@mfaferek93 mfaferek93 closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants