vscode-flix icon indicating copy to clipboard operation
vscode-flix copied to clipboard

refactor: simplify websocket code

Open sockmaster27 opened this issue 1 year ago • 3 comments

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

Fixes #305

sockmaster27 avatar Jul 07 '24 12:07 sockmaster27

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

magnus-madsen avatar Jul 27 '24 13:07 magnus-madsen

@sockmaster27 Some conflicts, unfortunately :)

magnus-madsen avatar Jul 29 '24 14:07 magnus-madsen

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

magnus-madsen avatar Aug 01 '24 19:08 magnus-madsen

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.

sockmaster27 avatar Aug 21 '24 09:08 sockmaster27

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

magnus-madsen avatar Aug 21 '24 09:08 magnus-madsen

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

sockmaster27 avatar Aug 21 '24 10:08 sockmaster27

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?

magnus-madsen avatar Aug 22 '24 08:08 magnus-madsen

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.

sockmaster27 avatar Aug 22 '24 10:08 sockmaster27

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 avatar Aug 22 '24 12:08 magnus-madsen

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?

magnus-madsen avatar Aug 22 '24 12:08 magnus-madsen

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.

sockmaster27 avatar Aug 22 '24 12:08 sockmaster27

But sometimes the compiler process dies,

It should really not.

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

magnus-madsen avatar Aug 22 '24 14:08 magnus-madsen

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

sockmaster27 avatar Aug 22 '24 14:08 sockmaster27

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.

magnus-madsen avatar Aug 22 '24 14:08 magnus-madsen

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.

sockmaster27 avatar Aug 22 '24 14:08 sockmaster27

I don't want this effect:

image

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

magnus-madsen avatar Aug 22 '24 16:08 magnus-madsen

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.

sockmaster27 avatar Aug 23 '24 09:08 sockmaster27

Can you double check:

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

magnus-madsen avatar Aug 23 '24 20:08 magnus-madsen

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

sockmaster27 avatar Aug 30 '24 14:08 sockmaster27