-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix one-tick-race condition in asyncMap #11249
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
fixes a one-tick-race condition in `asyncMap` |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,20 +33,21 @@ export function asyncMap<V, R>( | |||||||||||||||||||||||||
.then(both, both) | ||||||||||||||||||||||||||
.then( | ||||||||||||||||||||||||||
(result) => { | ||||||||||||||||||||||||||
--activeCallbackCount; | ||||||||||||||||||||||||||
next && next.call(observer, result); | ||||||||||||||||||||||||||
if (completed) { | ||||||||||||||||||||||||||
handler.complete!(); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
(error) => { | ||||||||||||||||||||||||||
--activeCallbackCount; | ||||||||||||||||||||||||||
throw error; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
.catch((caught) => { | ||||||||||||||||||||||||||
error && error.call(observer, caught); | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The race condition essentially is the following:
|
||||||||||||||||||||||||||
promiseQueue.finally(() => { | ||||||||||||||||||||||||||
--activeCallbackCount; | ||||||||||||||||||||||||||
if (completed) { | ||||||||||||||||||||||||||
handler.complete!(); | ||||||||||||||||||||||||||
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This might be a helpful comment, given the racy nature of this code. |
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
Comment on lines
+45
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To fix this, I moved this out of the promise chain that is assigned to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this logic, but I'm sure our future selves would appreciate a comment about why the |
||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
return (arg) => delegate && delegate.call(observer, arg); | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were not previously calling
handler.complete()
in the error handler, but now thepromiseQueue.finally
callback is potentially callinghandler.complete()
whenever we decrementactiveCallbackCount
. Is that a problem? Specifically, is it possible we might callerror.call(observer, caught)
and then also callcomplete.call(observer)
?