Repository navigation
Close RPC sessions on server restart. - #66
Conversation
|
Cool, does this fix issue #58 ? |
|
I believe this is intended to fix a different issue, namely the one where RPC sessions are not closed on server restart -- see Zulip. However this is not the right fix. The issue seems to be that:
|
I put some console.logging in RpcSession constructor then replayed the scenario of opening 2 files, restarting the server then switching the files and the log output shows the expected RpcSessions are created and each rpc call is passing the correct document Uri: |
|
A print statement here is not triggered for me on server restart, so indeed it looks like |
|
From what I can tell the |
|
I found and fixed a related bug here: #69 |
We should send the diagnostics, progress and position on EDIT: I can't get the HTML resetting to work, but we can reset the React UI quite easily ourselves. I implemented this alternative in #70. |
| if (this.webviewPanel && this.client.initializeResult && window.activeTextEditor){ | ||
| await this.webviewPanel.api.initialize(this.client.initializeResult, this.getLocation(window.activeTextEditor)); | ||
| } | ||
| void this.webviewPanel?.api?.serverRestarted() |
There was a problem hiding this comment.
I think the void this.webviewPanel?.api?.serverRestarted() event should be fired before calling this.webviewPanel.api.initialize
…ive diagnostics for the current line.
There were a few issues related to the message protocol that got in the way here. For example the I have now refactored the code a bit so that the diagnostics/progress contexts are always created, even if there is no server connected. This prevents race conditions that are caused by react's asynchronous updating. Since I was already refactoring the diagnostics context, I've also implemented #52.
I think we want to persist all UI state, open details, pins, tactic state filters, etc. The intention is that you can restart the server frequently and easily in Lean 4. |
| [] | ||
| ) | ||
|
|
||
| useEvent(ec.events.serverRestarted, () => sessions.ensureAllSessionsClosed()) |
There was a problem hiding this comment.
Instead of adding even more complexity to RpcSessions with #closed, why don't we leave it as-is, and instead call dispose(), then setSessions(new RpcSessions(ec)) here?
There was a problem hiding this comment.
That's not a bad idea! But I believe the closed flag will still be necessary to prevent references from the old session to be released.
There was a problem hiding this comment.
Ah yes, unfortunately. But we should still make a new RpcSessions object to make sure the promise chain in connectAt does not make modifications to this after a server restart (I don't think this can happen, but then again I didn't think the bugs we've seen would happen either). In that case dispose does not need to clear out #connected/#connecting, just has to clear the timeouts and set #closed so the FinalizationRegistry stops sending releases.
Thanks your new bits work really well, switching Lean server versions is now very smooth, and the bug I reported before is fixed, the new edits in the .lean file are being picked up and new diagnostics are being shown correctly. |
| <EditorContext.Provider value={ec}> | ||
| <Main/> | ||
| </EditorContext.Provider> | ||
| </React.StrictMode>, uiElement) |
|
|
||
| // this.client.initializeResult can be null if the server failed to start. | ||
| if (this.client.initializeResult) { | ||
| await webviewPanel.api.initialize(this.client.initializeResult, this.getLocation(editor)) |
There was a problem hiding this comment.
Much nicer, separating out the this.client.initializeResult until the separate serverRestarted event makes a lot of sense.
|
|
||
| let allReq = undefined | ||
| if (sv.hasWidgetsV1()) { | ||
| if (sv?.hasWidgetsV1()) { |
There was a problem hiding this comment.
Very minor, but I would use sv! here keeping in mind the invariant that the message UI is not rendered if sv === undefined.
| [] | ||
| ); | ||
|
|
||
| const serverInitializeResult = useEventResult(ec.events.serverRestarted); |
There was a problem hiding this comment.
If useEventResult were higher-order and taking a function that maps the event value, we could also use it for uri/events.changedCursorLocation above.
There was a problem hiding this comment.
Good suggestion! But there's no higher-order argument necessary, you can just do a ?.uri afterwards.
There was a problem hiding this comment.
Added the higher-order version anyhow to reduce rerenders.
|
|
||
| useEvent(ec.events.serverRestarted, () => { | ||
| sessions.dispose(); | ||
| setSessions(new RpcSessions(ec)); |
There was a problem hiding this comment.
How does this deal with the possibility that there could be some pending async calls to sendClientRequest or await sesh.call that could complete after the new RpcSessions has been created, then you could have competing async requests, etc... I'd feel better if RpcSessions had a this.disposed flag that each async completion checked and bailed if it is set to true.
There was a problem hiding this comment.
Good point. A lot of this code assumes that the #connected and #connecting maps stay the same (even though the RpcSessions object is recreated every time a session is created). I don't want to refactor this right now.
473b583 to
db230fb
Compare
|
I've noticed something else that's a bit disturbing. We use a lot of these React context objects to capture notifications sent from the server. However this causes serious performance issues. It's obvious once you think of it: <ProgressContext.Provider value={allProgress}>
{ret}
</ProgressContext.Provider>Whenever We need to replace all these providers by |
Isn't the whole point of contexts that only children which |
I don't know, I haven't found a precise description of when react will rerender the components or mount/unmount them. AFAICT by default
Yes, and this is also recommended by react. But this doesn't mean we can ignore this. If the extension is sluggish to the point of being unusable in a development build, then it becomes hard to work on it. |
I can confirm this happens in development mode. It is unfortunate, conceptually I would expect React to keep track of which children actually
I am more worried about this. This sounds like a bug in our logic more than anything about React. The |
That's not what React does, and it's well documented. And to reiterate, it has nothing to do with useContext. It's also independent of development/production build. The dev build only enables some additional warnings, it doesn't change the whole semantics.
Oh sorry, false alarm! I tried my standard stress test: https://gist.github.com/gebner/523230aa3f95a1f40b92aee998494da6 What happened is that I first hovered around in an interactive diagnostic, and this flickers a lot. I was afraid that this happened because I inadvertently swapped the order of the providers so I tried swapping them back. Then I hovered in the expected type and everything was fine. (Of course what actually happened (before and after the swapping) is that the interactive diagnostics are reloaded every time a publishDiagnostics notification arrives, but the expected type is only updated when you move the cursor or the line is still being processed.) |
I see. Per the docs, "Even if an ancestor uses |
|
|
||
| const [isPaused, setPaused, [uri, diags], _] = usePausableState(false, [uri0, diags0]); | ||
| const [isPaused, setPaused, [uri, diags, iDiags], _] = usePausableState(false, [uri0, diags0, iDiags0]); | ||
| if (isPaused) void iDiags() |
There was a problem hiding this comment.
Is this fetching diagnostics when paused? Should it not rather do so on !isPaused?
There was a problem hiding this comment.
Yes, iDiags is a thunk and the code above forces it when the component switches from !isPaused to isPaused. (It will also force it on every subsequent update when the component is paused, but those will be no-ops.)
The idea is that we don't need to load the diagnostics when the "all messages" view is closed and not paused. But when it switches to paused, we need to fetch them.
| if (!diags0) diags0 = []; | ||
| const diags0 = dc.get(uri0) || []; | ||
|
|
||
| let iDiags0 = React.useMemo(() => lazy(async () => { |
There was a problem hiding this comment.
I'm not sure this is the right way to do this. Per React docs: "Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.".
There was a problem hiding this comment.
The memo is fine here. The lazy function is side effect-free, it only creates the thunk but does not execute it.
But maybe we should use useEffect for the iDiags() call below..
Vtec234
left a comment
There was a problem hiding this comment.
Thanks!! My takeaway is that we should rethink:
- the state machine/internal logic in
RpcSessions - the use of React contexts;
Events used correctly may trigger fewer re-renders
Is this the right way to see a render count? When I 'run without debugging" I'm seeing 71 renders during startup, and 36 renders each time I type a character in the .lean file....which does seem a tad excessive... const rendersCountRef = React.useRef(0);
rendersCountRef.current += 1;
return (
<ConfigContext.Provider value={config}>
<VersionContext.Provider value={sv}>
<WithRpcSessions>
<WithLspDiagnosticsContext>
<ProgressContext.Provider value={allProgress}>
{ret}
</ProgressContext.Provider>
</WithLspDiagnosticsContext>
</WithRpcSessions>
</VersionContext.Provider>
<div>
RendersCounter rendered <b>{rendersCountRef.current}</b>
</div>
</ConfigContext.Provider>I also packaged the extension as a vsix built from command line and ran that and the increment is smaller, it increments by |
|
I think I found a bug:
I think the problem is that the infoview does need to send |
I think it is right, and I agree it sounds excessive to call all our render functions this many times. Although it really depends, e.g. if only the top-level component is rendered 36 times but the expensive-to-compute children are memoised and not re-rendered, then it might be fine -- we should probably |
Yes. The numbers don't surprise me. I think this means that there are around 15 commands (def/...) below the cursor. The server sends 2 notifications for every processed command (publishDiagnostics and fileProgress) and we do a rerender for each one of them. |
|
I'm not sure if this is a regression in this PR, but if I leave vscode running in the background for a few minutes (one minute is not enough), then I get the following message (in the infoview hover) when I come back:
(I could reproduce this on master, so I've filed it as #73.) |
I've seen this also. |

Fixes #52.