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
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
"obsidian": "latest",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
},
"dependencies": {
"tree-kill": "^1.2.2"
}
}
4 changes: 4 additions & 0 deletions src/ProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class ProcessManager {
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
82 changes: 75 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class OpenCodePlugin extends Plugin {
private lastBaseUrl: string | null = null;
private contextEventRefs: EventRef[] = [];
private contextRefreshTimer: number | null = null;
private serverPid: number | null = null;

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

async onunload(): Promise<void> {
await this.stopServer();
// Sync cleanup using stored PID - processManager may already be destroyed
if (this.serverPid) {
this.killPidSync(this.serverPid);
this.serverPid = null;
}
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 (the 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
}

// Method 3: Kill any remaining processes with our port
try {
const port = this.settings?.port || 14096;
execSync(`wmic process where "CommandLine like '%opencode%serve%port ${port}%'" delete`, { stdio: "ignore" });
} catch {
// No matching processes or command failed
}
} 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 @@ -182,13 +241,15 @@ 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");
}
return success;
}

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

Expand Down Expand Up @@ -436,11 +497,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
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);
});
}
}
2 changes: 1 addition & 1 deletion tests/ProcessManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe("ProcessManager", () => {

expect(success).toBe(false);
expect(currentManager.getState()).toBe("error");
expect(currentManager.getLastError()).toContain("not found");
expect(currentManager.getLastError()).toMatch(/not found|exit code/);
});

test("handles double stop gracefully", async () => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"esModuleInterop": true,
"importHelpers": true,
"isolatedModules": true,
"strictNullChecks": true,
Expand Down