fix: prevent WebSocket errors in Chrome during upload measurement#21
fix: prevent WebSocket errors in Chrome during upload measurement#21ArtemZhyto wants to merge 1 commit intom-lab:mainfrom
Conversation
|
Thank you @ArtemZhyto for contributing! This code is directly on the hot path - have you considered the performance implications? If the websocket error on Chromium is essentially benign, it may not be worth adding a conditional there. What do you think? |
bassosimone
left a comment
There was a problem hiding this comment.
@ArtemZhyto Hi and thank you for contributing. Questions: is the WebSocket transition of state synchronous with respect to the state of the JavaScript engine? That is, what provides us with the guarantee that after checking for the WebSocket state, the .send is not going to throw because the WebSocket was nonetheless closed? Can you elaborate on these points please? 🙏
|
Hi @robertodauria! Thanks for the catch. You're absolutely right that this is the hot path. My reasoning for the While the error is benign, preventing it makes the library's output much cleaner for developers and telemetry. However, if we want to prioritize maximum throughput, we could keep the check only for the |
|
Hi @bassosimone! Great question. In the JavaScript environment, execution is governed by a single-threaded Event Loop. Since there is no thread preemption, the WebSocket state cannot transition during the execution of a synchronous block of code. Once the |
Description
This PR addresses the "WebSocket is already in CLOSING or CLOSED state" error that frequently occurs in Chromium-based browsers at the end of an upload test.
Changes
safeSendhelper function to verifysock.readyState === WebSocket.OPENbefore any data transmission.readyStatecheck before callingsock.close()to prevent errors when the socket is already being closed by the server or a timeout.safeSendguard.Motivation
The
uploaderloop is scheduled viasetTimeout(..., 0). In some cases, the WebSocket transitions to aCLOSINGorCLOSEDstate between the time the timeout is scheduled and when the function actually executes. This PR ensures the client handles these transitions gracefully without throwing console errors.Fixes #10
This change is