ink icon indicating copy to clipboard operation
ink copied to clipboard

Race condition in 5.1.0 when using `rerender` and `unmount`

Open mdonnalley opened this issue 10 months ago • 4 comments

We use the rerender method to tigger a new rendering of a component and then later call unmount when we're done with it. This has worked well for our use case but after upgrading to 5.1.0, we're seeing a race condition that's causing the component to render after the component has been unmounted.

I created a repo that replicates the issue

git clone https://github.com/mdonnalley/ink-bug.git
cd ink-bug
npm install
node --loader=import-jsx index.js

With 5.1.0

--- Time to render the component  ---
Hello Foo!
--- Done! ---
Hello Baz!

With 5.0.1

--- Time to render the component  ---
Hello Baz!
--- Done! ---

Best I can tell the regression is caused by this PR, which swapped lodash's throttle for es-toolkit's.

Our best workaround at the moment is to pin ink to 5.0.1 but it's not a great long term solution since we'd like to stay up to date with latest.

We also considered adding a 10ms sleep before unmounting the component. This seems to work but it would require some painful refactoring on our end to make the entire call stack async.

mdonnalley avatar Jan 15 '25 19:01 mdonnalley

// @wojtekmaj

sindresorhus avatar Apr 16 '25 19:04 sindresorhus

@raon0211 Would you be kind to have a look? Looks like es-toolkit/compat's throttle has some behavioral differences from lodash's throttle that we were not aware of.

wojtekmaj avatar Apr 16 '25 19:04 wojtekmaj

Hello, thanks for bringing this to our attention.

There was indeed a problem with the throttle function, which has been fixed in our latest version.

We'll make sure to add a test case for this scenario in the ink package to prevent similar issues in the future.

raon0211 avatar Apr 30 '25 12:04 raon0211

We fixed this issue in the latest version of es-toolkit. In the meantime, please let us some time to add new test cases to ink.

raon0211 avatar May 08 '25 11:05 raon0211