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(comlink): ensure nodes always accept handshake synchronize events #2163

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

rdunk
Copy link
Member

@rdunk rdunk commented Nov 14, 2024

Currently, comlink's node state machine will only handle syn handshake events when in an idle state. In some situations, the controller or some of the channels it maintains may detect a loss of connection and transition to a handshaking state, the disconnect event emitted at this point may then fail to reach the relevant node(s), and thus the node will remain in a connected state, as the nodes themselves only blindly respond to heartbeat events.

This PR adds a change so that the node state machine will always transition to its internal handshaking state when receiving a syn handshake event, regardless of the state it is currently in. This should greatly improve the success rate of attempt reconnects. For example you may have noticed in our test environments that the connection is often lost after a HMR, this should no longer be the case.

It also drops the default expected response timeout to 3s from 10s for requests for all requests that expect a response except for those initiated with .fetch (which remains at 10s).

@rdunk rdunk requested a review from a team as a code owner November 14, 2024 17:39
Copy link

vercel bot commented Nov 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
live-visual-editing-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-astro ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-next-with-i18n ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-nuxt ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-page-builder-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-remix ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm
visual-editing-svelte ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 5:45pm

@@ -260,8 +260,7 @@ export const createController = (input: {targetOrigin: string}): Controller => {

const stop = () => {
channels.forEach((channel) => {
channel.disconnect()
channel.stop()
cleanupChannel(channel as unknown as Channel<Message, Message>)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the following also work?

Suggested change
cleanupChannel(channel as unknown as Channel<Message, Message>)
cleanupChannel(channel)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not as it stands, I guess this has to do with type variance? channel here is of type Channel<R, S>, and that isn't considered a subtype of Channel<Message, Message> even if R and S extend Message...or something like that.

I need to look at improving the typing in places, if you have any suggestions they'd be very welcome! 😊

Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! :O 💖

@stipsan stipsan merged commit 5e31bec into main Nov 14, 2024
20 checks passed
@stipsan stipsan deleted the fix/comlink-reconnection branch November 14, 2024 19:53
@ecospark ecospark bot mentioned this pull request Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants