Skip to content

Commit

Permalink
cleanInvalidSocketPath on start
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GitMensch committed Feb 7, 2024
1 parent 9fb2875 commit 8b1188c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 42 additions & 4 deletions src/mibase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
Expand All @@ -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());
});
});
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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('');
});
});
}

0 comments on commit 8b1188c

Please sign in to comment.