websocket abruptly closes

I'm using MS cognitive services with CloudFlare Pages and the websocket connection closes before covnerting text to audio and generting the file:
console.log(sdk.Diagnostics.SetLoggingLevel(sdk.LogLevel.Debug));

const prom = new Promise((resolve, reject) => {
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, null);

synthesizer.speakSsmlAsync(
ssml,
async (result) => {
console.log('RESULT', result);
if (result.errorDetails) {
console.log('1 if ');
console.log('ERROR', result.errorDetails);
synthesizer.close();
} else {
console.log('2 if');
const file = new File([result.audioData], fileName, {
type: 'audio/mp3',
lastModified: Date.now()
});
const res = await context.env.R2_IGLSBKT.put(fileName, file);
console.log('res R2', res);
synthesizer.close();
resolve(fileName);
}
},
(error) => {
console.log('err', error);
synthesizer.close();
}
);
});

await context.waitUntil(prom);
console.log(sdk.Diagnostics.SetLoggingLevel(sdk.LogLevel.Debug));

const prom = new Promise((resolve, reject) => {
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, null);

synthesizer.speakSsmlAsync(
ssml,
async (result) => {
console.log('RESULT', result);
if (result.errorDetails) {
console.log('1 if ');
console.log('ERROR', result.errorDetails);
synthesizer.close();
} else {
console.log('2 if');
const file = new File([result.audioData], fileName, {
type: 'audio/mp3',
lastModified: Date.now()
});
const res = await context.env.R2_IGLSBKT.put(fileName, file);
console.log('res R2', res);
synthesizer.close();
resolve(fileName);
}
},
(error) => {
console.log('err', error);
synthesizer.close();
}
);
});

await context.waitUntil(prom);
I got this error:
(log) ERROR websocket send error: Websocket not ready 7E9767F5499D4D44AB1AEADF2328152C DA30C36F329148D58231F50472556959 Error
at $e.sendRawMessage (functionsWorker-0.04974505925468953.js:33452:129)
at $e.<anonymous> (functionsWorker-0.04974505925468953.js:33474:22)
at Generator.next (<anonymous>)
at c (functionsWorker-0.04974505925468953.js:33336:13)
(log) ERROR websocket send error: Websocket not ready 7E9767F5499D4D44AB1AEADF2328152C DA30C36F329148D58231F50472556959 Error
at $e.sendRawMessage (functionsWorker-0.04974505925468953.js:33452:129)
at $e.<anonymous> (functionsWorker-0.04974505925468953.js:33474:22)
at Generator.next (<anonymous>)
at c (functionsWorker-0.04974505925468953.js:33336:13)
My understanding is that WaitUntil kept the task running in the background until completion. Why then does the ws end before completion? How can I keep running in the background? (I'm in Unbound mode)
8 Replies
Vero
Vero2y ago
Converting text to audio sounds like an arduous task. You might be hitting other limits (e.g. the 128 MB memory limit). I'd advise that you try this first outside of a waitUntil, just to see if it's possible, and only then try actually putting it into a waitUntil. waitUntil itself is a best-effort mechanism. There are no guarantees that the work will definitely be completed
rodrigo
rodrigoOP2y ago
@veerooo What do you mean by 'arduous task'? this is done at MS servers. The point is that it takes a while to get the response (from my experience doing the convertion using Remix with Node.js). I mean, it worked for me using node.js ... so the point is I don't know how to make it work with Workers because the connections is closed. I tried without waitUntil and it didn't work . But what do you mean? drop waitUntil and use and standard promise await?
Vero
Vero2y ago
Yeah, drop the waitUntil and just await it before returning any response, just to see if it's even possible in Workers. We don't have perfect compatibility with Node.js (we run in a v8 environment), so it's possible that this requires a bit of tweaking (or it may be downright impossible). The team is suggesting to start with the simplest possible version of the code to see how that works, and then optimize by pulling out into waitUntil etc.
rodrigo
rodrigoOP2y ago
@Vero With a promise the result it's even worse:"
(log) RESULT [object Object]
(log) 1 if
(log) ERROR websocket send error: Websocket not ready B084381288B84CFD9A409F995DD2944F A9B5DB6012074A028CCE40ABBB330607 Error
at $e.sendRawMessage (functionsWorker-0.5286015062338394.js:33452:129)
at $e.<anonymous> (functionsWorker-0.5286015062338394.js:33474:22)
at Generator.next (<anonymous>)
at c (functionsWorker-0.5286015062338394.js:33336:13)
(debug) 2023-04-27T15:17:42.006Z | ConnectionClosedEvent | privName: ConnectionClosedEvent | privEventId: F444FDBB64F54926BB1A96D3FD5D2B89 | privEventTime: 2023-04-27T15:17:42.006Z |
privEventType: 0 | privMetadata: {} | privConnectionId: B084381288B84CFD9A409F995DD2944F | privReason: WebSocket disconnected without sending Close frame. | privStatusCode: 1006
X [ERROR] Error: Promise will never complete.
(log) RESULT [object Object]
(log) 1 if
(log) ERROR websocket send error: Websocket not ready B084381288B84CFD9A409F995DD2944F A9B5DB6012074A028CCE40ABBB330607 Error
at $e.sendRawMessage (functionsWorker-0.5286015062338394.js:33452:129)
at $e.<anonymous> (functionsWorker-0.5286015062338394.js:33474:22)
at Generator.next (<anonymous>)
at c (functionsWorker-0.5286015062338394.js:33336:13)
(debug) 2023-04-27T15:17:42.006Z | ConnectionClosedEvent | privName: ConnectionClosedEvent | privEventId: F444FDBB64F54926BB1A96D3FD5D2B89 | privEventTime: 2023-04-27T15:17:42.006Z |
privEventType: 0 | privMetadata: {} | privConnectionId: B084381288B84CFD9A409F995DD2944F | privReason: WebSocket disconnected without sending Close frame. | privStatusCode: 1006
X [ERROR] Error: Promise will never complete.
rodrigo
rodrigoOP2y ago
I opend this issue with MS , and they fixed the code to allow user Workers (V8) https://github.com/microsoft/cognitive-services-speech-sdk-js/issues/645
GitHub
SDK with Cloudflare workers and Fetch API · Issue #645 · microsoft/...
When using this sdk with cloudflare workers I get this error: XMLHttpRequest is not defined I understand it's due to t...
rodrigo
rodrigoOP2y ago
What I don't understand it's why the connection is dropped when I'm in Unbound mode and supposedly there's 30 sec to execute, right? According to MS, on their side it's ok (see the last comment) And there's another issue: apparently I can only test it in prod because MS's sdk only works with wss and miniflare apparently only support ws ( @veerooo any suggestion on what to do? Using a promise apparently it doesn't await to the response ...
hydroxide
hydroxide12mo ago
I know this is 6+ months old, but if anyone else is coming up against this, it seems to be an issue with Cloudflare workerd. https://github.com/cloudflare/workerd/issues/1370#issuecomment-1859447303
GitHub
🐛 Bug Report — Runtime APIs: names of WebSocket ready state consta...
Names of ready states constants on the WebSocket class, defined in here differ from that of the Web IDL definition in the WebSocket Living Standard. In workerd, every name have extra prefix: READY_...
hydroxide
hydroxide12mo ago
There's an open PR so hopefully should be fixed soon but I've also mentioned a temporary workaround in case you need it beforehand
Want results from more Discord servers?
Add your server