Skip to content

Commit c17be01

Browse files
committed
debug: recover gracefully from a stuck auto attach pipe
1 parent e097cf1 commit c17be01

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

extensions/debug-auto-launch/src/extension.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { promises as fs } from 'fs';
67
import { createServer, Server } from 'net';
78
import * as vscode from 'vscode';
89
import * as nls from 'vscode-nls';
@@ -203,7 +204,27 @@ async function createAttachServer(context: vscode.ExtensionContext) {
203204
return undefined;
204205
}
205206

206-
server = new Promise<Server>((resolve, reject) => {
207+
server = createServerInner(ipcAddress).catch(err => {
208+
console.error(err);
209+
return undefined;
210+
});
211+
212+
return await server;
213+
}
214+
215+
const createServerInner = async (ipcAddress: string) => {
216+
try {
217+
return await createServerInstance(ipcAddress);
218+
} catch (e) {
219+
// On unix/linux, the file can 'leak' if the process exits unexpectedly.
220+
// If we see this, try to delete the file and then listen again.
221+
await fs.unlink(ipcAddress).catch(() => undefined);
222+
return await createServerInstance(ipcAddress);
223+
}
224+
};
225+
226+
const createServerInstance = (ipcAddress: string) =>
227+
new Promise<Server>((resolve, reject) => {
207228
const s = createServer(socket => {
208229
let data: Buffer[] = [];
209230
socket.on('data', async chunk => {
@@ -229,14 +250,8 @@ async function createAttachServer(context: vscode.ExtensionContext) {
229250
})
230251
.on('error', reject)
231252
.listen(ipcAddress, () => resolve(s));
232-
}).catch(err => {
233-
console.error(err);
234-
return undefined;
235253
});
236254

237-
return await server;
238-
}
239-
240255
/**
241256
* Destroys the auto-attach server, if it's running.
242257
*/

0 commit comments

Comments
 (0)