-
Notifications
You must be signed in to change notification settings - Fork 132
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
Infinite loop after last unsubscribe in withFilter function. #81
Comments
Doing const getNextPromise = () => {
return asyncIterator
.next()
.then(payload => Promise.all([
payload,
Promise.resolve(filterFn(payload.value, args, context, info)).catch(() => false),
]))
.then(([payload, filterResult]) => {
if (filterResult === true || payload.done === true) {
return payload;
}
// Skip the current value and wait for the next one
return getNextPromise();
});
}; |
+1 Fall into this trap |
@maktouch Firstly if fixed it in the same way, but it has a side effect - module.exports.withFilter = (asyncIteratorFn, filterFn) => {
return (rootValue, args, context, info) => {
const asyncIterator = asyncIteratorFn();
let returned = false;
const getNextPromise = () => {
if (returned) {
return {done: true, value: undefined};
}
return asyncIterator
.next()
.then(payload => Promise.all([
payload,
returned ? false : Promise.resolve(filterFn(payload.value, args, context, info)).catch(() => false),
]))
.then(([payload, filterResult]) => {
if (filterResult === true) {
return payload;
}
// Skip the current value and wait for the next one
return getNextPromise();
});
};
return {
next() {
return getNextPromise();
},
return() {
returned = true;
return asyncIterator.return();
},
throw(error) {
return asyncIterator.throw(error);
},
[$$asyncIterator]() {
return this;
},
};
};
}; |
@maktouch What about only skip filterFn if value is undefined? Promise.resolve(!!payload.value && filterFn(payload.value, args, context, info)).catch(() => false) |
I don't know, actually. I'm not sure what's the intended behaviour of getNextPromise; I noticed that my subscribe functions gets called every time I subscribe to something, and it's passing undefined. It would be nice to have input from the maintainers for some more clarification. In the meantime, I'm checking for |
apollographql#81 Check for filterResult or if it `payload.done`. Has a side effect that it `undefined` is passed to filterFn. At least it doesn't infinite loop.
Fix Issue #81 Infinite loop after last unsubscribe in withFilter function
I use withFilter to validate payload and got an infinite loop when there are no subscribers left (payload is resolved immediately with
{value: undefined, done: true}
).This function recursively calls itself even if payload.done is true and it leads to infinite call. Adding check for payload.done before
return getNextPromise();
fixes this problem or am I missing something?The text was updated successfully, but these errors were encountered: