You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Assertion 'Response body stream writer deinitialized before .end or .error was sent.' hit in Vapor when using body.collect in Middleware & streaming
#2933
I've got a Vapor app which has a small Middleware which just attempts to print the first bits of the incoming body and then just continue the request chain. This appears to work fine until I use a route (/echo) which uses body streaming.
When I then post to that route, Vapor hits this assertion
assert(self.isComplete, "Response body stream writer deinitialized before .end or .error was sent.")
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer { app.shutdown() }
struct MyMiddleware: Middleware {
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
request.body.collect(max: 1).whenSuccess { print("first body bits", $0.map { String(buffer: $0) } ?? "n/a") }
return next.respond(to: request)
}
}
app.middleware.use(MyMiddleware(), at: .beginning)
app.on(.POST, "echo", body: .stream) { request -> Response in
let r = Response(body: .init(stream: { writer in
request.body.drain { body in
switch body {
case .buffer(let buffer):
return writer.write(.buffer(buffer))
case .error(let error):
return writer.write(.error(error))
case .end:
return writer.write(.end)
}
}
}))
r.headers.add(name: "content-type", value: "application/octet-stream")
return r
}
try app.run()
To trigger the crash, you can post anything that's like 2000 bytes or more usually. For example
@mkll Hmm, but the middleware doesn't really know anything about the request, right? Basically this would mean that any middleware that looks into the body at all is incompatible with all routes that stream, correct?
Because body.drain from a middleware also doesn't work.
If that's all working as designed, then at least a better error message would be good, no?
I've got a Vapor app which has a small Middleware which just attempts to print the first bits of the incoming body and then just continue the request chain. This appears to work fine until I use a route (
/echo
) which uses body streaming.When I then post to that route, Vapor hits this assertion
To trigger the crash, you can post anything that's like 2000 bytes or more usually. For example
When the middleware isn't used, this appears to work ok.
Vapor: 4.68.0
The text was updated successfully, but these errors were encountered: