@ohos-ports/floss 5.0.1-beta.6 → 5.0.1-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser-runner.js +68 -0
- package/package.json +1 -1
package/lib/browser-runner.js
CHANGED
|
@@ -68,6 +68,71 @@ function execOhosBm(args, timeout) {
|
|
|
68
68
|
return execCmd(OHOS_BM_PATH, args, timeout);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
// ─── Browser process cleanup ─────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
var killedPids = new Set();
|
|
74
|
+
|
|
75
|
+
function getBrowserPids() {
|
|
76
|
+
try {
|
|
77
|
+
var { execFileSync } = require("child_process");
|
|
78
|
+
var output = execFileSync("ps", ["-A", "-o", "pid,name"], {
|
|
79
|
+
timeout: 5000,
|
|
80
|
+
encoding: "utf-8",
|
|
81
|
+
}).trim();
|
|
82
|
+
var pids = [];
|
|
83
|
+
var lines = output.split("\n");
|
|
84
|
+
for (var i = 0; i < lines.length; i++) {
|
|
85
|
+
var line = lines[i].trim();
|
|
86
|
+
if (line.indexOf("htbrowser") !== -1 || line.indexOf(BROWSER_PKG) !== -1) {
|
|
87
|
+
var parts = line.split(/\s+/);
|
|
88
|
+
var pid = parseInt(parts[0], 10);
|
|
89
|
+
if (!isNaN(pid) && pid > 0) {
|
|
90
|
+
pids.push(pid);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return pids;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isProcessAlive(pid) {
|
|
101
|
+
try {
|
|
102
|
+
process.kill(pid, 0);
|
|
103
|
+
return true;
|
|
104
|
+
} catch (e) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function forceKillBrowserProcesses() {
|
|
110
|
+
for (var attempt = 0; attempt < 3; attempt++) {
|
|
111
|
+
var pids = getBrowserPids();
|
|
112
|
+
if (pids.length === 0) break;
|
|
113
|
+
|
|
114
|
+
var anyKilled = false;
|
|
115
|
+
for (var i = 0; i < pids.length; i++) {
|
|
116
|
+
var pid = pids[i];
|
|
117
|
+
if (killedPids.has(pid)) continue;
|
|
118
|
+
if (isProcessAlive(pid)) {
|
|
119
|
+
try {
|
|
120
|
+
process.kill(pid, "SIGKILL");
|
|
121
|
+
killedPids.add(pid);
|
|
122
|
+
anyKilled = true;
|
|
123
|
+
} catch (e) {
|
|
124
|
+
killedPids.add(pid);
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
killedPids.add(pid);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!anyKilled) break;
|
|
132
|
+
await sleep(500);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
71
136
|
// ─── Browser launch / close ───────────────────────────────────────────
|
|
72
137
|
|
|
73
138
|
function buildLaunchArgs() {
|
|
@@ -99,6 +164,8 @@ async function launchBrowser() {
|
|
|
99
164
|
// 2. Kill existing browser process
|
|
100
165
|
try { execOhosAa(["force-stop", "--bundlename", BROWSER_PKG], 10000); } catch {}
|
|
101
166
|
await sleep(1000);
|
|
167
|
+
// Poll for surviving processes and SIGKILL stragglers
|
|
168
|
+
await forceKillBrowserProcesses();
|
|
102
169
|
|
|
103
170
|
// 3. Start browser with CDP enabled
|
|
104
171
|
var cmdArgs = buildLaunchArgs();
|
|
@@ -116,6 +183,7 @@ async function launchBrowser() {
|
|
|
116
183
|
|
|
117
184
|
async function closeBrowser() {
|
|
118
185
|
try { execOhosAa(["force-stop", "--bundlename", BROWSER_PKG], 10000); } catch {}
|
|
186
|
+
await forceKillBrowserProcesses();
|
|
119
187
|
}
|
|
120
188
|
|
|
121
189
|
// ─── CDP endpoint discovery ──────────────────────────────────────────
|