Skip to content

Commit 392ef6a

Browse files
committed
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
1 parent 9fb2875 commit 392ef6a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Versioning].
88
[keep a changelog]: https://keepachangelog.com/en/1.0.0
99
[semantic versioning]: https://semver.org/spec/v2.0.0.html
1010

11+
## Unreleased
12+
13+
### Fixed
14+
15+
- invalid existing debugserver sockets are closed on start
16+
(@chenhaoyang2019, [@GitMensch])
17+
1118
## [0.27.0] - 2024-02-07
1219

1320
### Added

src/mibase.ts

+42-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class MI2DebugSession extends DebugSession {
4848
super(debuggerLinesStartAt1, isServer);
4949
}
5050

51-
protected initDebugger() {
51+
protected async initDebugger() {
5252
this.miDebugger.on("launcherror", this.launchError.bind(this));
5353
this.miDebugger.on("quit", this.quitEvent.bind(this));
5454
this.miDebugger.on("exited-normally", this.quitEvent.bind(this));
@@ -64,6 +64,10 @@ export class MI2DebugSession extends DebugSession {
6464
this.miDebugger.on("thread-exited", this.threadExitedEvent.bind(this));
6565
this.miDebugger.once("debug-ready", (() => this.sendEvent(new InitializedEvent())));
6666
try {
67+
const socketlists = systemPath.join(os.tmpdir(), "code-debug-sockets");
68+
if (fs.existsSync(socketlists)) {
69+
await cleanInvalidSocketPath(socketlists);
70+
}
6771
this.commandServer = net.createServer(c => {
6872
c.on("data", data => {
6973
const rawCmd = data.toString();
@@ -75,7 +79,7 @@ export class MI2DebugSession extends DebugSession {
7579
args = JSON.parse(rawCmd.substring(spaceIndex + 1));
7680
}
7781
Promise.resolve(this.miDebugger[func].apply(this.miDebugger, args)).then(data => {
78-
c.write(data.toString());
82+
c.write(data instanceof Object ? JSON.stringify(data).toString() : data.toString());
7983
});
8084
});
8185
});
@@ -516,10 +520,9 @@ export class MI2DebugSession extends DebugSession {
516520
}
517521
} else if (typeof id == "string") {
518522
// Variable members
519-
let variable;
520523
try {
521524
// TODO: this evaluates on an (effectively) unknown thread for multithreaded programs.
522-
variable = await this.miDebugger.evalExpression(JSON.stringify(id), 0, 0);
525+
const variable = await this.miDebugger.evalExpression(JSON.stringify(id), 0, 0);
523526
try {
524527
let expanded = expandValue(createVariable, variable.result("value"), id, variable);
525528
if (!expanded) {
@@ -759,3 +762,38 @@ function prettyStringArray(strings) {
759762
return JSON.stringify(strings);
760763
} else return strings;
761764
}
765+
766+
async function cleanInvalidSocketPath(socketlists:string){
767+
return new Promise((resolve, reject) => {
768+
fs.readdir(socketlists, (err, files) => {
769+
if (!err) {
770+
if (files.length == 0) resolve('');
771+
files.forEach((file)=>{
772+
try {
773+
const conn = net.connect(systemPath.join(socketlists, file));
774+
conn.setTimeout(200);
775+
conn.on('error', ()=>{
776+
fs.unlink(systemPath.join(socketlists, file), (err) => {
777+
if (err)
778+
// eslint-disable-next-line no-console
779+
console.error("Failed to unlink invalid debug server");
780+
resolve('');
781+
});
782+
});
783+
conn.on('timeout', ()=>{
784+
conn.destroy();
785+
});
786+
} catch {
787+
fs.unlink(systemPath.join(socketlists, file), (err) => {
788+
if (err)
789+
// eslint-disable-next-line no-console
790+
console.error("Failed to unlink invalid debug server");
791+
resolve('');
792+
});
793+
}
794+
});
795+
}
796+
resolve('');
797+
});
798+
});
799+
}

0 commit comments

Comments
 (0)