Skip to content

jextract/JNI: Propagate CompletableFuture cancellation to Swift tasks - #974

Open
sidepelican wants to merge 10 commits into
swiftlang:mainfrom
sidepelican:future_cancel_propagate
Open

sidepelican wants to merge 10 commits into
swiftlang:mainfrom
sidepelican:future_cancel_propagate

Conversation

@sidepelican

Copy link
Copy Markdown
Contributor

Problem

JNI bindings expose Swift async functions to Java as methods returning a CompletableFuture.
Cancelling the returned future currently leaves the underlying Swift task running.

Solution

The generated Java binding now passes a SwiftAsyncResultSink to Swift in place of the CompletableFuture. The sink holds the future returned to the Java caller and uses whenComplete to forward cancellation to the Swift task.

The tricky case is when cancellation occurs after Swift has produced a result but before Java has taken ownership of it.
For results, Swift allocates storage with UnsafeMutablePointer.allocate and passes the pointer to Java. Java then becomes responsible for releasing that storage.
If cancellation races with this handoff, the pointer can be left without an owner.

For results that require an arena, SwiftAsyncResultSink uses a temporary SwiftArena backed by an ArrayDeque to collect wrappers during conversion. It converts any result delivered to the sink even if the future has already been cancelled. The sink then registers each wrapper with the caller's arena. Once registered, a wrapper is owned by that arena. If cancellation or an error interrupts the transfer, the sink releases the wrappers still in the deque.

To support the temporary arena, JNISwift2JavaGenerator.JavaTranslation can now select the arena variable used by the generated result conversion.

This PR adds cancellation forwarding only to JExtractAsyncFuncMode.completableFuture.

Fixes: #947

@sidepelican
sidepelican requested a review from ktoso as a code owner November 20, 2026 07:01

@Override
public void register(SwiftInstance instance) {
checkValid();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also fixes a race between async result registration and close().

@piercifani piercifani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. I read through it and couldn't find a leak or a double free, including when the cancel arrives just as Swift finishes.

I also checked it from Kotlin: cancelling a coroutine that's waiting on future.await() now stops the Swift task too.

One suggestion: the Swift task only stops when the future is cancelled. If the future finishes some other way first, for example through orTimeout(), complete() or completeExceptionally(), the Swift task keeps running. It would be good to stop it in those cases too.

@sidepelican

Copy link
Copy Markdown
Contributor Author

@piercifani Thanks for the review and for testing this from Kotlin! Good catch on orTimeout().

I updated the bridge to request cancellation of the Swift task whenever the returned future completes.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

jextract/jni: Propagate CompletableFuture cancellation to Swift tasks

2 participants