Skip to content
Closed
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
76 changes: 69 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class OpenCodePlugin extends Plugin {
private viewManager: ViewManager;
private cachedIframeUrl: string | null = null;
private lastBaseUrl: string | null = null;
private serverPid: number | null = null;

async onload(): Promise<void> {
console.log("Loading OpenCode plugin");
Expand Down Expand Up @@ -149,10 +150,62 @@ export default class OpenCodePlugin extends Plugin {

async onunload(): Promise<void> {
this.contextManager.destroy();
await this.stopServer();
// Use sync cleanup with stored PID - processManager may already be destroyed
if (this.serverPid) {
this.killPidSync(this.serverPid);
this.serverPid = null;
} else {
await this.stopServer();
}
this.app.workspace.detachLeavesOfType(OPENCODE_VIEW_TYPE);
}

private killPidSync(pid: number): void {
try {
if (process.platform === "win32") {
const { execSync } = require("child_process");

// Method 1: Kill child processes (actual node.exe) using PowerShell
try {
const output = execSync(
`powershell -Command "Get-CimInstance Win32_Process -Filter \\"ParentProcessId=${pid}\\" | Select-Object ProcessId"`,
{ encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }
);

const lines = output.split("\n").slice(3);
for (const line of lines) {
const childPid = line.trim();
if (childPid && !isNaN(parseInt(childPid))) {
try {
execSync(`taskkill /F /PID ${childPid}`, { stdio: "ignore" });
} catch {
// Child may already be gone
}
}
}
} catch {
// PowerShell lookup failed, continue to other methods
}

// Method 2: Kill the parent process (cmd.exe)
try {
execSync(`taskkill /F /PID ${pid}`, { stdio: "ignore" });
} catch {
// Parent may already be gone
}
} else {
// Unix: kill the process group
try {
process.kill(-pid, "SIGTERM");
} catch {
process.kill(pid, "SIGTERM");
}
}
} catch {
// Process may already be gone
}
}

async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
Expand Down Expand Up @@ -194,6 +247,7 @@ export default class OpenCodePlugin extends Plugin {
async startServer(): Promise<boolean> {
const success = await this.processManager.start();
if (success) {
this.serverPid = this.processManager.getPid();
new Notice("OpenCode server started");
} else {
const error = this.processManager.getLastError();
Expand All @@ -208,6 +262,7 @@ export default class OpenCodePlugin extends Plugin {

async stopServer(): Promise<void> {
await this.processManager.stop();
this.serverPid = null;
new Notice("OpenCode server stopped");
}

Expand Down Expand Up @@ -287,11 +342,18 @@ export default class OpenCodePlugin extends Plugin {
}

private registerCleanupHandlers(): void {
this.registerEvent(
this.app.workspace.on("quit", () => {
console.log("[OpenCode] Obsidian quitting - performing sync cleanup");
this.stopServer();
})
);
// Hook into window close event for cleanup (more reliable than onunload on Windows)
const cleanupHandler = () => {
if (this.serverPid) {
this.killPidSync(this.serverPid);
this.serverPid = null;
}
};
window.addEventListener("beforeunload", cleanupHandler);

// Register for cleanup when plugin unloads
this.register(() => {
window.removeEventListener("beforeunload", cleanupHandler);
});
}
}
4 changes: 4 additions & 0 deletions src/server/ServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class ServerManager extends EventEmitter {
return this.lastError;
}

getPid(): number | null {
return this.process?.pid ?? null;
}

getUrl(): string {
const encodedPath = btoa(this.projectDirectory);
return `http://${this.settings.hostname}:${this.settings.port}/${encodedPath}`;
Expand Down
33 changes: 31 additions & 2 deletions src/server/process/WindowsProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,37 @@ export class WindowsProcess implements OpenCodeProcess {

console.log("[OpenCode] Stopping server process tree, PID:", pid);

// Use taskkill with /T flag to kill process tree
await this.execAsync(`taskkill /T /F /PID ${pid}`);
// Method 1: Find and kill child processes (actual node.exe) using PowerShell
// This is necessary because shell: true spawns cmd.exe -> node.exe, and
// killing cmd.exe leaves node.exe orphaned
try {
const { execSync } = require("child_process");
const output = execSync(
`powershell -Command "Get-CimInstance Win32_Process -Filter \\"ParentProcessId=${pid}\\" | Select-Object ProcessId"`,
{ encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }
);

const lines = output.split("\n").slice(3); // Skip headers
for (const line of lines) {
const childPid = line.trim();
if (childPid && !isNaN(parseInt(childPid))) {
try {
execSync(`taskkill /F /PID ${childPid}`, { stdio: "ignore" });
} catch {
// Child may already be gone
}
}
}
} catch {
// PowerShell lookup failed, continue to other methods
}

// Method 2: Kill the parent process (cmd.exe)
try {
await this.execAsync(`taskkill /F /PID ${pid}`);
} catch {
// Parent may already be gone
}

// Wait for process to exit
await this.waitForExit(process, 5000);
Expand Down
Loading