Skip to content
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

Error when running in Cloudflare Workers #1

Open
timothymiller opened this issue Feb 12, 2024 · 2 comments · May be fixed by #2
Open

Error when running in Cloudflare Workers #1

timothymiller opened this issue Feb 12, 2024 · 2 comments · May be fixed by #2

Comments

@timothymiller
Copy link

I appreciate your work on this project. Without it, it would take much longer to hash passwords with plain javascript.

Unfortunately, I receive this error when I attempt to hash a password:

A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.

I'm on the latest version of workerd and miniflare.

Please let me know if you need anything from me.

@timothymiller
Copy link
Author

The talented @robmarsher has a solution. The bug is in the invoke function:

export async function invoke(args: string[]) {
  const stdout = new TransformStream()
  const stderr = new TransformStream()
  const wasi = new WASI({
    args: ['argon2-wasi.wasm', ...args],
    stdout: stdout.writable,
    stderr: stderr.writable,
  })
  const instance = new WebAssembly.Instance(argon2, {
    wasi_snapshot_preview1: wasi.wasiImport,
  })
  const promise = wasi.start(instance)
  const errors = stderr.readable.getReader().read()
  const ret = stdout.readable.getReader().read()
  const [errorsStream, resultStream, _] = await Promise.all([errors, ret, promise])
  const errorsValue = new TextDecoder().decode(errorsStream.value)
  if (errorsValue) {
    throw new Error(errorsValue)
  }
  const retValue = new TextDecoder().decode(resultStream.value)
  return retValue.trim()
}

@rmarscher
Copy link

Yes, cloudflare's workerd seemed to suddenly not like the await wasi.start(instance) line. Locally with miniflare, I also saw an error - "The script will never generate a response". Setting up the stderr and stdout readable streams and then awaiting those together with the wasi.start got it working again.

I love how simple the code is for this project. Thank you so much for publishing it.

rmarscher added a commit to rmarscher/argon2-wasi that referenced this issue May 9, 2024
rmarscher added a commit to rmarscher/argon2-wasi that referenced this issue May 9, 2024
@rmarscher rmarscher linked a pull request May 9, 2024 that will close this issue
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 a pull request may close this issue.

2 participants