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): pass on uncaught errors in idleTimeout #21846

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions cli/tests/unit/websocket_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,33 @@ Deno.test(
await Promise.all([deferred.promise, server.finished]);
},
);

Deno.test(
{ sanitizeOps: false },
async function websocketServerGetsGhosted() {
const ac = new AbortController();
const listeningDeferred = Promise.withResolvers<void>();

const server = Deno.serve({
handler: (req) => {
const { socket, response } = Deno.upgradeWebSocket(req, {
idleTimeout: 2,
});
socket.onerror = () => socket.close();
socket.onclose = () => ac.abort();
return response;
},
signal: ac.signal,
onListen: () => listeningDeferred.resolve(),
hostname: "localhost",
port: servePort,
});

await listeningDeferred.promise;
const r = await fetch("https://localhost:4545/ghost_ws_client");
assertEquals(r.status, 200);
await r.body?.cancel();

await server.finished;
},
);
7 changes: 5 additions & 2 deletions ext/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,15 @@ class WebSocket extends EventTarget {
clearTimeout(this[_idleTimeoutTimeout]);
this[_idleTimeoutTimeout] = setTimeout(async () => {
if (this[_readyState] === OPEN) {
await op_ws_send_ping(this[_rid]);
await PromisePrototypeCatch(op_ws_send_ping(this[_rid]), () => {});
this[_idleTimeoutTimeout] = setTimeout(async () => {
if (this[_readyState] === OPEN) {
this[_readyState] = CLOSING;
const reason = "No response from ping frame.";
await op_ws_close(this[_rid], 1001, reason);
await PromisePrototypeCatch(
op_ws_close(this[_rid], 1001, reason),
() => {},
);
this[_readyState] = CLOSED;

const errEvent = new ErrorEvent("error", {
Expand Down
48 changes: 48 additions & 0 deletions test_util/src/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,54 @@ async fn main_server(
);
Ok(response)
}
(&Method::GET, "/ghost_ws_client") => {
use tokio::io::AsyncReadExt;

let mut tcp_stream = TcpStream::connect("localhost:4248").await.unwrap();
#[cfg(unix)]
// SAFETY: set socket keep alive.
unsafe {
use std::os::fd::AsRawFd;

let fd = tcp_stream.as_raw_fd();
let mut val: libc::c_int = 1;
let r = libc::setsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_KEEPALIVE,
&mut val as *mut _ as *mut libc::c_void,
std::mem::size_of_val(&val) as libc::socklen_t,
);
assert_eq!(r, 0);
}

// Typical websocket handshake request.
let headers = [
"GET / HTTP/1.1",
"Host: localhost",
"Upgrade: websocket",
"Connection: Upgrade",
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==",
"Sec-WebSocket-Version: 13",
"\r\n",
]
.join("\r\n");
tcp_stream.write_all(headers.as_bytes()).await.unwrap();

let mut buf = [0u8; 200];
let n = tcp_stream.read(&mut buf).await.unwrap();
assert!(n > 0);

// Ghost the server:
// - Close the read half of the connection.
// - forget the TcpStream.
let tcp_stream = tcp_stream.into_std().unwrap();
let _ = tcp_stream.shutdown(std::net::Shutdown::Read);
std::mem::forget(tcp_stream);

let res = Response::new(empty_body());
Ok(res)
}
(_, "/multipart_form_data.txt") => {
let b = "Preamble\r\n\
--boundary\t \r\n\
Expand Down