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

HTTP echo server crashes with unfulfilled promise if client uses EOF framing #2391

Closed
weissi opened this issue Jun 10, 2020 · 0 comments · Fixed by #2404
Closed

HTTP echo server crashes with unfulfilled promise if client uses EOF framing #2391

weissi opened this issue Jun 10, 2020 · 0 comments · Fixed by #2404
Labels
bug Something isn't working
Projects

Comments

@weissi
Copy link
Contributor

weissi commented Jun 10, 2020

I tried to implement a HTTP echo server and it always crashes with a promise leak

import Vapor

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

let app = Application(env)
defer { app.shutdown() }

app.on(.POST, "echo")  { request -> Response in
    return 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)
            }
        }
    }, count: 0))
}

try app.run()

Repro:

  • run the above program
  • echo -ne 'POST /echo HTTP/1.1\r\n\r\n' | nc localhost 8080 (note this doesn't send a content-length or transfer-encoding header which means it's EOF framed (message ends on EOF, NIO will handle that though and send you an .end on EOF)).

Result:

Fatal error: leaking promise created at (file: ".../checkouts/vapor/Sources/Vapor/HTTP/Server/HTTPServerHandler.swift", line: 53): file .../checkouts/vapor/Sources/Vapor/HTTP/Server/HTTPServerHandler.swift, line 53
@weissi weissi changed the title HTTP echo server crashes with unfulfilled promise HTTP echo server crashes with unfulfilled promise if client misbehaves Jun 10, 2020
@weissi weissi changed the title HTTP echo server crashes with unfulfilled promise if client misbehaves HTTP echo server crashes with unfulfilled promise if client uses EOF framing Jun 10, 2020
tanner0101 added a commit that referenced this issue Jun 23, 2020
@tanner0101 tanner0101 added the bug Something isn't working label Jun 23, 2020
@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Jun 23, 2020
Vapor 4 automation moved this from To Do to Done Jun 24, 2020
tanner0101 added a commit that referenced this issue Jun 24, 2020
* echo server

* warning

* fix tests

* drain unhandled request after response end

* rm commented code

* add handler tests

* test EOF framing, fixes #2391

* test incorrect response body stream count, fixes #2392

* http fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Vapor 4
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants