najamelan / ws_stream_wasm

Wasm convenience API for WebSockets
The Unlicense
94 stars 13 forks source link

Issue with nightly rust and specific rust flags #12

Open mhchia opened 1 year ago

mhchia commented 1 year ago

Description

I've encountered an issue with the library when using the nightly Rust version in conjunction with the rustflags +atomics,+bulk-memory,+mutable-globals.

Here's the relevant code snippet:

    let (mut ws_meta, mut ws_stream) = WsMeta::connect(ws_server_url, None).await
        .expect_throw("Assumed the client ws connection would succeed");
    let mut io_stream = ws_stream.into_io();
    let message = b"Hello from browser".to_vec();
    io_stream
        .write(&message).await
        .expect_throw("Failed to write to websocket");

While attempting to write to a websocket (io_stream), the operation fails.

Environment

OS: macOS 12.6 cargo: 1.68.0-nightly (70898e522 2022-12-05) rustc: 1.68.0-nightly (bdb07a8ec 2022-12-11) Browser: Chrome Version 116.0.5845.140 (Official Build) (x86_64)

Reproduction Steps

For a detailed step-by-step reproduction guide, please refer to this repository.

I'd greatly appreciate any feedback, solutions, or clarifications regarding this issue.

najamelan commented 1 year ago

Thanks for reporting. I'll try to address this this weekend.

mhchia commented 1 year ago

Just an update on our end: my colleague found this line failed

https://github.com/najamelan/ws_stream_wasm/blob/4035d4657388040938f22829fef2628c1243bbfa/src/ws_stream.rs#L314

If the line is replaced by

WsMessage::Binary(d) => {
    let buffer = ArrayBuffer::new(
        d.len()
        .try_into()
        .expect("Message was too large to be sent."),
    );
    Uint8Array::new(&buffer).copy_from(d.as_slice());
    self.ws
        .send_with_array_buffer(&buffer)
        .map_err(|_| WsErr::ConnectionNotOpen)?
}

, it works. However, we then encountered a oom issue with our application

image

I was guessing it is related to memory leaking, for example, buffer is not gc'ed and too many writes may end up using up the memory. But it's just my suspicion and I haven't verified it and may be wrong.

Hope these information will be helpful 🙏