Skip to content

AHC holds onto its body stream writer even after the request is complete #663

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

Closed
weissi opened this issue Feb 3, 2023 · 1 comment · Fixed by #665
Closed

AHC holds onto its body stream writer even after the request is complete #663

weissi opened this issue Feb 3, 2023 · 1 comment · Fixed by #665

Comments

@weissi
Copy link
Contributor

weissi commented Feb 3, 2023

AHC seems to hold onto its body streaming delegate closure even after the request is complete. That means it's pretty much impossible to use it for bidi streaming without leaking it.

test case

    func testStuff() throws {
        let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        defer {
            XCTAssertNoThrow(try group.syncShutdownGracefully())
        }

        let server = try ServerBootstrap(group: group)
            .childChannelInitializer { channel in
                channel.eventLoop.scheduleTask(in: .milliseconds(500)) {
                    channel.writeAndFlush(ByteBuffer(string: "HTTP/1.1 200 ok\r\ncontent-length: 2\r\n\r\nok")).map {
                        channel.close(promise: nil)
                    }.whenComplete { result in
                        print("server write complete", result)
                    }
                }
                return channel.eventLoop.makeSucceededFuture(())
            }
            .bind(host: "127.1", port: 0).wait()
        defer {
            XCTAssertNoThrow(try server.close().wait())
        }
        var request = try HTTPClient.Request(url: "http://\(server.localAddress!.ipAddress!):\(server.localAddress!.port!)/",
                                             method: .POST)
        let writerPromise = self.group.any().makePromise(of: HTTPClient.Body.StreamWriter.self)
        let donePromise = self.group.any().makePromise(of: Void.self)
        class LeakDetector {
            init() {
                print("INIT")
            }

            deinit {
                print("DEINIT")
            }
        }
        var leakDetector: LeakDetector? = LeakDetector()
        request.body = .stream { [leakDetector] writer in
            _ = leakDetector
            writerPromise.succeed(writer)
            return donePromise.futureResult
        }
        weak var weakLeak: LeakDetector? = leakDetector
        leakDetector = nil

        let resultFuture = self.httpClient.execute(request: request)
        request.body = nil // this is pointless but whatever
        writerPromise.futureResult.whenSuccess { writer in
            writer.write(.byteBuffer(ByteBuffer(string: "hello"))).map {
                print("written")
            }.cascade(to: donePromise)
        }
        print("HTTP sent")

        let result = try resultFuture.wait()

        XCTAssertEqual(.ok, result.status)
        XCTAssertEqual("ok", result.body.map { String(buffer: $0) } ?? "none")
        print("HTTP done", result)
        while weakLeak != nil {
            print("not nil")
            sleep(1)
        }
    }
@weissi weissi changed the title AHC holds onto its body streaming delegate even after the request is complete AHC holds onto its body stream writer even after the request is complete Feb 3, 2023
@weissi
Copy link
Contributor Author

weissi commented Feb 5, 2023

Presumably the easiest fix is to stop holding onto Request.body in the RequestBag type once we've started sending the body.

The retain cycle is something like RequestBag -> Request -> (if it captures a promise to be fulfilled with BodyStreamWiter) BodyStreamWriter -> RequestBag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant