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

Precondition Failed error #15

Open
WBIT opened this issue Dec 19, 2020 · 1 comment · May be fixed by #16
Open

Precondition Failed error #15

WBIT opened this issue Dec 19, 2020 · 1 comment · May be fixed by #16

Comments

@WBIT
Copy link

WBIT commented Dec 19, 2020

I'm getting the following error when attempting to send an SMS:

Precondition failed: file /Users/wanderbit/Documents/VaporDevelopment/arframer/.build/checkouts/swift-nio/Sources/NIO/ChannelPipeline.swift, line 1447
Illegal instruction: 4

Here's my route:

app.post("sendsms")  { req -> EventLoopFuture<ClientResponse> in
        let sms = OutgoingSMS(body: "hi", from: "+15555555555, to: "+15555555556")
        return req.twilio.send(sms)
    }

I believe the issue is that this package sends an sms with application.client. Would it be better to pass in a client to the send() function so the work could be done on the request instead of the application?

@WBIT
Copy link
Author

WBIT commented Dec 19, 2020

I believe the issue is here where we get the application's eventloopgroup even though we could be inside of a request:

    public func send(_ sms: OutgoingSMS) -> EventLoopFuture<ClientResponse> {
        guard let configuration = self.configuration else {
            fatalError("Twilio not configured. Use app.twilio.configuration = ...")
        }
        
        return application.eventLoopGroup.future().flatMapThrowing { _ -> HTTPHeaders in
            let authKeyEncoded = try self.encode(accountId: configuration.accountId, accountSecret: configuration.accountSecret)
            var headers = HTTPHeaders()
            headers.add(name: .authorization, value: "Basic \(authKeyEncoded)")
            return headers
        }.flatMap { headers in
            let twilioURI = URI(string: "https://api.twilio.com/2010-04-01/Accounts/\(configuration.accountId)/Messages.json")
            return self.application.client.post(twilioURI, headers: headers) {
                try $0.content.encode(sms, as: .urlEncodedForm)
            }
        }
    }

According to the Vapor docs:

Vapor expects that route closures will stay on req.eventLoop. If you hop threads, you must ensure access to Request and the final response future all happen on the request's event loop.

I think when we call application.eventLoopGroup.future() we're jumping outside of the requests event loop and causing an issue.

@DenTelezhkin DenTelezhkin linked a pull request Jan 27, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant