Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: processQueue never recovers from exceptions #34221

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
44 changes: 35 additions & 9 deletions packages/apps-engine/deno-runtime/lib/messenger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeAll } from "https://deno.land/[email protected]/io/write_all.ts";
import { writeAll } from 'https://deno.land/[email protected]/io/write_all.ts';

import * as jsonrpc from 'jsonrpc-lite';

Expand Down Expand Up @@ -33,26 +33,52 @@ const COMMAND_PONG = '_zPONG';

export const RPCResponseObserver = new EventTarget();

class ProcessingLock {
private isProcessing = false;

lock() {
const granted = this.isProcessing === false;

if (!granted) {
return {
granted,
[Symbol.dispose]: () => {},
};
}

this.isProcessing = true;

return {
granted,
[Symbol.dispose]: () => {
this.isProcessing = false;
},
};
}
[Symbol.dispose]() {
this.isProcessing = false;
}
}

export const Queue = new (class Queue {
private queue: Uint8Array[] = [];
private isProcessing = false;
private isProcessingLock = new ProcessingLock();

private async processQueue() {
if (this.isProcessing) {
using processing = this.isProcessingLock.lock();

if (!processing.granted) {
return;
}

this.isProcessing = true;

while (this.queue.length) {
const message = this.queue.shift();
const [message] = this.queue;

if (message) {
await Transport.send(message);
}
this.queue.shift();
}

this.isProcessing = false;
}

public enqueue(message: jsonrpc.JsonRpc | typeof COMMAND_PONG) {
Expand All @@ -63,7 +89,7 @@ export const Queue = new (class Queue {
public getCurrentSize() {
return this.queue.length;
}
});
})();

export const Transport = new (class Transporter {
private selectedTransport: Transporter['stdoutTransport'] | Transporter['noopTransport'];
Expand Down
Loading