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

AsyncLocalStorage for iterators and generators #52317

Closed
himself65 opened this issue Apr 1, 2024 · 2 comments
Closed

AsyncLocalStorage for iterators and generators #52317

himself65 opened this issue Apr 1, 2024 · 2 comments
Labels
async_hooks Issues and PRs related to the async hooks subsystem.

Comments

@himself65
Copy link
Member

himself65 commented Apr 1, 2024

Version

18, 20, 22

Platform

macos

Subsystem

async_hooks

What steps will reproduce the bug?

import { AsyncLocalStorage } from 'node:async_hooks'

const als = new AsyncLocalStorage()

const iter = als.run(10, function * () {
  yield 1 + (als.getStore() ?? 0)
  yield 2 + (als.getStore() ?? 0)
})

for await (const value of iter) {
  console.log(value)
}

const changed = als.run(10, function () {
  const iter = AsyncLocalStorage.bind(function * () {
    yield 1 + (als.getStore() ?? 0)
    yield 2 + (als.getStore() ?? 0)
  })()
  iter[Symbol.iterator] = AsyncLocalStorage.bind(iter[Symbol.iterator])
  return iter;
})

for (const value of changed) {
  console.log(value)
}

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

11
12
11
12

What do you see instead?

1
2
1
2

Additional information

No response

@himself65
Copy link
Member Author

workaround:

const changed = als.run(10, function () {
  const iter = function * () {
    yield 1 + (als.getStore() ?? 0)
    yield 2 + (als.getStore() ?? 0)
  }
  const sp = AsyncLocalStorage.snapshot();
  return function * () {
    const f = iter()
    while (true) {
      const { value, done } = sp(() => f.next())
      if (done) {
        break
      }
      yield value
    }
  }()
})

@dead-claudia
Copy link

This feels like a dupe of #42237.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants