-
Notifications
You must be signed in to change notification settings - Fork 13
Support cancellation of tasks with FetchHTTPClient #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xbhatnag
wants to merge
1
commit into
main
Choose a base branch
from
fetch-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+53
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,13 @@ import JavaScriptKit | |
| /// # Javascript Imports | ||
| /// This file defines the Javascript classes and functions imported into Swift. | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/AbortController | ||
| @JSClass(from: .global) struct AbortController: @unchecked Sendable { | ||
| @JSGetter var signal: JSObject? | ||
| @JSFunction init() throws(JSException) | ||
| @JSFunction func abort() throws(JSException) | ||
| } | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/Headers | ||
| @JSClass(from: .global) struct Headers { | ||
| @JSFunction init() throws(JSException) | ||
|
|
@@ -58,11 +65,13 @@ import JavaScriptKit | |
| let body: JSObject? | ||
| let method: String? | ||
| let headers: Headers? | ||
| let signal: JSObject? | ||
|
|
||
| init(body: JSObject?, method: String?, headers: Headers?) { | ||
| init(body: JSObject?, method: String?, headers: Headers?, signal: JSObject?) { | ||
| self.body = body | ||
| self.method = method | ||
| self.headers = headers | ||
| self.signal = signal | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -76,6 +85,7 @@ import JavaScriptKit | |
| // TODO: Find a way to remove the @unchecked. This object has to be moved through the different Swift reader types. | ||
| @JSClass(from: .global) struct ReadableStreamDefaultReader: @unchecked Sendable { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, |
||
| @JSFunction func read() async throws(JSException) -> Chunk | ||
| @JSFunction func releaseLock() throws(JSException) | ||
| } | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these functions safe to call from any threads?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm no expert on how this translates to JS, but isn't JS single-threaded, making WASM single-threaded, making all the asynchronous contexts in Swift also single-threaded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that multithreading is supported in WASM: https://webassembly.org/features/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Today I learned :) @MaxDesiatov might know more about this specific question...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our Swift SDK for Wasm is single-threaded by default. Multi-threading is available as an option, so I don't think
@unchecked Sendableis justifiable here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could work as a temporary measure. I would like @kateinoigakukun to chime in, as he's been using the multi-threaded Swift SDK for Wasm extensively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any JS methods against a JSObject should be called on the thread on which the JSObject is created. So as long as those methods are called on the thread the
AbortControllerinstance is created on, it's safe. However if we cannot guarantee that, we need to useJSRemote<T>to call them safely: swiftwasm/JavaScriptKit#711Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does JS thread map to Swift concurrency? There is no guarantee of the thread that an async method runs on since threads can switch arbitrarily after an
awaitright? Should we wrap everything in JSRemote or just use@MainActor? (I assume that MainActor maps to the main thread in JS as well)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MainActor-isolated code is consistently mapped to the browser main thread in any case, and there is no guarantee that any other async code runs on. However, practically speaking, JavaScriptKit does not provide a global concurrent executor even under a multithreading environment, and it just provides
TaskExecutorimplementations, and those implementations guarantee no thread hop in a structured async code so that client code can control which thread a specific code can run on in an opt-in way.I took a closer look on the code, and I can say
FetchHTTPClient.performitself is safe because all JS object access happens within a structured code, butResponseBodyReader.readhas to useJSRemotebecause the caller might be running on a different thread than the threadFetchHTTPClient.performwas called.After swiftwasm/JavaScriptKit#747 lands, we can fix the potential thread issue by the following patch.
Patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope to understand a bit more how this works. Is
fetchallowed to be called from any thread? DoesJSRemoteinternally switch back to the creating thread to call methods?Wondering if there is an efficiency difference between wrapping the reader in JSRemote or just slapping MainActor on everything to let Swift handle thread hopping.