Skip to content

Commit

Permalink
UserID: continue the auction if userId init fails (prebid#8788)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored and JacobKlein26 committed Feb 8, 2023
1 parent 6ff8cb8 commit eaee420
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 10 additions & 6 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,9 @@ function getPPID() {
* @param {Object} reqBidsConfigObj required; This is the same param that's used in pbjs.requestBids.
* @param {function} fn required; The next function in the chain, used by hook.js
*/
export function requestBidsHook(fn, reqBidsConfigObj, {delay = GreedyPromise.timeout} = {}) {
export function requestBidsHook(fn, reqBidsConfigObj, {delay = GreedyPromise.timeout, getIds = getUserIdsAsync} = {}) {
GreedyPromise.race([
getUserIdsAsync(),
getIds().catch(() => null),
delay(auctionDelay)
]).then(() => {
// pass available user id data to bid adapters
Expand Down Expand Up @@ -762,13 +762,17 @@ function refreshUserIds({submoduleNames} = {}, callback) {
function getUserIdsAsync() {
return initIdSystem().then(
() => getUserIds(),
(e) =>
e === INIT_CANCELED
(e) => {
if (e === INIT_CANCELED) {
// there's a pending refresh - because GreedyPromise runs this synchronously, we are now in the middle
// of canceling the previous init, before the refresh logic has had a chance to run.
// Use a "normal" Promise to clear the stack and let it complete (or this will just recurse infinitely)
? Promise.resolve().then(getUserIdsAsync)
: GreedyPromise.reject(e)
return Promise.resolve().then(getUserIdsAsync)
} else {
logError('Error initializing userId', e)
return GreedyPromise.reject(e)
}
}
);
}

Expand Down
15 changes: 14 additions & 1 deletion test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,22 @@ describe('User ID', function () {
clearStack().then(() => {
// simulate init complete
mockIdCallback.callArg(0, {id: {MOCKID: '1111'}});
})
});
});

it('should continue the auction when init fails', (done) => {
startInit();
requestBidsHook(() => {
done();
},
{adUnits: [getAdUnitMock()]},
{
delay: delay(),
getIds: () => Promise.reject(new Error())
}
);
})

it('should not get stuck when init fails', () => {
const err = new Error();
mockIdCallback.callsFake(() => { throw err; });
Expand Down

0 comments on commit eaee420

Please sign in to comment.