From 7909ac436f6c8474f735b7e32838ecbb03f5cff7 Mon Sep 17 00:00:00 2001 From: Vincent Le Normand Date: Fri, 23 Jun 2023 11:59:36 +0200 Subject: [PATCH 1/2] When sending a reply, check if it succeed or not. If it fails, send a failure instead of a success. --- Sources/SecureXPC/SequentialResultProvider.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/SecureXPC/SequentialResultProvider.swift b/Sources/SecureXPC/SequentialResultProvider.swift index f5a659f..e0d2948 100644 --- a/Sources/SecureXPC/SequentialResultProvider.swift +++ b/Sources/SecureXPC/SequentialResultProvider.swift @@ -239,8 +239,12 @@ public class SequentialResultProvider { do { try encodingWork(&response) if let deliveryHandler = deliveryHandler { - xpc_connection_send_message_with_reply(connection, response, nil) { _ in - deliveryHandler(.success(())) + xpc_connection_send_message_with_reply(connection, response, nil) { result in + if xpc_get_type(result) == XPC_TYPE_ERROR { + deliveryHandler(.failure(XPCError.fromXPCObject(result))) + } else { + deliveryHandler(.success(())) + } } } else { xpc_connection_send_message(connection, response) From 687ea6d82d3f9f537b6371b28fe7e0255db86c67 Mon Sep 17 00:00:00 2001 From: Vincent Le Normand Date: Fri, 23 Jun 2023 13:41:09 +0200 Subject: [PATCH 2/2] Fixed tests. The test was validating no error for the third message, but it was not sent because an AsyncSequence is finished when an error has been thrown --- .../Sequential Result Tests.swift | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Tests/SecureXPCTests/Client & Server/Sequential Result Tests.swift b/Tests/SecureXPCTests/Client & Server/Sequential Result Tests.swift index 4232338..2e50dc8 100644 --- a/Tests/SecureXPCTests/Client & Server/Sequential Result Tests.swift +++ b/Tests/SecureXPCTests/Client & Server/Sequential Result Tests.swift @@ -313,7 +313,8 @@ class SequentialResultTests: XCTestCase { } } - let expectation = self.expectation(description: "The second sequence value won't be decodable") + let secondSequenceValueNotDecodableExpectation = self.expectation(description: "The second sequence value won't be decodable") + let thirdSequenceValueSentExpectation = self.expectation(description: "Attempted to send third sequence value") enum ExampleError: Error, Codable { case didNotWork } @@ -325,10 +326,19 @@ class SequentialResultTests: XCTestCase { do { try await provider.success(value: .noValue) try await provider.success(value: .alwaysFailedDecode(NotActuallyDecodable())) - try await provider.success(value: .noValue) } catch { XCTFail("Unexpected error: \(error)") } + do { + try await provider.success(value: .noValue) + XCTFail("Sending the third reply should fail with an XPCError.connectionInterrupted error") + } + catch XPCError.connectionInterrupted { + // Expected error + } catch { + XCTFail("Unexpected error: \(error)") + } + thirdSequenceValueSentExpectation.fulfill() } let sequence = client.send(to: route) @@ -340,10 +350,10 @@ class SequentialResultTests: XCTestCase { _ = try await iterator.next() } catch { if case XPCError.decodingError(_) = error { - expectation.fulfill() + secondSequenceValueNotDecodableExpectation.fulfill() } } - + await self.waitForExpectations(timeout: 1) }