diff --git a/CHANGELOG.md b/CHANGELOG.md index 47db805..79604af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,11 @@ Versioning]. - Fixes #421 - Added `registerLimit` option to specify the registers to display - PR #444 ([@chenzhiy2001]) +### Fixed + +- close invalid existing sockets from previous usage of this extension during + start of a new debugging session (@chenhaoyang2019, [@GitMensch]) + ## [0.27.0] - 2024-02-07 ### Added diff --git a/src/mibase.ts b/src/mibase.ts index ba3fbeb..274888e 100644 --- a/src/mibase.ts +++ b/src/mibase.ts @@ -49,7 +49,7 @@ export class MI2DebugSession extends DebugSession { super(debuggerLinesStartAt1, isServer); } - protected initDebugger() { + protected async initDebugger() { this.miDebugger.on("launcherror", this.launchError.bind(this)); this.miDebugger.on("quit", this.quitEvent.bind(this)); this.miDebugger.on("exited-normally", this.quitEvent.bind(this)); @@ -65,6 +65,10 @@ export class MI2DebugSession extends DebugSession { this.miDebugger.on("thread-exited", this.threadExitedEvent.bind(this)); this.miDebugger.once("debug-ready", (() => this.sendEvent(new InitializedEvent()))); try { + const socketlists = systemPath.join(os.tmpdir(), "code-debug-sockets"); + if (fs.existsSync(socketlists)) { + await cleanInvalidSocketPath(socketlists); + } this.commandServer = net.createServer(c => { c.on("data", data => { const rawCmd = data.toString(); @@ -76,7 +80,7 @@ export class MI2DebugSession extends DebugSession { args = JSON.parse(rawCmd.substring(spaceIndex + 1)); } Promise.resolve((this.miDebugger as any)[func].apply(this.miDebugger, args)).then(data => { - c.write(data.toString()); + c.write(data instanceof Object ? JSON.stringify(data).toString() : data.toString()); }); }); }); @@ -532,10 +536,9 @@ export class MI2DebugSession extends DebugSession { } } else if (typeof id == "string") { // Variable members - let variable; try { // TODO: this evaluates on an (effectively) unknown thread for multithreaded programs. - variable = await this.miDebugger.evalExpression(JSON.stringify(id), 0, 0); + const variable = await this.miDebugger.evalExpression(JSON.stringify(id), 0, 0); try { let variableValue = variable.result("value"); const pattern = /'([^']*)' /g; @@ -787,3 +790,38 @@ function prettyStringArray(strings: any) { return JSON.stringify(strings); } else return strings; } + +async function cleanInvalidSocketPath(socketlists:string){ + return new Promise((resolve, reject) => { + fs.readdir(socketlists, (err, files) => { + if (!err) { + if (files.length == 0) resolve(''); + files.forEach((file)=>{ + try { + const conn = net.connect(systemPath.join(socketlists, file)); + conn.setTimeout(200); + conn.on('error', ()=>{ + fs.unlink(systemPath.join(socketlists, file), (err) => { + if (err) + // eslint-disable-next-line no-console + console.error("Failed to unlink invalid debug server"); + resolve(''); + }); + }); + conn.on('timeout', ()=>{ + conn.destroy(); + }); + } catch { + fs.unlink(systemPath.join(socketlists, file), (err) => { + if (err) + // eslint-disable-next-line no-console + console.error("Failed to unlink invalid debug server"); + resolve(''); + }); + } + }); + } + resolve(''); + }); + }); +}