Skip to content

Improve handling of exceptions thrown during act callback#2433

Merged
robertknight merged 5 commits intomasterfrom
act-exception-handling
Mar 31, 2020
Merged

Improve handling of exceptions thrown during act callback#2433
robertknight merged 5 commits intomasterfrom
act-exception-handling

Conversation

@robertknight
Copy link
Copy Markdown
Member

This PR fixes #2432 by ensuring that effect and pending render queues are flushed if a callback passed to act throws an exception. Previously useEffect effects and state updates would stop working after a call to act aborted with an exception. The result was that a single failure in a test suite could trigger a cascade of failures in unrelated tests, which was pretty confusing.

In the process, I discovered an issue that if an effect threw an exception, the effect would remain in the component's effect queue and would end up running again the next time that component's effects were flushed.

If act's callback throws an exception, sync or async, make sure that any
pending flushes of the render or effect queues are executed. If this is
not done, Preact will be left in a state where it has a non-empty
update/effect queue and so future deferred updates/effects will not
trigger another flush since one is already pending. However the
already-pending update will never get flushed, since `act` had
overridden the default flushing mechanism. As a result, deferred updates and
effects scheduled later don't get run.

Fixes #2432
Make sure that future state updates and effects continue to run if an
exception occurs during a call to `act`.

This required resolving an issue with hooks that an exception inside
`useEffect` left the effect callback in the effect queue, so it would
run again the next time effects were flushed.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 31, 2020

Size Change: +140 B (0%)

Total Size: 38.6 kB

Filename Size Change
hooks/dist/hooks.module.js 1.08 kB +1 B
hooks/dist/hooks.umd.js 1.12 kB -1 B
test-utils/dist/testUtils.js 437 B +47 B (10%) ⚠️
test-utils/dist/testUtils.module.js 439 B +47 B (10%) ⚠️
test-utils/dist/testUtils.umd.js 515 B +46 B (8%) 🔍
ℹ️ View Unchanged
Filename Size Change
compat/dist/compat.js 3.12 kB 0 B
compat/dist/compat.module.js 3.14 kB 0 B
compat/dist/compat.umd.js 3.17 kB 0 B
debug/dist/debug.js 2.98 kB 0 B
debug/dist/debug.module.js 2.96 kB 0 B
debug/dist/debug.umd.js 3.04 kB 0 B
devtools/dist/devtools.js 175 B 0 B
devtools/dist/devtools.module.js 185 B 0 B
devtools/dist/devtools.umd.js 252 B 0 B
dist/preact.js 3.72 kB 0 B
dist/preact.min.js 3.72 kB 0 B
dist/preact.module.js 3.73 kB 0 B
dist/preact.umd.js 3.78 kB 0 B
hooks/dist/hooks.js 1.05 kB 0 B

compressed-size-action

Comment thread hooks/src/index.js Outdated
The Preact tests generally don't use `context` blocks and the ESLint rules
don't know to treat a `context` block like `describe` (see
jest-community/eslint-plugin-jest#83)
@github-actions
Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.004%) to 99.8% when pulling b042875 on act-exception-handling into ca42c53 on master.

Duplicate the logic to reset effects in the `catch` branch per
recommendation at #2433 (review)
to save a few bytes, rather than using a variable.
Copy link
Copy Markdown
Member

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, nice work!

@robertknight
Copy link
Copy Markdown
Member Author

Thanks for the quick review!

@robertknight robertknight merged commit f6577c4 into master Mar 31, 2020
@robertknight robertknight deleted the act-exception-handling branch March 31, 2020 17:22
@marvinhagemeister
Copy link
Copy Markdown
Member

Great work! That's a very nice improvement!!

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.

Exception thrown during act callback breaks future effects

3 participants