Skip to content

fix(bindgen): return JSPI host imports synchronously when the context cannot wait - #2324

Open
chenyan2002 wants to merge 2 commits into
bytecodealliance:mainfrom
chenyan2002:async-dtor
Open

chenyan2002 wants to merge 2 commits into
bytecodealliance:mainfrom
chenyan2002:async-dtor

Conversation

@chenyan2002

Copy link
Copy Markdown
Contributor

With JSPI, an import marked async (via asyncImports) always returned a promise to the guest, even when the host answered synchronously. So calling from a context that can't wait trapped with "cannot block a synchronous task before returning". This includes two contexts:

  • Resource destructors: a destructor runs synchronously, either in its own sync task or with no task at all.
  • Instantiation: _initialize or a start function runs with no current task.

Changes

  • Generator trampolines. Imports marked async whose function type and lowering are both sync are now emitted as generator functions that yield. A new _maybeSyncGenerator runs them synchronously and only switches to a promise when a yielded value is a promise.
  • task.enterHost(). It's a new sync method holding the host-entry logic from enter({ isHost: true })
  • _withGlobalCurrentTaskMetaAsync is now only async when its closure returns a promise.
  • _lowerImportBackwardsCompat calls the host right away for manually-async imports, and returns the result synchronously when it isn't a promise.
  • _suspendingImport takes a new callHost flag, set only for these lowered host imports. The call traps only if the host actually returns a promise.

This PR doesn't fix the P3 async imports, which may need its own issue: P3 async imports go through _lowerImport, which always returns a promise, so an async-lowered call from a task that can't block still traps, even though the calls never need to block.

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.

1 participant