use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use remedy_ui_core::host::Waker;
pub(crate) struct Launch {
pub(crate) args: Vec<String>,
pub(crate) reply: Option<Sender<i32>>,
}
#[derive(Default)]
pub(crate) struct Inbox {
pub(crate) launches: Vec<Launch>,
pub(crate) waker: Option<Waker>,
}
pub(crate) type SharedInbox = Arc<Mutex<Inbox>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ReplyWait {
pub(crate) reply: Duration,
pub(crate) exit: Option<Duration>,
}
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Claimed {
First,
HandedOver(Option<i32>),
}
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown", not(feature = "web")), allow(dead_code))]
pub(crate) fn deliver(inbox: &SharedInbox, args: Vec<String>, reply: Option<Sender<i32>>) {
let waker = {
let mut i = inbox.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
i.launches.push(Launch { args, reply });
i.waker.clone()
};
if let Some(w) = waker {
w();
}
}
pub(crate) fn claim(
app: &str,
args: &[String],
wait: Option<ReplyWait>,
inbox: SharedInbox,
) -> Result<Claimed, String> {
#[cfg(windows)]
{
let wait =
wait.map(|w| remedy_ui_platform::bridge_windows::ReplyWait { reply: w.reply, exit: w.exit });
remedy_ui_platform::bridge_windows::claim_single_instance_replying(app, args, wait, move |a, r| {
deliver(&inbox, a, r);
})
.map(|c| match c {
remedy_ui_platform::bridge_windows::Claim::First => Claimed::First,
remedy_ui_platform::bridge_windows::Claim::HandedOver(code) => Claimed::HandedOver(code),
})
.map_err(|e| e.to_string())
}
#[cfg(unix)]
{
unix::claim(app, args, wait, inbox)
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
{
let _ = (app, args, wait, inbox);
Err("a browser page claims a single instance with the `web` feature".into())
}
#[cfg(not(any(windows, unix, all(target_arch = "wasm32", target_os = "unknown"))))]
{
let _ = (app, args, wait, inbox);
Err("single instance is not available on this platform yet".into())
}
}
#[cfg(any(unix, test))]
mod frame {
use super::ReplyWait;
pub(super) const MARK: u8 = 0x01;
pub(super) const FLAG_EXIT: u8 = 1;
pub(super) fn encode(args: &[String], wait: Option<ReplyWait>) -> Vec<u8> {
let payload = args.join("\0").into_bytes();
let Some(w) = wait else { return payload };
let mut out = vec![MARK, if w.exit.is_some() { FLAG_EXIT } else { 0 }];
out.extend_from_slice(&u32::try_from(payload.len()).unwrap_or(u32::MAX).to_le_bytes());
out.extend_from_slice(&payload);
out
}
pub(super) fn decode(bytes: &[u8]) -> Vec<String> {
let text = String::from_utf8_lossy(bytes);
if text.is_empty() { Vec::new() } else { text.split('\0').map(str::to_owned).collect() }
}
}
#[cfg(unix)]
mod unix {
use std::io::{Read, Write};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::sync::mpsc::channel;
use super::frame::{FLAG_EXIT, MARK, decode, encode};
use super::{Claimed, ReplyWait, SharedInbox, deliver};
fn socket(app: &str) -> PathBuf {
let id: String = app.chars().map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }).collect();
match std::env::var_os("XDG_RUNTIME_DIR").filter(|v| !v.is_empty()) {
Some(dir) => PathBuf::from(dir).join(format!("remedyui-{id}.sock")),
None => {
let user = std::env::var("USER").unwrap_or_default();
std::env::temp_dir().join(format!("remedyui-{id}-{user}.sock"))
}
}
}
fn send(
path: &PathBuf,
args: &[String],
wait: Option<ReplyWait>,
) -> std::io::Result<Result<Option<i32>, String>> {
let mut s = UnixStream::connect(path)?;
s.write_all(&encode(args, wait))?;
let Some(w) = wait else { return Ok(Ok(None)) };
Ok(await_reply(s, w).map(Some))
}
fn await_reply(mut s: UnixStream, w: ReplyWait) -> Result<i32, String> {
let timed_out = |e: &std::io::Error| {
matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut)
};
let _ = s.set_read_timeout(Some(w.reply).filter(|d| !d.is_zero()));
let mut code = [0u8; 4];
match s.read_exact(&mut code) {
Ok(()) => {}
Err(e) if timed_out(&e) => {
return Err(format!("the running instance did not reply within {:?}", w.reply));
}
Err(_) => return Err("the running instance ended without replying".into()),
}
if let Some(exit) = w.exit {
let _ = s.set_read_timeout(Some(exit).filter(|d| !d.is_zero()));
let mut rest = Vec::new();
if let Err(e) = s.read_to_end(&mut rest)
&& timed_out(&e)
{
return Err(format!("the running instance did not exit within {exit:?}"));
}
}
Ok(i32::from_le_bytes(code))
}
const RECEIVE_WAIT: std::time::Duration = std::time::Duration::from_secs(5);
fn receive(s: &mut UnixStream) -> Option<(Vec<String>, Option<bool>)> {
let mut first = [0u8; 1];
match s.read(&mut first) {
Ok(0) => return Some((Vec::new(), None)),
Ok(_) => {}
Err(_) => return None,
}
if first[0] != MARK {
let mut rest = first.to_vec();
s.read_to_end(&mut rest).ok()?;
return Some((decode(&rest), None));
}
let mut head = [0u8; 5];
s.read_exact(&mut head).ok()?;
let [flags, len @ ..] = head;
let len = u64::from(u32::from_le_bytes(len));
let mut payload = Vec::new();
if std::io::Read::take(&mut *s, len).read_to_end(&mut payload).ok()? as u64 != len {
return None;
}
Some((decode(&payload), Some(flags & FLAG_EXIT != 0)))
}
fn hand_to_winner(path: &PathBuf, args: &[String], wait: Option<ReplyWait>) -> Result<Claimed, String> {
let mut last = String::new();
for _ in 0..25 {
match send(path, args, wait) {
Ok(sent) => return sent.map(Claimed::HandedOver),
Err(e) => last = e.to_string(),
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(format!("the running instance does not listen: {last}"))
}
pub(super) fn claim(
app: &str,
args: &[String],
wait: Option<ReplyWait>,
inbox: SharedInbox,
) -> Result<Claimed, String> {
let path = socket(app);
if let Ok(sent) = send(&path, args, wait) {
return sent.map(Claimed::HandedOver);
}
let lock = std::fs::File::options()
.create(true)
.truncate(false)
.write(true)
.open(path.with_extension("lock"))
.ok();
let taken = lock.as_ref().map(std::fs::File::try_lock);
if matches!(taken, Some(Err(std::fs::TryLockError::WouldBlock))) {
return hand_to_winner(&path, args, wait);
}
let _ = std::fs::remove_file(&path);
let listener = match UnixListener::bind(&path) {
Ok(l) => l,
Err(_) => return hand_to_winner(&path, args, wait),
};
std::thread::Builder::new()
.name("remedy-ui single instance".into())
.spawn(move || {
let _lock = lock;
for stream in listener.incoming().flatten() {
let mut s = stream;
let _ = s.set_read_timeout(Some(RECEIVE_WAIT));
match receive(&mut s) {
Some((launch, None)) => deliver(&inbox, launch, None),
Some((launch, Some(hold))) => {
let (tx, rx) = channel::<i32>();
deliver(&inbox, launch, Some(tx));
let _ = std::thread::Builder::new()
.name("remedy-ui single instance reply".into())
.spawn(move || {
let Ok(code) = rx.recv() else { return };
if s.write_all(&code.to_le_bytes()).is_ok() && hold {
std::mem::forget(s);
}
});
}
None => {}
}
}
})
.map_err(|e| e.to_string())?;
Ok(Claimed::First)
}
}
#[derive(Debug)]
pub struct InstanceReply(ReplyKind);
#[derive(Debug)]
enum ReplyKind {
Now(i32),
Later(Arc<Mutex<LaterSlot>>),
}
#[derive(Debug, Default)]
struct LaterSlot {
code: Option<i32>,
to: Option<Sender<i32>>,
abandoned: bool,
}
impl InstanceReply {
pub fn code(code: i32) -> Self {
InstanceReply(ReplyKind::Now(code))
}
pub fn later() -> (InstanceReply, ReplyLater) {
let slot = Arc::new(Mutex::new(LaterSlot::default()));
(InstanceReply(ReplyKind::Later(slot.clone())), ReplyLater(Some(slot)))
}
pub(crate) fn deliver(self, to: Sender<i32>) {
match self.0 {
ReplyKind::Now(code) => {
let _ = to.send(code);
}
ReplyKind::Later(slot) => {
let mut s = slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
match s.code.take() {
Some(code) => {
let _ = to.send(code);
}
None if !s.abandoned => s.to = Some(to),
None => {}
}
}
}
}
}
impl From<()> for InstanceReply {
fn from((): ()) -> Self {
InstanceReply::code(0)
}
}
impl From<i32> for InstanceReply {
fn from(code: i32) -> Self {
InstanceReply::code(code)
}
}
#[derive(Debug)]
pub struct ReplyLater(Option<Arc<Mutex<LaterSlot>>>);
impl ReplyLater {
pub fn send(mut self, code: i32) {
if let Some(slot) = self.0.take() {
let mut s = slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
match s.to.take() {
Some(to) => {
let _ = to.send(code);
}
None => s.code = Some(code),
}
}
}
}
impl Drop for ReplyLater {
fn drop(&mut self) {
if let Some(slot) = self.0.take() {
let mut s = slot.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
s.abandoned = true;
s.to = None;
}
}
}
#[cfg(test)]
impl ReplyKind {
fn code_now(&self) -> Option<i32> {
match self {
ReplyKind::Now(c) => Some(*c),
ReplyKind::Later(_) => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_reply_reaches_the_launch_whichever_comes_first() {
let (tx, rx) = std::sync::mpsc::channel();
InstanceReply::code(3).deliver(tx);
assert_eq!(rx.try_recv(), Ok(3));
let (reply, later) = InstanceReply::later();
later.send(4);
let (tx, rx) = std::sync::mpsc::channel();
reply.deliver(tx);
assert_eq!(rx.try_recv(), Ok(4));
let (reply, later) = InstanceReply::later();
let (tx, rx) = std::sync::mpsc::channel();
reply.deliver(tx);
assert!(rx.try_recv().is_err(), "nothing yet");
std::thread::spawn(move || later.send(5)).join().ok();
assert_eq!(rx.try_recv(), Ok(5));
let (reply, later) = InstanceReply::later();
let (tx, rx) = std::sync::mpsc::channel();
reply.deliver(tx);
drop(later);
assert_eq!(rx.try_recv(), Err(std::sync::mpsc::TryRecvError::Disconnected));
assert_eq!(InstanceReply::from(()).0.code_now(), Some(0));
}
#[test]
fn framing_keeps_the_arguments() {
let wait = ReplyWait { reply: Duration::from_secs(1), exit: None };
let bytes = frame::encode(&["--stop".into(), "a b".into()], Some(wait));
assert_eq!(bytes.first(), Some(&frame::MARK));
assert_eq!(bytes.get(1), Some(&0));
assert_eq!(
frame::decode(bytes.get(6..).unwrap_or_default()),
vec!["--stop".to_owned(), "a b".to_owned()]
);
assert_eq!(frame::decode(&frame::encode(&[], None)), Vec::<String>::new());
}
#[cfg(unix)]
#[test]
fn a_launch_cannot_take_the_place_of_a_running_instance() {
let name = format!("remedy-ui-lock-test-{}", std::process::id());
assert_eq!(claim(&name, &[], None, SharedInbox::default()), Ok(Claimed::First));
let id: String = name.chars().map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }).collect();
let dir = std::env::var_os("XDG_RUNTIME_DIR").filter(|v| !v.is_empty());
let path = match dir {
Some(dir) => std::path::PathBuf::from(dir).join(format!("remedyui-{id}.sock")),
None => {
let user = std::env::var("USER").unwrap_or_default();
std::env::temp_dir().join(format!("remedyui-{id}-{user}.sock"))
}
};
assert!(std::fs::remove_file(&path).is_ok(), "the first instance bound {}", path.display());
let second = claim(&name, &[], None, SharedInbox::default());
assert!(second.is_err(), "a second first instance: {second:?}");
let _ = std::fs::remove_file(path.with_extension("lock"));
}
#[test]
#[ignore = "run as a child process by the test below"]
fn first_instance_child() {
let Ok(name) = std::env::var("REMEDY_UI_INSTANCE_TEST") else { return };
let inbox = SharedInbox::default();
assert_eq!(claim(&name, &[], None, inbox.clone()), Ok(Claimed::First));
println!("ready");
let started = std::time::Instant::now();
while started.elapsed() < Duration::from_secs(20) {
let launch = inbox.lock().ok().and_then(|mut i| i.launches.pop());
if let Some(l) = launch {
if let Some(to) = l.reply {
let code = if l.args == ["--stop"] { 7 } else { 1 };
let _ = to.send(code);
}
std::thread::sleep(Duration::from_millis(400));
return;
}
std::thread::sleep(Duration::from_millis(20));
}
}
#[test]
fn a_second_launch_gets_the_reply_and_waits_for_the_exit() {
use std::io::BufRead;
let name = format!("remedy-ui-reply-test-{}", std::process::id());
let Ok(exe) = std::env::current_exe() else { return };
let Ok(mut child) = std::process::Command::new(exe)
.args(["--ignored", "--exact", "instance::tests::first_instance_child", "--nocapture"])
.env("REMEDY_UI_INSTANCE_TEST", &name)
.stdout(std::process::Stdio::piped())
.spawn()
else {
unreachable!("the child starts")
};
let Some(out) = child.stdout.take() else { unreachable!("piped") };
let mut lines = std::io::BufReader::new(out).lines();
assert!(lines.any(|l| l.is_ok_and(|l| l.contains("ready"))), "the child claimed first");
let wait = ReplyWait { reply: Duration::from_secs(10), exit: Some(Duration::from_secs(10)) };
let got = claim(&name, &["--stop".into()], Some(wait), SharedInbox::default());
assert_eq!(got, Ok(Claimed::HandedOver(Some(7))));
let mut exited = false;
for _ in 0..50 {
if child.try_wait().ok().flatten().is_some() {
exited = true;
break;
}
std::thread::sleep(Duration::from_millis(20));
}
assert!(exited, "the first instance exited before the reply wait ended");
let _ = child.kill();
}
}