Visitar URL original
[Swift] Swift IPC handlers should return their reply and may be async [reland] by adetaylor · Pull Request #76310 · WebKit/WebKit · GitHub
Skip to content

[Swift] Swift IPC handlers should return their reply and may be async [reland] - #76310

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
adetaylor:eng/reland-swift-ipc-async-handlers
Oct 9, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
adetaylor:eng/reland-swift-ipc-async-handlers

Conversation

@adetaylor

@adetaylor adetaylor commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

9fdd260

[Swift] Swift IPC handlers should return their reply and may be async [reland]
https://bugs.webkit.org/show_bug.cgi?id=324524

Reviewed by Richard Robinson

A handler for a message with a reply now returns it, and any handler may be async. For instance,
IPCTesterReceiver's deferred reply no longer deals with WTF::CompletionHandler and becomes
simply:

    func deferredReplyMessage(connection: IPC.Connection, arg1: UInt32) async -> UInt64 {
        deferredReplyArgument = arg1
        return await withCheckedContinuation { pendingDeferredReply = $0 }
    }

so the reply is whatever the handler returns, whenever it returns. It cannot forget to
reply or reply twice, and no C++ reply type appears in its signature.

Async or not, throwing or not, all four combinations go through one generated call, since a
single `await` and a single `try` each cover a whole expression:

    let reply = try await mayThrowInvalidMessage(target.foo(connection: connection, ...))

Nothing in .messages.in says whether a handler is async: handleMessageSynchronous moves the
reply encoder into the completion handler, so Synchronous only means the sender blocks, and
deferring is the receiver's choice. A handler without a reply may need to await too, so every
message is dispatched the same way.

Everything runs on the main thread. Handlers are @MainActor. @MainActor functions are not
exposed to C++, so the generated wrapper stays synchronous, enters the actor with
MainActor.assumeIsolated - which also asserts IPC dispatched us on the main thread - and
starts the handler with Task.immediate, which runs it inline until its first suspension and
inherits the actor, so the remainder stays on the main thread too. Running inline is what keeps
Swift-handled messages in order with C++-handled ones; Task.init would not. The body lives in a
@MainActor static method because a typed `catch` inside a closure widens to `any Error`.
mayThrowInvalidMessage's async overload is nonisolated(nonsending), without which awaiting it
would send the reply out of the actor.

Adds -Werror RegionIsolation, without which none of this is checked: data-race diagnostics
are only warnings when a C++ type is involved, because C++ modules carry no concurrency
annotations. That is most of our Swift.

None of this crosses a real boundary - it is one thread and one stack frame - but the wrapper
must be nonisolated for C++ to call it, because swiftc silently omits @MainActor members from
the generated C++ header.

Everything under Scripts/webkit/tests/ is messages_unittest.py -r output, except
TestWithSwift.messages.in, which gains a message with an empty reply.

This is a reland; the previous version hit a Swift compiler bug rdar://189495426 on Windows
alone. This is worked around in this version by packing the function arguments into a struct.

* Source/WebKit/Platform/IPC/MessageCheck.swift:
(mayThrowInvalidMessage(_:)):
(Task.immediateOnMainActor(_:)):
(InvalidMessage.markMessageInvalid(_:on:)): Deleted.
* Source/WebKit/Scripts/webkit/messages.py:
(swift_reply_completion_argument):
(generate_swift_message_handler):
(swift_dispatch_target_and_function):
* Source/WebKit/Scripts/webkit/tests/TestWithStreamSwiftEnabledByMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamSwiftMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftConditionallyAndEnabledByMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftConditionallyMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessageReceiver.swift:
* Source/WebKit/Shared/IPCTesterReceiver.swift:
(IPCTesterReceiver.asyncMessage):
(IPCTesterReceiver.deferredReplyMessage):
(IPCTesterReceiver.completeDeferredReply(connection:arg1:)):
(IPCTesterReceiver.pendingDeferredReply):
(IPCTesterReceiver.deferredReply): Deleted.
(IPCTesterReceiver.completeDeferredReply(_:arg1:)):
* Source/WebKit/UIProcess/WebBackForwardList.swift:
(WebBackForwardList.backForwardAddItem):
(WebBackForwardList.backForwardSetChildItem):
(WebBackForwardList.backForwardClearChildren):
(WebBackForwardList.backForwardUpdateItem):
(WebBackForwardList.backForwardGoToItem):
(WebBackForwardList.backForwardListContainsItem):
(WebBackForwardList.backForwardAllItems):
(WebBackForwardList.backForwardItemAtIndexForWebContent):
(WebBackForwardList.backForwardListCounts):
* Source/cmake/WebKitSwiftFlags.cmake:
* Source/WTF/wtf/PlatformHave.h:
* Source/WebKit/Configurations/AllowedSPI.toml:
* Source/WebKit/Platform/spi/Cocoa/_Concurrency_SPI.swiftinterface: Added.
* Source/WebKit/Scripts/webkit/tests/MessageArgumentDescriptions.cpp:
(IPC::jsValueForArguments):
(IPC::jsValueForReplyArguments):
(IPC::messageArgumentDescriptions):
(IPC::messageReplyArgumentDescriptions):
* Source/WebKit/Scripts/webkit/tests/MessageNames.cpp:
* Source/WebKit/Scripts/webkit/tests/MessageNames.h:
* Source/WebKit/Scripts/webkit/tests/TestWithSwift.messages.in:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessageReceiver.cpp:
(WebKit::TestWithSwiftMessageForwarder::didReceiveMessage):
(CompletionHandlers::TestWithSwift::completeWithDefaultReply):
(IPC::jsValueForDecodedMessage<MessageName::TestWithSwift_TestMessageWithEmptyReply>):
(IPC::jsValueForDecodedMessageReply<MessageName::TestWithSwift_TestMessageWithEmptyReply>):
(IPC::jsValueForDecodedMessage<MessageName::TestWithSwift_TestMessageWithEmptyReplyReply>):
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessages.h:
(Messages::TestWithSwift::TestMessageWithEmptyReply::name):
(Messages::TestWithSwift::TestMessageWithEmptyReply::asyncMessageReplyName):
(Messages::TestWithSwift::TestMessageWithEmptyReply::TestMessageWithEmptyReply):
(Messages::TestWithSwift::TestMessageWithEmptyReply::encode):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::name):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::TestMessageWithEmptyReplyReply):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::encode):
* Configurations/CommonBase.xcconfig:
* Source/WebKit/CMakeLists.txt:
* Source/WebKit/Shared/Concurrency+Extras.swift: Added.
(Task.immediateOnMainActor(_:)):
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/323222@main

36cbac4

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
❌ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win ⏳ 🛠 ios-apple
✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests ⏳ 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe ⏳ 🛠 vision-apple
✅ 🧪 webkitpy ✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-AS-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 🧪 merge ✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@adetaylor adetaylor self-assigned this Oct 8, 2026
@adetaylor adetaylor added the WebKit Process Model Bugs related to WebKit's multi-process architecture label Oct 8, 2026
@webkit-early-warning-system

webkit-early-warning-system commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

@adetaylor
adetaylor force-pushed the eng/reland-swift-ipc-async-handlers branch from a8b6d09 to 36cbac4 Compare October 8, 2026 13:05
@webkit-early-warning-system

webkit-early-warning-system commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

@adetaylor adetaylor added the merge-queue Applied to send a pull request to merge-queue label Oct 9, 2026
… [reland]

https://bugs.webkit.org/show_bug.cgi?id=324524

Reviewed by Richard Robinson

A handler for a message with a reply now returns it, and any handler may be async. For instance,
IPCTesterReceiver's deferred reply no longer deals with WTF::CompletionHandler and becomes
simply:

    func deferredReplyMessage(connection: IPC.Connection, arg1: UInt32) async -> UInt64 {
        deferredReplyArgument = arg1
        return await withCheckedContinuation { pendingDeferredReply = $0 }
    }

so the reply is whatever the handler returns, whenever it returns. It cannot forget to
reply or reply twice, and no C++ reply type appears in its signature.

Async or not, throwing or not, all four combinations go through one generated call, since a
single `await` and a single `try` each cover a whole expression:

    let reply = try await mayThrowInvalidMessage(target.foo(connection: connection, ...))

Nothing in .messages.in says whether a handler is async: handleMessageSynchronous moves the
reply encoder into the completion handler, so Synchronous only means the sender blocks, and
deferring is the receiver's choice. A handler without a reply may need to await too, so every
message is dispatched the same way.

Everything runs on the main thread. Handlers are @mainactor. @mainactor functions are not
exposed to C++, so the generated wrapper stays synchronous, enters the actor with
MainActor.assumeIsolated - which also asserts IPC dispatched us on the main thread - and
starts the handler with Task.immediate, which runs it inline until its first suspension and
inherits the actor, so the remainder stays on the main thread too. Running inline is what keeps
Swift-handled messages in order with C++-handled ones; Task.init would not. The body lives in a
@mainactor static method because a typed `catch` inside a closure widens to `any Error`.
mayThrowInvalidMessage's async overload is nonisolated(nonsending), without which awaiting it
would send the reply out of the actor.

Adds -Werror RegionIsolation, without which none of this is checked: data-race diagnostics
are only warnings when a C++ type is involved, because C++ modules carry no concurrency
annotations. That is most of our Swift.

None of this crosses a real boundary - it is one thread and one stack frame - but the wrapper
must be nonisolated for C++ to call it, because swiftc silently omits @mainactor members from
the generated C++ header.

Everything under Scripts/webkit/tests/ is messages_unittest.py -r output, except
TestWithSwift.messages.in, which gains a message with an empty reply.

This is a reland; the previous version hit a Swift compiler bug rdar://189495426 on Windows
alone. This is worked around in this version by packing the function arguments into a struct.

* Source/WebKit/Platform/IPC/MessageCheck.swift:
(mayThrowInvalidMessage(_:)):
(Task.immediateOnMainActor(_:)):
(InvalidMessage.markMessageInvalid(_:on:)): Deleted.
* Source/WebKit/Scripts/webkit/messages.py:
(swift_reply_completion_argument):
(generate_swift_message_handler):
(swift_dispatch_target_and_function):
* Source/WebKit/Scripts/webkit/tests/TestWithStreamSwiftEnabledByMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamSwiftMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftConditionallyAndEnabledByMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftConditionallyMessageReceiver.swift:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessageReceiver.swift:
* Source/WebKit/Shared/IPCTesterReceiver.swift:
(IPCTesterReceiver.asyncMessage):
(IPCTesterReceiver.deferredReplyMessage):
(IPCTesterReceiver.completeDeferredReply(connection:arg1:)):
(IPCTesterReceiver.pendingDeferredReply):
(IPCTesterReceiver.deferredReply): Deleted.
(IPCTesterReceiver.completeDeferredReply(_:arg1:)):
* Source/WebKit/UIProcess/WebBackForwardList.swift:
(WebBackForwardList.backForwardAddItem):
(WebBackForwardList.backForwardSetChildItem):
(WebBackForwardList.backForwardClearChildren):
(WebBackForwardList.backForwardUpdateItem):
(WebBackForwardList.backForwardGoToItem):
(WebBackForwardList.backForwardListContainsItem):
(WebBackForwardList.backForwardAllItems):
(WebBackForwardList.backForwardItemAtIndexForWebContent):
(WebBackForwardList.backForwardListCounts):
* Source/cmake/WebKitSwiftFlags.cmake:
* Source/WTF/wtf/PlatformHave.h:
* Source/WebKit/Configurations/AllowedSPI.toml:
* Source/WebKit/Platform/spi/Cocoa/_Concurrency_SPI.swiftinterface: Added.
* Source/WebKit/Scripts/webkit/tests/MessageArgumentDescriptions.cpp:
(IPC::jsValueForArguments):
(IPC::jsValueForReplyArguments):
(IPC::messageArgumentDescriptions):
(IPC::messageReplyArgumentDescriptions):
* Source/WebKit/Scripts/webkit/tests/MessageNames.cpp:
* Source/WebKit/Scripts/webkit/tests/MessageNames.h:
* Source/WebKit/Scripts/webkit/tests/TestWithSwift.messages.in:
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessageReceiver.cpp:
(WebKit::TestWithSwiftMessageForwarder::didReceiveMessage):
(CompletionHandlers::TestWithSwift::completeWithDefaultReply):
(IPC::jsValueForDecodedMessage<MessageName::TestWithSwift_TestMessageWithEmptyReply>):
(IPC::jsValueForDecodedMessageReply<MessageName::TestWithSwift_TestMessageWithEmptyReply>):
(IPC::jsValueForDecodedMessage<MessageName::TestWithSwift_TestMessageWithEmptyReplyReply>):
* Source/WebKit/Scripts/webkit/tests/TestWithSwiftMessages.h:
(Messages::TestWithSwift::TestMessageWithEmptyReply::name):
(Messages::TestWithSwift::TestMessageWithEmptyReply::asyncMessageReplyName):
(Messages::TestWithSwift::TestMessageWithEmptyReply::TestMessageWithEmptyReply):
(Messages::TestWithSwift::TestMessageWithEmptyReply::encode):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::name):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::TestMessageWithEmptyReplyReply):
(Messages::TestWithSwift::TestMessageWithEmptyReplyReply::encode):
* Configurations/CommonBase.xcconfig:
* Source/WebKit/CMakeLists.txt:
* Source/WebKit/Shared/Concurrency+Extras.swift: Added.
(Task.immediateOnMainActor(_:)):
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/323222@main
@webkit-commit-queue
webkit-commit-queue force-pushed the eng/reland-swift-ipc-async-handlers branch from 36cbac4 to 9fdd260 Compare October 9, 2026 12:15
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 323222@main (9fdd260): https://commits.webkit.org/323222@main

Reviewed commits have been landed. Closing PR #76310 and removing active labels.

@webkit-commit-queue
webkit-commit-queue merged commit 9fdd260 into WebKit:main Oct 9, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WebKit Process Model Bugs related to WebKit's multi-process architecture

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants