Skip to content

Commit 4c999cb

Browse files
committed
fix(vscode): handle a failed restart outside the serialized run
Review feedback on #6057. The restart failure handler ran inside the serialized task, and the not_signed_in branch of it signs the user in and then restarts the client again. That restart waited on the run that was still waiting on the handler, so a successful Tobiko Cloud sign-in never completed and the server stayed stopped. The run now records the error and returns; the caller handles it once the run has finished, so the restart triggered by signing in starts a fresh run. The error is claimed by whichever caller reads it first, so callers coalesced into the same run do not each report it. Covered by a test that mirrors the shape of the real restart. That test only works because the simulated run awaits before it reports failure: without that await the re-entrant call lands in the synchronous prefix of the run, before it is recorded as in flight, and no deadlock is possible. Verified it times out against the previous arrangement. Signed-off-by: Adegbite Ayoade <tripleaceme@gmail.com>
1 parent 2750fe6 commit 4c999cb

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

‎vscode/extension/src/extension.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { onDidChangePythonInterpreter } from './utilities/common/python'
2727
import { requiresLspRestart } from './utilities/common/configurationChange'
2828
import { coalesceAsync } from './utilities/coalesceAsync'
2929
import { sleep } from './utilities/sleep'
30-
import { handleError } from './utilities/errors'
30+
import { ErrorType, handleError } from './utilities/errors'
3131

3232
import { selector, completionProvider } from './completion/completion'
3333
import { LineagePanel } from './webviews/lineagePanel'
@@ -73,6 +73,14 @@ export async function activate(context: vscode.ExtensionContext) {
7373
*/
7474
let restartInvokedByUser = false
7575

76+
/**
77+
* Set by a failed run and handled by the caller once the run has finished.
78+
* Handling it inside the run would deadlock: the not_signed_in handler waits
79+
* on a sign-in that restarts the client again, and that restart would wait on
80+
* the run that is still waiting on the handler.
81+
*/
82+
let restartError: ErrorType | undefined
83+
7684
const runRestart = async (): Promise<void> => {
7785
const invokedByUser = restartInvokedByUser
7886
restartInvokedByUser = false
@@ -84,12 +92,7 @@ export async function activate(context: vscode.ExtensionContext) {
8492
traceVerbose('Restarting SQLMesh LSP client')
8593
const result = await lspClient.restart(invokedByUser)
8694
if (isErr(result)) {
87-
await handleError(
88-
authProvider,
89-
restartLsp,
90-
result.error,
91-
'LSP restart failed',
92-
)
95+
restartError = result.error
9396
return
9497
}
9598

@@ -116,6 +119,13 @@ export async function activate(context: vscode.ExtensionContext) {
116119
const restartLsp = async (invokedByUser = false): Promise<void> => {
117120
restartInvokedByUser = restartInvokedByUser || invokedByUser
118121
await restartLspSerialized()
122+
123+
// Claimed so that callers coalesced into the same run don't each report it.
124+
const error = restartError
125+
restartError = undefined
126+
if (error) {
127+
await handleError(authProvider, restartLsp, error, 'LSP restart failed')
128+
}
119129
}
120130

121131
// commands needing the restart helper

‎vscode/extension/src/utilities/coalesceAsync.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,46 @@ describe('coalesceAsync', () => {
112112
task.release()
113113
await second
114114
})
115+
116+
// A failed restart is reported to the caller, which handles it after the run
117+
// has finished. The not_signed_in handler signs the user in and restarts the
118+
// client again, so that handler must not run inside the task: the restart it
119+
// triggers would wait on the run that is still waiting on the handler, and
120+
// neither would ever settle. See #5920.
121+
it('lets a failure handler trigger another run without deadlocking', async () => {
122+
let failNextRun = true
123+
let reportedError: string | undefined
124+
let runs = 0
125+
126+
const runRestart = async (): Promise<void> => {
127+
runs += 1
128+
// The real run awaits the client restart before it knows the outcome.
129+
// Without that await the re-entrant call below lands in the synchronous
130+
// prefix of the run, before it has been recorded as in flight, and the
131+
// deadlock this covers cannot happen.
132+
const failed = await Promise.resolve(failNextRun)
133+
if (failed) {
134+
failNextRun = false
135+
reportedError = 'not_signed_in'
136+
}
137+
}
138+
139+
const runRestartSerialized = coalesceAsync(runRestart)
140+
141+
const restart = async (): Promise<void> => {
142+
await runRestartSerialized()
143+
144+
const error = reportedError
145+
reportedError = undefined
146+
if (error === 'not_signed_in') {
147+
// Stands in for the sign-in flow, which restarts once signed in.
148+
await restart()
149+
}
150+
}
151+
152+
await restart()
153+
154+
expect(runs).toBe(2)
155+
expect(reportedError).toBeUndefined()
156+
}, 2_000)
115157
})

0 commit comments

Comments
 (0)