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

refactor: simplify websocket code #425

Merged
merged 13 commits into from
Sep 3, 2024
Merged

Conversation

sockmaster27
Copy link
Contributor

Outsources the reconnecting part to reconnecting-websocket, like it's done on play.flix.dev.

Fixes #305

@magnus-madsen
Copy link
Member

Does it work if you put your laptop to standby/sleep and resume later?

@magnus-madsen
Copy link
Member

@sockmaster27 Some conflicts, unfortunately :)

server/src/engine/socket.ts Show resolved Hide resolved
server/src/engine/socket.ts Outdated Show resolved Hide resolved
@magnus-madsen
Copy link
Member

I added some concerns + I am not sure if all the old code has been removed.

@sockmaster27 sockmaster27 marked this pull request as draft August 21, 2024 08:49
@sockmaster27
Copy link
Contributor Author

For whatever reason, each reconnect spawns a new process, and I can't figure out why. It doesn't seem to our code that runs.

It doesn't impact anything though, so I don't think it's super important since it behaves how it's supposed to.

@magnus-madsen
Copy link
Member

For whatever reason, each reconnect spawns a new process, and I can't figure out why. It doesn't seem to our code that runs.

It doesn't impact anything though, so I don't think it's super important since it behaves how it's supposed to.

A new Flix compiler instance? Those are pretty heavy.

We dont accidentially send an exit? https://github.com/flix/flix/blob/master/main/src/ca/uwaterloo/flix/api/lsp/LanguageServer.scala#L255

@sockmaster27
Copy link
Contributor Author

Found it. If you're curious, it was reinitializing and spawning a new REPL on every reconnection.

@sockmaster27 sockmaster27 marked this pull request as ready for review August 21, 2024 10:43
client/src/engine/jobs.ts Show resolved Hide resolved
server/src/engine/socket.ts Show resolved Hide resolved
setTimeout(onOpen, 0)
}

webSocket.removeEventListener('open', openHandler)
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain some of the above code, I am not sure how it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried improving it, but let me know if there's still something that should be explained better.

@magnus-madsen
Copy link
Member

Did you try that this works in practice? E.g. if you start VSCode and enter sleep or hibernation then it works when you resume?

@sockmaster27
Copy link
Contributor Author

Did you try that this works in practice? E.g. if you start VSCode and enter sleep or hibernation then it works when you resume?

It does, but I've only managed to make it lose connection a single time. Anyway, the tests should cover it.

@magnus-madsen
Copy link
Member

I think there is confusion: I don't want to restart the compiler. The connection will drop, but we should just be able to reconnect.

@magnus-madsen
Copy link
Member

magnus-madsen commented Aug 22, 2024

Let me sketch the architecture I have in mind:

When the extension starts, before the compiler is started, we find a free port. We then start the compiler.

This allows us to run as many VSCode instances and Flix compilers as we want. Each gets a new fresh port that they can communicate over.

The Flix compiler is never restarted. The only thing that can happen is that during standby the connection between VSCode and the compiler may drop. However, simply reconnecting in loop should make everything work again.

Does it make sense?

@sockmaster27
Copy link
Contributor Author

But sometimes the compiler process dies, e.g. if you (accidentally) kill it in task manager. Right now this will cause the compiler to be relaunched, otherwise VS Code would sit in a loop trying to reconnect until you were to manually restart it.

The restarting will only happen if the compiler is dead, which the extension can infer by seeing if the socket has been freed to the system. Otherwise it will simply reconnect to the same instance.

@magnus-madsen
Copy link
Member

But sometimes the compiler process dies,

It should really not.

The problem with restarting the compiler is resource leaks. I don't want that.

@sockmaster27
Copy link
Contributor Author

Resource leaks in what way?
Again, it only relaunches it if it's already completely gone.

@magnus-madsen
Copy link
Member

Resource leaks in what way? Again, it only relaunches it if it's already completely gone.

You assume that because the port has been released the process has died.

@sockmaster27
Copy link
Contributor Author

Resource leaks in what way? Again, it only relaunches it if it's already completely gone.

You assume that because the port has been released the process has died.

Yes. This is what was done before, and I have yet to see a case where it wasn't true.

Admittedly, this is an edge case, but so is losing connection to the server, so I'm thinking that we might as well handle it correctly while we're at it.

@magnus-madsen
Copy link
Member

I don't want this effect:

image

I want the compiler to be a server -- started once -- that you can reconnect to.

@sockmaster27
Copy link
Contributor Author

Again, I really don't see that as a risk, and I think relaunching is the correct thing to do, but if that's what's blocking I'll remove the feature.

@magnus-madsen
Copy link
Member

Can you double check:

  • Everything still works after standby or hibernate?
  • Starting two VSCode instances works correctly?

@sockmaster27
Copy link
Contributor Author

Can you double check:

* Everything still works after standby or hibernate?

* Starting two VSCode instances works correctly?

Yes, everything works on my machine.

If we want to be extra sure, a non-Windows user might also want to try it.
Heres the built extension: flix-1.29.0.zip (zipped because GitHub doesn't allow .vsix files)
It can be installed via code --install-extension ./flix-1.29.0.vsix --force

@magnus-madsen magnus-madsen merged commit 81db5b4 into flix:master Sep 3, 2024
6 checks passed
@sockmaster27 sockmaster27 deleted the socket branch September 3, 2024 05:31
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.

Simplify websocket code
2 participants