Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanInvalidSocketPath on start #400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 42 additions & 4 deletions src/mibase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
Expand All @@ -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());
});
});
});
Expand Down Expand Up @@ -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 = /'([^']*)' <repeats (\d+) times>/g;
Expand Down Expand Up @@ -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('');
});
});
}