Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent "BadUnexpectedError" from OpenSecureChannel #197

Open
LukeSawyers opened this issue Nov 29, 2024 · 7 comments
Open

Inconsistent "BadUnexpectedError" from OpenSecureChannel #197

LukeSawyers opened this issue Nov 29, 2024 · 7 comments

Comments

@LukeSawyers
Copy link

Before I start I just wanted to say thank you for making this library, this is a much more accessible alternative to the official repo. This issue is the only thing currently giving me pause.

client.OpenSecureChannel(MessageSecurityMode.None, SecurityPolicy.None, null);

Irregularly returns BadUnexpectedError when connecting to Omron NJ PLC embedded OPC UA Server. I say irregularly because it seems to depend on the configuration the code is run in - the issue almost never occurs in debug, sometimes in release, and quite often when running in docker.

Any insight here would be helpful, I'm thinking this might be a race condition of some kind

@nauful
Copy link
Owner

nauful commented Nov 29, 2024

I can't reproduce this locally. Can you take a Wireshark capture of the connection attempt when it fails?

@LukeSawyers
Copy link
Author

BadUnexpectedErrorCap.zip

Occurs 8 times before finally connecting

@nauful
Copy link
Owner

nauful commented Nov 29, 2024

It looks like the TCP connection is aborted on the client's side before a response is received. Can you try running TestServer and TestClient together?

@LukeSawyers
Copy link
Author

TestClient&Server.zip

FYI Platform is Ubuntu 24.04, .NET 8.0.110

@nauful
Copy link
Owner

nauful commented Nov 29, 2024

I don't see any disconnects in this capture, did it work fine?

If so, it may be related to Ubuntu or the docker setup. The initial connection with security disabled is blocking, so I don't see anything that would cause a timing issue. The only error I can find is the TCP connection gets cut on the client side and the client doesn't receive any response from the server.

@LukeSawyers
Copy link
Author

I’m happy to dig into this further, would you be able to point out where this would be most likely to occur and any logging that could be done to track this down?

@nauful
Copy link
Owner

nauful commented Nov 29, 2024

TestClient:

var openRes = client.OpenSecureChannel(messageSecurityMode, securityPolicy, serverCert);

This is the method in the UA client:

private StatusCode OpenSecureChannelInternal(bool renew)

Incoming bytes are consumed here, perhaps check this for failures:

private int Consume(SLChannel config, MemoryBuffer recvBuf)

If a message fails to process due to an error, this will trigger:

if (recvAccumSize == -1 || recvAccumSize >= MaximumMessageSize)

If your environment closes the connection, this will trigger:

if (bytesRead == 0)

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

No branches or pull requests

2 participants