Skip to content

Commit

Permalink
UserID module: fix bug with userID init sometimes getting stuck in an…
Browse files Browse the repository at this point in the history
… infinite loop (prebid#8362)

In some situations userID submodules can throw exceptions (prebid#8360) which then, after prebid#8201, causes the ID system to get stuck in an infinite loop.
  • Loading branch information
dgirardi authored and changjun committed May 9, 2022
1 parent 5541bda commit b11c1e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ function delayFor(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const INIT_CANCELED = {};

function idSystemInitializer({delay = delayFor} = {}) {
/**
* @returns a {promise, resolve, reject} trio where `promise` is resolved by calling `resolve` or `reject`.
Expand Down Expand Up @@ -562,7 +564,7 @@ function idSystemInitializer({delay = delayFor} = {}) {

function cancelAndTry(promise) {
if (cancel != null) {
cancel.reject();
cancel.reject(INIT_CANCELED);
}
cancel = breakpoint();
return Promise.race([promise, cancel.promise]);
Expand Down Expand Up @@ -803,8 +805,9 @@ function refreshUserIds({submoduleNames} = {}, callback) {
* });
* ```
*/

function getUserIdsAsync() {
return initIdSystem().then(() => getUserIds(), () => getUserIdsAsync());
return initIdSystem().then(() => getUserIds(), (e) => e === INIT_CANCELED ? getUserIdsAsync() : Promise.reject(e));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ describe('User ID', function () {
mockIdCallback.callArg(0, {id: {MOCKID: '1111'}});
})
});

it('should not get stuck when init fails', () => {
const err = new Error();
mockIdCallback.callsFake(() => { throw err; });
return getGlobal().getUserIdsAsync().catch((e) =>
expect(e).to.equal(err)
);
});
});

it('pbjs.refreshUserIds updates submodules', function(done) {
Expand Down

0 comments on commit b11c1e6

Please sign in to comment.