Vapor and Sendable Route Handlers

Hi.

After adding .enableExperimentalFeature("StrictConcurrency") to my Vapor executable target, I am getting converting non-sendable function value to '@Sendable (Request) async throws -> Response' may introduce data races warning on all my request handlers inside my RouteCollection controllers.

Here is an example of such a controller:

struct AuthController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        let auth = routes.grouped("auth")

        auth.post("login", use: login(req:))
    }

    func login(req: Request) async throws -> Response {
        // some code goes here
    }
}

Should I ignore converting non-sendable function value to '@Sendable (Request) async throws -> Response warning, or should I manually add @Sendable to all handlers? Is there a better way?

Thank you.

2 Likes

You can ignore this warning, it's an unfortunate transition point before Swift 6 where the compiler isn't clever enough to work out things are safe when they are. You can mark the function @Sendable but I would recommend against it - this is the equivalent of marking it unchecked sendable, instead of the equivalent of a closure where it enforces sensibility (I know, don't ask).

You can see how we workaround this issue in the updated template

3 Likes