johanhelsing / matchbox

Painless peer-to-peer WebRTC networking for rust wasm (and native!)
Apache License 2.0
889 stars 73 forks source link

WASM Refreshing one peer causes instant panic in other. #287

Closed sarahkittyy closed 1 year ago

sarahkittyy commented 1 year ago

This is a different issue to #113.

bevy_matchbox = { git = "https://github.com/johanhelsing/matchbox", branch = "main", features = ["ggrs"] }
bevy = { version = "0.11", features = ["mp3", "wav"] }
bevy_ggrs = { git = "https://github.com/gschup/bevy_ggrs", branch = "main" }

To reproduce:

The culprit is this unwrap here: https://github.com/johanhelsing/matchbox/blob/0e8df99735c137699103f15a6f17f8b0fb4be233/matchbox_socket/src/webrtc_socket/mod.rs#L217

Relevant console error:

app.js:449 panicked at 'called `Result::unwrap()` on an `Err` value: MessagingError(JsError(JsValue(InvalidStateError: Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'
Error: Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'
    at http://192.168.0.149:9999/wasm/app.js:1294:25
    at handleError (http://192.168.0.149:9999/wasm/app.js:251:18)
    at imports.wbg.__wbg_send_3264748509a2dd7c (http://192.168.0.149:9999/wasm/app.js:1293:67)
    at web_sys::features::gen_RtcDataChannel::RtcDataChannel::send_with_u8_array::h77285afc2b4a3741 (wasm://wasm/06fb8afa:wasm-function[46246]:0xc350d0)
    at matchbox_socket::webrtc_socket::wasm::<impl matchbox_socket::webrtc_socket::PeerDataSender for web_sys::features::gen_RtcDataChannel::RtcDataChannel>::send::hc07993961dabd924 (wasm://wasm/06fb8afa:wasm-function[43207]:0xc020ba)
    at <futures_util::future::future::fuse::Fuse<Fut> as core::future::future::Future>::poll::h98ef1e254452f433 (wasm://wasm/06fb8afa:wasm-function[643]:0x19a53b)
    at futures_util::future::future::FutureExt::poll_unpin::h7309e8d41f30958f (wasm://wasm/06fb8afa:wasm-function[88301]:0xd9911c)
    at matchbox_socket::webrtc_socket::socket::run_socket::{{closure}}::{{closure}}::{{closure}}::h3f57e1c301bafcd9 (wasm://wasm/06fb8afa:wasm-function[39024]:0xbb1bd8)
    at <futures_util::future::poll_fn::PollFn<F> as core::future::future::Future>::poll::hc6036724fd86f41f (wasm://wasm/06fb8afa:wasm-function[14619]:0x888ddb)
    at matchbox_socket::webrtc_socket::socket::run_socket::{{closure}}::hebb7623b38029b8d (wasm://wasm/06fb8afa:wasm-function[1293]:0x2f159a))))', C:\Users\sarah\.cargo\git\checkouts\matchbox-9d3f96917a0a12d8\9ad07f5\matchbox_socket\src\webrtc_socket\mod.rs:217:51

Stack:

Error
    at imports.wbg.__wbg_new_abda76e883ba8a5f (http://192.168.0.149:9999/wasm/app.js:433:21)
    at console_error_panic_hook::hook::h3365ffba7e1ce71e (wasm://wasm/06fb8afa:wasm-function[8249]:0x6e6c90)
    at core::ops::function::Fn::call::hce63c938a140a485 (wasm://wasm/06fb8afa:wasm-function[97660]:0xdb5ba7)
    at std::panicking::rust_panic_with_hook::h5cee2a9564faeb6d (wasm://wasm/06fb8afa:wasm-function[14468]:0x880bbe)
    at std::panicking::begin_panic_handler::{{closure}}::h38a949ca54e64e6b (wasm://wasm/06fb8afa:wasm-function[20634]:0x9a25b7)
    at std::sys_common::backtrace::__rust_end_short_backtrace::hc4f3d2fd3090193f (wasm://wasm/06fb8afa:wasm-function[96359]:0xdb2de9)
    at rust_begin_unwind (wasm://wasm/06fb8afa:wasm-function[37469]:0xb90fb5)
    at core::panicking::panic_fmt::h8a19fa1eb63fbb67 (wasm://wasm/06fb8afa:wasm-function[49759]:0xc6982b)
    at core::result::unwrap_failed::h11ff2abd8d53a5bd (wasm://wasm/06fb8afa:wasm-function[24712]:0xa3b225)
    at <futures_util::future::future::fuse::Fuse<Fut> as core::future::future::Future>::poll::h98ef1e254452f433 (wasm://wasm/06fb8afa:wasm-function[643]:0x19addd)
sarahkittyy commented 1 year ago

I changed my app to depend on the unwrap-healing branch of https://github.com/johanhelsing/matchbox/pull/283. This fixes the issue.

simbleau commented 1 year ago

OOh, didn't know #283 would come in handy so fast ;) That's awesome.

unwrap-healing fixes this fundamentally because instead of panicking if a send fails, it tries to send it instead (and ignores the error).

For now, you could use

bevy_matchbox = { git = "https://github.com/johanhelsing/matchbox", branch = "unwrap-healing", features = ["ggrs"] }

It might be a few days or weeks before unwrap-healing makes it into a release because I think #286 needs some discussion and will eventually go into #283.