From 8b1188cc0f225968fd61b24d2340a47ac51b8009 Mon Sep 17 00:00:00 2001 From: Simon Sobisch Date: Wed, 7 Feb 2024 08:26:33 +0000 Subject: [PATCH] cleanInvalidSocketPath on start check for existing and invalid debugserver sockets on start and close those originally done by @chenhaoyang2019 as a side change in fd7d8ee1642b6e29e84667daefd4083c0771af45 at https://gitee.com/openkylin/native-debug.git --- CHANGELOG.md | 7 +++++++ src/mibase.ts | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab0c2613..cbb419ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ Versioning]. [keep a changelog]: https://keepachangelog.com/en/1.0.0 [semantic versioning]: https://semver.org/spec/v2.0.0.html +## Unreleased + +### 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 55cfca01..e45ac9a9 100644 --- a/src/mibase.ts +++ b/src/mibase.ts @@ -48,7 +48,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)); @@ -64,6 +64,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(); @@ -75,7 +79,7 @@ export class MI2DebugSession extends DebugSession { args = JSON.parse(rawCmd.substring(spaceIndex + 1)); } Promise.resolve(this.miDebugger[func].apply(this.miDebugger, args)).then(data => { - c.write(data.toString()); + c.write(data instanceof Object ? JSON.stringify(data).toString() : data.toString()); }); }); }); @@ -516,10 +520,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 expanded = expandValue(createVariable, variable.result("value"), id, variable); if (!expanded) { @@ -759,3 +762,38 @@ function prettyStringArray(strings) { 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(''); + }); + }); +}