single-instance: Allow spawning child instance (same binary) from inside main app while keeping single-instance for desktop icon launch #16527
Describe the problemI'm using
Questions
|
Replies: 2 comments 3 replies
|
pub fn run() {
let is_child = std::env::args_os().any(|a| a == "--child-instance")
|| std::env::var_os("MY_APP_CHILD_INSTANCE").is_some();
let mut builder = tauri::Builder::default();
if !is_child {
builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
if let Some(w) = app.get_webview_window("main") {
let _ = w.set_focus();
}
}));
}
builder.run(tauri::generate_context!()).expect("error running tauri app");
}From the running main app, spawn the same binary with One platform detail to watch on Windows: two processes of the same Tauri app default to sharing the same WebView2 user-data folder under AI-assisted: verified against tauri-apps/plugins-workspace ( |
|
Hi! The conditional plugin registration approach suggested above seems reasonable for this use case. One additional consideration is that bypassing the single-instance lock doesn't automatically make the child processes fully independent. Depending on the application, multiple instances of the same executable may still share:
If the child instances are intended to run independently, I'd suggest assigning each child a unique instance identifier and using it to separate any resources that shouldn't be shared. It would also be useful to distinguish between normal application launches and intentional child launches early in the startup process, before initializing resources with side effects. This way, the single-instance behavior remains intact for normal desktop launches while child processes can have their own lifecycle and storage where needed. I haven't tested this approach across platforms, but it seems like an important consideration beyond skipping the instance lock. Hope this helps! |
tauri-plugin-single-instancedoes not have a built-in bypass flag, because plugin registration happens inside your own Rust entrypoint beforetauri::Builder::runstarts (plugins/single-instance/src/lib.rs:51-56,127-135). If you skip registeringtauri_plugin_single_instance::init(...)ontauri::Builderwhen a child flag or environment variable is present, the spawned child process never checks or acquires the named mutex (Windows), D-Bus name (Linux), or Unix socket (macOS), while normal launches from Explorer or the desktop icon still go through the single-instance lock and focus your main window: