-
-
Notifications
You must be signed in to change notification settings - Fork 71
fix: RTCDataChannel readyState race condition and improve typings #359
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
Draft
mertushka
wants to merge
15
commits into
murat-dogan:master
Choose a base branch
from
mertushka:fix-rtcdatachannel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
13dd568
fix(polyfill)(RTCDataChannel): fix readyState race condition and impr…
mertushka 5863b40
Merge branch 'murat-dogan:master' into master
mertushka 6e22bd1
revert: unstandard closing event
mertushka 37e5283
chore: remove unnecessary checks
mertushka c62b15c
fix: close existing datachannels before closing peerconnection
mertushka 05381db
fix(polyfill): returning instead of throwing on closing/closed state
mertushka 5100ad9
fix(types): add explicit ArrayBuffer cast for sendMessageBinary calls
mertushka 0ec58d2
chore: remove unneeded closing check
mertushka ddce92f
fix: also check for closing data channels
mertushka b291be2
fix: prevent another race condition window
mertushka 2168c78
fix: dataChannel onClosed race condition
mertushka f3c71ec
fix: match the spec behaviour
mertushka dc6a887
chore: remove comment
mertushka 0081313
fix(RTCPeerConnection): implement spec-compliant abrupt data channel …
mertushka 9cbbf63
fix(RTCDataChannel): improve close event handling logic
mertushka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,15 +48,19 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| }); | ||
|
|
||
| this.#dataChannel.onClosed(() => { | ||
| // Simulate closing event | ||
| if (this.#readyState === 'closed') return; | ||
|
|
||
| if (!this.#closeRequested) { | ||
| // if close was not requested, we emit 'closing' before 'close' to match the spec behavior | ||
| this.#readyState = 'closing'; | ||
| this.dispatchEvent(new Event('closing')); | ||
| } | ||
|
|
||
| setImmediate(() => { | ||
| this.#readyState = 'closed'; | ||
| this.dispatchEvent(new Event('close')); | ||
| if (this.#readyState !== 'closed') { | ||
| this.#readyState = 'closed'; | ||
| this.dispatchEvent(new Event('close')); | ||
| } | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to use |
||
| }); | ||
|
|
||
|
|
@@ -79,32 +83,22 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
|
|
||
| this.#dataChannel.onMessage((data) => { | ||
| if (ArrayBuffer.isView(data)) { | ||
| if (this.binaryType == 'arraybuffer') data = data.buffer; | ||
| else data = Buffer.from(data.buffer); | ||
| data = | ||
| this.binaryType === 'arraybuffer' | ||
| ? (data.buffer as ArrayBuffer) | ||
| : Buffer.from(data.buffer); | ||
| } | ||
|
|
||
| this.dispatchEvent(new MessageEvent('message', { data })); | ||
| }); | ||
|
|
||
| // forward events to properties | ||
| this.addEventListener('message', (e) => { | ||
| if (this.onmessage) this.onmessage(e as MessageEvent); | ||
| }); | ||
| this.addEventListener('bufferedamountlow', (e) => { | ||
| if (this.onbufferedamountlow) this.onbufferedamountlow(e); | ||
| }); | ||
| this.addEventListener('error', (e) => { | ||
| if (this.onerror) this.onerror(e as RTCErrorEvent); | ||
| }); | ||
| this.addEventListener('close', (e) => { | ||
| if (this.onclose) this.onclose(e); | ||
| }); | ||
| this.addEventListener('closing', (e) => { | ||
| if (this.onclosing) this.onclosing(e); | ||
| }); | ||
| this.addEventListener('open', (e) => { | ||
| if (this.onopen) this.onopen(e); | ||
| }); | ||
| this.addEventListener('open', (e) => this.onopen?.(e)); | ||
| this.addEventListener('message', (e) => this.onmessage?.(e as MessageEvent)); | ||
| this.addEventListener('error', (e) => this.onerror?.(e as RTCErrorEvent)); | ||
| this.addEventListener('close', (e) => this.onclose?.(e)); | ||
| this.addEventListener('closing', (e) => this.onclosing?.(e)); | ||
| this.addEventListener('bufferedamountlow', (e) => this.onbufferedamountlow?.(e)); | ||
| } | ||
|
|
||
| set binaryType(type) { | ||
|
|
@@ -168,7 +162,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| return this.#readyState; | ||
| } | ||
|
|
||
| send(data): void { | ||
| send(data: string | Blob | ArrayBuffer | ArrayBufferView): void { | ||
| if (this.#readyState !== 'open') { | ||
| throw new exceptions.InvalidStateError( | ||
| "Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'", | ||
|
|
@@ -190,15 +184,19 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT | |
| this.#dataChannel.sendMessageBinary(data); | ||
| } else { | ||
| if (process?.versions?.bun) { | ||
| this.#dataChannel.sendMessageBinary(Buffer.from(data)); | ||
| this.#dataChannel.sendMessageBinary(Buffer.from(data as ArrayBuffer)); | ||
| } else { | ||
| this.#dataChannel.sendMessageBinary(new Uint8Array(data)); | ||
| this.#dataChannel.sendMessageBinary(new Uint8Array(data as ArrayBuffer)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| close(): void { | ||
| if (this.#readyState === 'closing' || this.#readyState === 'closed') return; | ||
|
|
||
| this.#closeRequested = true; | ||
| this.#readyState = 'closing'; | ||
|
|
||
| setImmediate(() => { | ||
| this.#dataChannel.close(); | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was incorrect. Why would we simulate a "closing" event when the data channel is already closed? Instead, we start the "closing" state at the beginning of the
<RTCDataChannel>.close()method, not here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a gap in the libdatachannel API. Step 4 of 6.2.4 says:
We don't get a "closing" notification from libdatachannel before the channel is actually closed so this is the best we can do in order to emit the events required by the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So how are we gonna handle both cases?
Step 5 of https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close
NOTE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linked case is when the RTCPeerConnection is explicitly closed by the user so set a flag or a status and use that to decide whether or not to emit the event(s)?