Skip to content

Commit

Permalink
Fixing race condition in block fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinius committed Apr 2, 2021
1 parent 5e8e7b4 commit 6b26837
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/source/blockedsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class BlockedSource extends BaseSource {
cachedBlocks.set(blockId, this.blockCache.get(blockId));
} else if (this.blockRequests.has(blockId)) {
blockRequests.set(blockId, this.blockRequests.get(blockId));
} else if (this.blockIdsToFetch.has(blockId)) {
missingBlockIds.add(blockId);
} else {
this.blockIdsToFetch.add(blockId);
missingBlockIds.add(blockId);
Expand All @@ -104,10 +106,15 @@ export class BlockedSource extends BaseSource {

for (const blockId of missingBlockIds) {
const block = this.blockRequests.get(blockId);
if (!block) {
const cachedBlock = this.blockCache.get(blockId);

if (block) {
blockRequests.set(blockId, block);
} else if (cachedBlock) {
cachedBlocks.set(blockId, cachedBlock);
} else {
throw new Error(`Block ${blockId} is not in the block requests`);
}
blockRequests.set(blockId, block);
}

// actually await all pending requests
Expand Down

0 comments on commit 6b26837

Please sign in to comment.