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

Client-Server connection not established using Swift-NIO #3064

Open
amore4193 opened this issue Jan 16, 2025 · 5 comments
Open

Client-Server connection not established using Swift-NIO #3064

amore4193 opened this issue Jan 16, 2025 · 5 comments

Comments

@amore4193
Copy link

amore4193 commented Jan 16, 2025

We are experiencing an issue where the client and server connection is not being established. We are using the Swift-NIO library with the following versions:

This is the reference code for the client. the issue the happening after updating Xcode version - 15.1.0 to 16.1.0 for SDK 18 support.
https://github.com/apple/swift-nio/blob/2.40.0/Sources/NIOWebSocketClient/main.swift

SwiftNIO: 2.40.0
SwiftNIOHTTP1: 2.40.0
SwiftNIOWebSocket: 2.40.0
SwiftNIOSSL: 2.19.0

Client-side: Xcode 16.1.0

Expected Behavior
The client should successfully connect to the server without issues.

Actual Behavior
We are getting the upgraderDeniedUpgrade error from errorCaught method of class HTTPInitialRequestHandler

The connection between the client and server fails without any clear errors. We have verified that both sides are running compatible Swift-NIO above versions. any guidance, suggestions, or help diagnosing this issue would be greatly appreciated.

@Lukasa
Copy link
Contributor

Lukasa commented Jan 17, 2025

Thanks for filing this issue. UpgraderDeniedUpgrade implies the server is refusing the upgrade. Do you control the server in question?

@amore4193
Copy link
Author

amore4193 commented Jan 17, 2025

The server is also using Swift-NIO (the same version as the client as above). The connection was working fine with Xcode 15.1.0, but after upgrading to Xcode 16.1.0 at the client end (for SDK 18 support), we started seeing the upgraderDeniedUpgrade error.

@Lukasa
Copy link
Contributor

Lukasa commented Jan 17, 2025

Are any errors printed on the server side?

@amore4193
Copy link
Author

amore4193 commented Jan 22, 2025

We do not see any errors logged on the server side. Please let us know if we need to add any specific logging code in a particular class or method to capture the required logs.

Currently, our WebSocket server is implemented based on the following sample code:

Swift-NIO WebSocket Server

We have overridden the channelActive method of the ChannelInboundHandler interface in the HTTPHandler class. We observe that the control reaches this method, and we can log the client address when it connects. However, the connection gets disconnected shortly thereafter.

Additionally, we verified that the connection fails during client-side verification, specifically in the shouldAllowUpgrade method of the following class:

NIOWebSocketClientUpgrader.swift

It seems like this method is returning false.

Note: This works in the simulator but does not if the app runs on devices.

@Lukasa
Copy link
Contributor

Lukasa commented Jan 22, 2025

Ok, let's add some debugging. Add this class:

final class ErrorHandler: ChannelInboundHandler {
    typealias InboundIn = Any
    typealias InboundOut = Any

    func errorCaught(context: ChannelHandlerContext, error: any Error) {
        // Replace this print with any logging you'd prefer
        print(error)
        context.fireErrorCaught(error)
    }
}

Then, amend

return channel.pipeline.configureHTTPServerPipeline(withServerUpgrade: config).flatMap {
channel.pipeline.addHandler(httpHandler)
}
to be:

        return channel.pipeline.configureHTTPServerPipeline(withServerUpgrade: config).flatMap {
            channel.pipeline.addHandler(httpHandler)
        }.flatMap {
            channel.pipeline.addHandler(ErrorHandler())
        }

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