Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 9f3bc0e

Browse files
committed
Merge branch 'dev' of https://github.com/sst/opencode into dev
2 parents 6c1a1a7 + 4779d99 commit 9f3bc0e

2 files changed

Lines changed: 27 additions & 77 deletions

File tree

packages/tauri/src-tauri/src/lib.rs

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
use std::{
22
net::{SocketAddr, TcpListener},
3-
process::Command,
43
sync::{Arc, Mutex},
54
time::{Duration, Instant},
65
};
76
#[cfg(target_os = "macos")]
87
use tauri::TitleBarStyle;
9-
use tauri::{AppHandle, LogicalSize, Manager, Monitor, RunEvent, WebviewUrl, WebviewWindow};
10-
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogResult};
8+
use tauri::{AppHandle, LogicalSize, Manager, RunEvent, WebviewUrl, WebviewWindow};
119
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
1210
use tauri_plugin_shell::ShellExt;
1311
use tokio::net::TcpSocket;
1412

1513
#[derive(Clone)]
1614
struct ServerState(Arc<Mutex<Option<CommandChild>>>);
1715

16+
#[tauri::command]
17+
fn kill_sidecar(app: AppHandle) {
18+
let Some(server_state) = app.try_state::<ServerState>() else {
19+
println!("Server not running");
20+
return;
21+
};
22+
23+
let Some(server_state) = server_state
24+
.0
25+
.lock()
26+
.expect("Failed to acquire mutex lock")
27+
.take()
28+
else {
29+
println!("Server state missing");
30+
return;
31+
};
32+
33+
let _ = server_state.kill();
34+
35+
println!("Killed server");
36+
}
37+
1838
fn get_sidecar_port() -> u16 {
1939
option_env!("OPENCODE_PORT")
2040
.map(|s| s.to_string())
@@ -29,40 +49,6 @@ fn get_sidecar_port() -> u16 {
2949
})
3050
}
3151

32-
fn find_and_kill_process_on_port(port: u16) -> Result<(), Box<dyn std::error::Error>> {
33-
// Find all listeners on the specified port
34-
let listeners = listeners::get_processes_by_port(port)?;
35-
36-
if listeners.is_empty() {
37-
println!("No processes found listening on port {}", port);
38-
return Ok(());
39-
}
40-
41-
for listener in listeners {
42-
let pid = listener.pid;
43-
println!("Found process {} listening on port {}", pid, port);
44-
45-
// Kill the process using platform-appropriate command
46-
#[cfg(target_os = "windows")]
47-
{
48-
Command::new("taskkill")
49-
.args(["/F", "/PID", &pid.to_string()])
50-
.output()?;
51-
}
52-
53-
#[cfg(not(target_os = "windows"))]
54-
{
55-
Command::new("kill")
56-
.args(["-9", &pid.to_string()])
57-
.output()?;
58-
}
59-
60-
println!("Killed process {}", pid);
61-
}
62-
63-
Ok(())
64-
}
65-
6652
fn spawn_sidecar(app: &AppHandle, port: u16) -> CommandChild {
6753
let (mut rx, child) = app
6854
.shell()
@@ -116,6 +102,7 @@ pub fn run() {
116102
.plugin(tauri_plugin_shell::init())
117103
.plugin(tauri_plugin_process::init())
118104
.plugin(tauri_plugin_opener::init())
105+
.invoke_handler(tauri::generate_handler![kill_sidecar])
119106
.setup(move |app| {
120107
let app = app.handle().clone();
121108

@@ -124,28 +111,6 @@ pub fn run() {
124111

125112
let should_spawn_sidecar = !is_server_running(port).await;
126113

127-
// if server_running {
128-
// let res = app
129-
// .dialog()
130-
// .message(
131-
// "OpenCode Server is already running, would you like to restart it?",
132-
// )
133-
// .buttons(MessageDialogButtons::YesNo)
134-
// .blocking_show_with_result();
135-
136-
// match res {
137-
// MessageDialogResult::Yes => {
138-
// if let Err(e) = find_and_kill_process_on_port(port) {
139-
// eprintln!("Failed to kill process on port {}: {}", port, e);
140-
// }
141-
// true
142-
// }
143-
// _ => false,
144-
// }
145-
// } else {
146-
// true
147-
// };
148-
149114
let child = if should_spawn_sidecar {
150115
let child = spawn_sidecar(&app, port);
151116

@@ -218,24 +183,7 @@ pub fn run() {
218183
if let RunEvent::Exit = event {
219184
println!("Received Exit");
220185

221-
let Some(server_state) = app.try_state::<ServerState>() else {
222-
println!("Server not running");
223-
return;
224-
};
225-
226-
let Some(server_state) = server_state
227-
.0
228-
.lock()
229-
.expect("Failed to acquire mutex lock")
230-
.take()
231-
else {
232-
println!("Server state missing");
233-
return;
234-
};
235-
236-
let _ = server_state.kill();
237-
238-
println!("Killed server");
186+
kill_sidecar(app.clone());
239187
}
240188
});
241189
}

packages/tauri/src/updater.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { check, DownloadEvent } from "@tauri-apps/plugin-updater"
22
import { relaunch } from "@tauri-apps/plugin-process"
33
import { ask, message } from "@tauri-apps/plugin-dialog"
4+
import { invoke } from "@tauri-apps/api/core"
45

56
export const UPDATER_ENABLED = window.__OPENCODE__?.updaterEnabled ?? false
67

@@ -34,6 +35,7 @@ export async function runUpdater(onDownloadEvent?: (progress: DownloadEvent) =>
3435
return false
3536
}
3637

38+
await invoke("kill_sidecar")
3739
await relaunch()
3840

3941
return true

0 commit comments

Comments
 (0)