mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix(websocket): ignore resource close error (#9755)
It is possible that the WebSocket is already closed when we try to close it with `WebSocket#close` or in the `error` or `close` events. Currently this leads to an uncatchable promise rejection. This changes this so that closing an already closed WebSocket is a noop.
This commit is contained in:
parent
2d7fdb0a19
commit
0e72129da2
1 changed files with 17 additions and 4 deletions
|
@ -25,6 +25,19 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to close the resource (and ignores BadResource errors).
|
||||||
|
* @param {number} rid
|
||||||
|
*/
|
||||||
|
function tryClose(rid) {
|
||||||
|
try {
|
||||||
|
core.close(rid);
|
||||||
|
} catch (err) {
|
||||||
|
// Ignore error if the socket has already been closed.
|
||||||
|
if (!(err instanceof Deno.errors.BadResource)) throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handlerSymbol = Symbol("eventHandlers");
|
const handlerSymbol = Symbol("eventHandlers");
|
||||||
function makeWrappedHandler(handler) {
|
function makeWrappedHandler(handler) {
|
||||||
function wrappedHandler(...args) {
|
function wrappedHandler(...args) {
|
||||||
|
@ -125,7 +138,7 @@
|
||||||
const event = new CloseEvent("close");
|
const event = new CloseEvent("close");
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
core.close(this.#rid);
|
tryClose(this.#rid);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.#readyState = OPEN;
|
this.#readyState = OPEN;
|
||||||
|
@ -289,7 +302,7 @@
|
||||||
});
|
});
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
core.close(this.#rid);
|
tryClose(this.#rid);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -350,7 +363,7 @@
|
||||||
});
|
});
|
||||||
event.target = this;
|
event.target = this;
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
core.close(this.#rid);
|
tryClose(this.#rid);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -365,7 +378,7 @@
|
||||||
const closeEv = new CloseEvent("close");
|
const closeEv = new CloseEvent("close");
|
||||||
closeEv.target = this;
|
closeEv.target = this;
|
||||||
this.dispatchEvent(closeEv);
|
this.dispatchEvent(closeEv);
|
||||||
core.close(this.#rid);
|
tryClose(this.#rid);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue