jextract/jni: Propagate CompletableFuture cancellation to Swift tasks

Open
#947 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
java, swift
Domain
mobile-dev

Research direction

Start by tracing the JNI async call and generated thenApply result-conversion stage described in the issue, then review the reference workaround in the linked pull request. Check how cancellation reaches the source future and the Swift task; done means cancelling the returned Java future forwards cancellation to the Swift task so cooperative operations such as Task.sleep can observe it.

Written by the indexing model from the issue text.

Description

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: https://github.com/qoncept/swift-java/pull/10

Dominant language
Swift
Stars
1.3k
Forks
127
Avg merge
1h 6m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from swiftlang/swift-java

All issues in swiftlang/swift-java

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.