Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/angular/build/src/tools/sass/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ export default async function renderSassStylesheet(
containingUrl: containingUrl ? fileURLToPath(containingUrl) : null,
},
});
// Wait for the main thread to set the signal to 1 and notify, which tells
// us that a message can be received on the port.
// If the main thread is fast, the signal will already be set to 1, and no
// sleep/notify is necessary.
// However, there can be a race condition here:
// - the main thread sets the signal to 1, but does not get to the notify instruction yet
// - the worker does not pause because the signal is set to 1
// - the worker very soon enters this method again
// - this method sets the signal to 0 and sends the message
// - the signal is 0 and so the `Atomics.wait` call blocks
// - only now the main thread runs the `notify` from the first invocation, so the
// worker continues.
// - but there is no message yet in the port, because the thread should not have been
// waken up yet.
// To combat this, wait for a non-0 value _twice_.
// Almost every time, this immediately continues with "not-equal", because
// the signal is still set to 1, except during the race condition, when the second
// wait will wait for the correct notify.
Atomics.wait(importerChannel.signal, 0, 0);
Atomics.wait(importerChannel.signal, 0, 0);

const result = receiveMessageOnPort(importerChannel.port)?.message as string | null;
Expand Down