1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

fix(websocket): bad rid on WebSocketStream abort(#12913)

Fix a bad resource ID error when aborting a WebSocketStream immediately
after its creation.
This commit is contained in:
Andreu Botella 2021-11-28 12:25:49 +01:00 committed by GitHub
parent 2d830c263b
commit 1974eb1021
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 9 deletions

View file

@ -2,8 +2,8 @@
import {
assertEquals,
assertRejects,
assertThrows,
assertThrowsAsync,
} from "../../../test_util/std/testing/asserts.ts";
Deno.test("fragment", () => {
@ -57,12 +57,12 @@ Deno.test("echo string tls", async () => {
Deno.test("websocket error", async () => {
const ws = new WebSocketStream("wss://localhost:4242");
await Promise.all([
assertThrowsAsync(
assertRejects(
() => ws.connection,
Deno.errors.UnexpectedEof,
"tls handshake eof",
),
assertThrowsAsync(
assertRejects(
() => ws.closed,
Deno.errors.UnexpectedEof,
"tls handshake eof",
@ -80,3 +80,17 @@ Deno.test("echo uint8array", async () => {
ws.close();
await ws.closed;
});
Deno.test("aborting immediately throws an AbortError", async () => {
const controller = new AbortController();
const wss = new WebSocketStream("ws://localhost:4242", {
signal: controller.signal,
});
controller.abort();
await assertRejects(
() => wss.connection,
DOMException,
"connection was aborted",
);
await assertRejects(() => wss.closed, DOMException, "connection was aborted");
});

View file

@ -239,6 +239,16 @@ where
);
}
let cancel_resource = if let Some(cancel_rid) = args.cancel_handle {
let r = state
.borrow_mut()
.resource_table
.get::<WsCancelResource>(cancel_rid)?;
Some(r)
} else {
None
};
let unsafely_ignore_certificate_errors = state
.borrow()
.try_borrow::<UnsafelyIgnoreCertificateErrors>()
@ -283,13 +293,9 @@ where
let client = client_async(request, socket);
let (stream, response): (WsStream, Response) =
if let Some(cancel_rid) = args.cancel_handle {
let r = state
.borrow_mut()
.resource_table
.get::<WsCancelResource>(cancel_rid)?;
if let Some(cancel_resource) = cancel_resource {
client
.or_cancel(r.0.to_owned())
.or_cancel(cancel_resource.0.to_owned())
.await
.map_err(|_| DomExceptionAbortError::new("connection was aborted"))?
} else {