Skip to content

jextract/jni: Propagate CompletableFuture cancellation to Swift tasks #947

Open

Description

@teamhimeh

Problem

Cancelling a future returned by a JNI async call currently changes only the Java future's state. The underlying Swift task keeps running. The generated thenApply result-conversion stage also prevents cancellation of the returned future from reaching the source future.

Example

public func waitForWork() async throws -> Int64 {
  try await Task.sleep(for: .seconds(60))
  return 1
}

A Java caller can cancel the returned future:

var future = MySwiftLibrary.waitForWork();
future.cancel(false);

The Java future is cancelled, but the Swift task continues running. Cancellation of the returned future should also be forwarded to the Swift task, so cancellation-aware operations such as Task.sleep can observe it.

Swift cancellation is cooperative: forwarding cancellation does not interrupt an operating-system thread or wait for the task to finish.

Reference workaround implementation: qoncept#10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions