React: Bug: DevTools calls arbitrary generators which may be stateful

Created on 30 Aug 2020  ·  11Comments  ·  Source: facebook/react

function foo*() {
  yield 1;
  yield 2;
}

let gen = foo()

Currently if you put gen into state or props and then open this component in DevTools, it will consume that generator while trying to format it. So gen.next() will give you { done: true } next time you call it.

This happens here:

https://github.com/facebook/react/blob/60ba723bf78b9a28f60dce854e88e206fab52301/packages/react-devtools-shared/src/utils.js#L616-L623

I think that maybe we should treat iterables differently if they return themselves as an iterator. Since that means they're likely stateful and it's not ok to iterate over them.

We detect iterables here (DevTools terminology is wrong btw, it should be iterable rather than iterator):

https://github.com/facebook/react/blob/60ba723bf78b9a28f60dce854e88e206fab52301/packages/react-devtools-shared/src/utils.js#L438-L439

I think maybe we could split this into iterable and opaque_iterable, and make sure none of the codepaths attempt to traverse opaque_iterable or pass it to something that would consume it (e.g. Array.from).

We could detect it based on data[Symbol.iterator]() === data — that clearly signals the iterable is its own iterator (which is the case for generators), and therefore it's not OK for DevTools to consume it.

Maybe some other heuristic could work. But overall, the goal is that Map and friends is still being iterated over, but an arbitrary generator is not.

Developer Tools Bug good first issue (taken)

Most helpful comment

Just a note, I'm nearly done! Sorry for being slow it just was my first attempt!

All 11 comments

This is similar to https://github.com/facebook/react/issues/19660 where the same line of code causes devtools to crash with infinite generators.

I wasn't able to reproduce that devtools calling stateful generators was an issue though I definitely expected it to be a problem.

@gaearon I could give a shot on this!

@gaearon I would love to give this a go.

@gaearon : I would like to address this bug

@todortotev Go for it :+1: Please let us know if you no longer intend to work on it.

@todortotev Go for it 👍 Please let us know if you no longer intend to work on it.

On it!

just let us know branch you are putting your efforts, more guys would like to contribute, for it 👍
we should return opaque_iterable for data[Symbol.iterator]() === data

@eps1lon I agree with you both issues are related. if we choose to not extract generator. We’re not gonna have the issue anymore

Just a note, I'm nearly done! Sorry for being slow it just was my first attempt!

This should be resolved by #19831 once it lands.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UnbearableBear picture UnbearableBear  ·  3Comments

zpao picture zpao  ·  3Comments

jvorcak picture jvorcak  ·  3Comments

hnordt picture hnordt  ·  3Comments

trusktr picture trusktr  ·  3Comments