Skip to content

async-await: Move to completed state before cancelling task during finish() #1302

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

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/GRPC/AsyncAwaitSupport/GRPCAsyncServerHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ internal final class AsyncServerHandler<
self.state = .completed

case .active:
self.state = .completed
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix #1299

self.interceptors = nil
self.userHandlerTask?.cancel()

case .completed:
Expand Down Expand Up @@ -524,8 +526,10 @@ internal final class AsyncServerHandler<
self.interceptors.send(.message(response, metadata), promise: nil)

case .completed:
/// If we are in the completed state then the async writer delegate must have terminated.
preconditionFailure()
/// If we are in the completed state then the async writer delegate will have been cancelled,
/// however the cancellation is asynchronous so there's a chance that we receive this callback
/// after that has happened. We can drop the response.
()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix #1301

}
}

Expand Down
17 changes: 14 additions & 3 deletions Tests/GRPCTests/AsyncAwaitSupport/AsyncIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ final class AsyncIntegrationTests: GRPCTestCase {
}

override func tearDown() {
XCTAssertNoThrow(try self.client.close().wait())
XCTAssertNoThrow(try self.server.close().wait())
XCTAssertNoThrow(try self.group.syncShutdownGracefully())
XCTAssertNoThrow(try self.client?.close().wait())
XCTAssertNoThrow(try self.server?.close().wait())
XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
super.tearDown()
}

Expand Down Expand Up @@ -195,6 +195,17 @@ final class AsyncIntegrationTests: GRPCTestCase {
])
}
}

func testServerCloseAfterMessage() {
XCTAsyncTest {
let update = self.echo.makeUpdateCall()
try await update.requestStream.send(.with { $0.text = "hello" })
_ = try await update.responses.first(where: { _ in true })
XCTAssertNoThrow(try self.server.close().wait())
self.server = nil // So that tearDown() does not call close() again.
try await update.requestStream.finish()
}
}
}

extension HPACKHeaders {
Expand Down
36 changes: 34 additions & 2 deletions Tests/GRPCTests/GRPCAsyncServerHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {

await assertThat(self.recorder.metadata, .nil())
await assertThat(self.recorder.messages, .isEmpty())
await assertThat(self.recorder.status, .notNil(.hasCode(.unavailable)))
await assertThat(self.recorder.trailers, .is([:]))
await assertThat(self.recorder.status, .nil())
await assertThat(self.recorder.trailers, .nil())
} }

func testFinishAfterMessage() { XCTAsyncTest {
Expand All @@ -296,6 +296,38 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
// Wait for tasks to finish.
await handler.userHandlerTask?.value

await assertThat(self.recorder.messages.first, .is(ByteBuffer(string: "hello")))
await assertThat(self.recorder.status, .nil())
await assertThat(self.recorder.trailers, .nil())
} }

func testErrorAfterHeaders() { XCTAsyncTest {
let handler = self.makeHandler(observer: self.echo(requests:responseStreamWriter:context:))

handler.receiveMetadata([:])
handler.receiveError(CancellationError())

// Wait for tasks to finish.
await handler.userHandlerTask?.value

await assertThat(self.recorder.status, .notNil(.hasCode(.unavailable)))
await assertThat(self.recorder.trailers, .is([:]))
} }

func testErrorAfterMessage() { XCTAsyncTest {
let handler = self.makeHandler(observer: self.echo(requests:responseStreamWriter:context:))

handler.receiveMetadata([:])
handler.receiveMessage(ByteBuffer(string: "hello"))

// Wait for the async user function to have processed the message.
try self.recorder.recordedMessagePromise.futureResult.wait()

handler.receiveError(CancellationError())

// Wait for tasks to finish.
await handler.userHandlerTask?.value

await assertThat(self.recorder.messages.first, .is(ByteBuffer(string: "hello")))
await assertThat(self.recorder.status, .notNil(.hasCode(.unavailable)))
await assertThat(self.recorder.trailers, .is([:]))
Expand Down