vapor / websocket-kit

WebSocket client library built on SwiftNIO
https://docs.vapor.codes/4.0/advanced/websockets/
MIT License
272 stars 79 forks source link

NIOCore/NIOLoopBound.swift:120: Precondition failed in 2.14.0 #140

Open jhoughjr opened 1 year ago

jhoughjr commented 1 year ago

Describe the bug

I'm seeing a precondition failed when setting onText or onBinary on an upgraded websocket.

To Reproduce

vapor new testing; cd testing; open Package.swift;

add the following route to routes.swift

  app.webSocket("test",
                  maxFrameSize: WebSocketMaxFrameSize.default) { req in
        return req.headers

    } onUpgrade: { req, webSoc in

        webSoc.onText { ws, text in
            app.logger.info("\(text)")
        }
    }

Build and run. Then connect to ws://localhost:8080/test with a websocket client. When the onText handler is added, the server will crash with precondition failed.

Expected behavior

A properly added onText or onBinary handler that does not fail the box's precondition and crash.

Environment

MacOS 13.4, m1 Mac mini 2020 Xcode 14.3.1 Vapor: 4.77.0 toolbox: 18.7.1 websocket-kit: 2.14.0

Additional context

Add any other context about the problem here.


[ INFO ] GET /test [request-id: 1E6A83B5-84A4-4234-B5B2-B705F9F8C8BB]
NIOCore/NIOLoopBound.swift:120: Precondition failed
2023-06-14 08:23:57.758835-0500 App[44596:2119282] NIOCore/NIOLoopBound.swift:120: Precondition failed```
abidon commented 1 year ago

I have the same bug. Is there a fix in the pipeline or a workaround ?

vasek-broz commented 1 year ago

I am getting into almost the same trouble. Only the backtrace points to a different file and line in NIOCore. It happens on localhost during development as well as on Heroku where I host the application. I have also found out that ti started happening from version 2.13.0 so I am for now staying with 2.12.1 specified in package manifest.

m-barthelemy commented 11 months ago

Same here using websocket-kit as a client.

florianpeninon commented 10 months ago

Facing the same bug. Any update on a fix/workaround?

mjKosmic commented 10 months ago

i had the same issue when i was accidentally mixing escaping closure syntax and async/await in my websocket routes. fixing that fixed the problem for me

0xTim commented 10 months ago

You should be able to wrap your onText etc calls inside an event loop submit or similar to remove. But we are working on this and trying to fix the underlying issue

laugonz commented 4 months ago

i had the same issue when i was accidentally mixing escaping closure syntax and async/await in my websocket routes. fixing that fixed the problem for me

Hey, thanks a ton! Your tip about not mixing up escaping closures with async/await was spot-on. I wrapped my async/await stuff in a Task { ... }, and this fixed the issue.