Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(virtual-background): exit insertable-streams loop when the writer closes

Jaya Allamsettya6b1bd93dc07
diff --git a/react/features/stream-effects/virtual-background/pipeline/InsertableStreamsPipeline.ts b/react/features/stream-effects/virtual-background/pipeline/InsertableStreamsPipeline.ts
index 7f0734441f..fd1d314b8d 100644
--- a/react/features/stream-effects/virtual-background/pipeline/InsertableStreamsPipeline.ts
+++ b/react/features/stream-effects/virtual-background/pipeline/InsertableStreamsPipeline.ts
@@ -139,6 +139,23 @@ export default class InsertableStreamsPipeline {
this._trackReader = reader;
const writer = generator.writable.getWriter();
+ // Returns false if the writer is closed; the caller must break the loop to
+ // avoid an infinite cycle of failed writes burning CPU on a dead generator.
+ const tryWrite = async (f: VideoFrame, label: string): Promise<boolean> => {
+ try {
+ await writer.write(f);
+
+ return true;
+ } catch (err) {
+ if (err instanceof DOMException && err.name === 'InvalidStateError') {
+ return false;
+ }
+ logger.error(label, err);
+
+ return true;
+ }
+ };
+
try {
while (this._isRunning) {
const { value: frame, done } = await reader.read();
@@ -148,12 +165,12 @@ export default class InsertableStreamsPipeline {
}
if (!this._processor) {
- try {
- await writer.write(frame);
- } catch (err) {
- logger.error('[InsertableStreamsPipeline] Write error', err);
- }
+ const alive = await tryWrite(frame, '[InsertableStreamsPipeline] Write error');
+
frame.close();
+ if (!alive) {
+ break;
+ }
continue;
}
@@ -165,6 +182,8 @@ export default class InsertableStreamsPipeline {
logger.error('[InsertableStreamsPipeline] Frame processing error', err);
}
+ let alive = true;
+
if (resultCanvas) {
// Construct the frame outside the try so the catch path doesn't have a
// partially-initialised reference, and close it in finally so the frame is
@@ -174,22 +193,19 @@ export default class InsertableStreamsPipeline {
);
try {
- await writer.write(outFrame);
- } catch (err) {
- logger.error('[InsertableStreamsPipeline] Write error', err);
+ alive = await tryWrite(outFrame, '[InsertableStreamsPipeline] Write error');
} finally {
outFrame.close();
}
} else {
// Passthrough — write the raw frame to the generator.
- try {
- await writer.write(frame);
- } catch (err) {
- logger.error('[InsertableStreamsPipeline] Passthrough write error', err);
- }
+ alive = await tryWrite(frame, '[InsertableStreamsPipeline] Passthrough write error');
}
frame.close();
+ if (!alive) {
+ break;
+ }
}
} catch (err) {
// When stop() cancels the reader, reader.read() rejects — that is expected.