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(ext/websocket): order of ws writes #19131

Merged
merged 2 commits into from
May 18, 2023
Merged
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
4 changes: 4 additions & 0 deletions ext/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ where
let resource = ServerWebSocket {
ws: AsyncRefCell::new(FragmentCollector::new(stream)),
closed: Rc::new(Cell::new(false)),
tx_lock: AsyncRefCell::new(()),
};
let mut state = state.borrow_mut();
let rid = state.resource_table.add(resource);
Expand Down Expand Up @@ -317,6 +318,7 @@ pub enum MessageKind {
pub struct ServerWebSocket {
ws: AsyncRefCell<FragmentCollector<WebSocketStream>>,
closed: Rc<Cell<bool>>,
tx_lock: AsyncRefCell<()>,
}

impl ServerWebSocket {
Expand All @@ -325,6 +327,7 @@ impl ServerWebSocket {
self: Rc<Self>,
frame: Frame,
) -> Result<(), AnyError> {
let _lock = RcRef::map(&self, |r| &r.tx_lock).borrow_mut().await;
// SAFETY: fastwebsockets only needs a mutable reference to the WebSocket
// to populate the write buffer. We encounter an await point when writing
// to the socket after the frame has already been written to the buffer.
Expand Down Expand Up @@ -361,6 +364,7 @@ pub fn ws_create_server_stream(
let ws_resource = ServerWebSocket {
ws: AsyncRefCell::new(FragmentCollector::new(ws)),
closed: Rc::new(Cell::new(false)),
tx_lock: AsyncRefCell::new(()),
};

let rid = state.resource_table.add(ws_resource);
Expand Down