Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): give the tokio runtime and media threads 16 MiB stacks on every platform

Richie McIlroy9943457fcaf5
diff --git a/apps/desktop-gpui/src/camera_window.rs b/apps/desktop-gpui/src/camera_window.rs
index 73f41ae2a..060ecf0c5 100644
--- a/apps/desktop-gpui/src/camera_window.rs
+++ b/apps/desktop-gpui/src/camera_window.rs
@@ -1791,6 +1791,7 @@ impl CameraWindow {
let (out_tx, out_rx) = flume::bounded::<camera_blur::BlurOutput>(2);
if let Err(error) = std::thread::Builder::new()
.name("camera-blur".into())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
.spawn(move || camera_blur::run(job_rx, out_tx))
{
// job_rx is gone; the caller's try_send sees Disconnected and
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index c26bd1a10..a69618ab8 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -1638,6 +1638,7 @@ impl EditorWindow {
let (send, receive) = flume::bounded(1);
let worker = std::thread::Builder::new()
.name("cap-editor-import".to_string())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
.spawn(move || {
let result = PreparedMp4Import::prepare(
&target,
diff --git a/apps/desktop-gpui/src/feeds.rs b/apps/desktop-gpui/src/feeds.rs
index 8e0b8d9de..39acece9f 100644
--- a/apps/desktop-gpui/src/feeds.rs
+++ b/apps/desktop-gpui/src/feeds.rs
@@ -1273,6 +1273,7 @@ impl Feeds {
let blur = self.camera_preview_blur.clone();
if let Err(error) = std::thread::Builder::new()
.name("camera-preview".into())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
.spawn(move || {
run_camera_preview_worker(CameraPreviewWorkerConfig {
frames: frame_rx,
diff --git a/apps/desktop-gpui/src/import.rs b/apps/desktop-gpui/src/import.rs
index 7f83e04e5..6284eb48f 100644
--- a/apps/desktop-gpui/src/import.rs
+++ b/apps/desktop-gpui/src/import.rs
@@ -362,6 +362,7 @@ fn spawn_import(cx: &mut App, work: impl FnOnce(flume::Sender<ImportProgress>) +
let in_flight = InFlightImport::begin();
let worker = std::thread::Builder::new()
.name("cap-media-import".to_string())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
.spawn(move || {
let _in_flight = in_flight;
work(tx);
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index a29d75e3b..98a6b1b2b 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -220,11 +220,20 @@ fn create_log_appender(
}
}
-#[cfg(target_os = "windows")]
-fn windows_runtime() -> tokio::runtime::Runtime {
+/// Stack for every thread that renders, decodes, encodes or runs camera
+/// effects. Render and export futures are generic over this crate's closures,
+/// so they compile at this crate's dev opt-level and need far more stack than
+/// Rust's 2 MiB default: the export estimate overflowed a 2 MiB tokio worker
+/// on macOS (2026-10-10). Only address space is reserved; pages are committed
+/// as the thread actually uses them.
+pub(crate) const MEDIA_THREAD_STACK_SIZE: usize = 16 * 1024 * 1024;
+
+/// gpui_tokio's own runtime keeps tokio's 2 MiB worker stacks; this one
+/// matches the Tauri app's runtime on every platform.
+fn app_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
- .thread_stack_size(16 * 1024 * 1024)
+ .thread_stack_size(MEDIA_THREAD_STACK_SIZE)
.enable_all()
.build()
.expect("Failed to initialize Tokio")
@@ -272,16 +281,11 @@ fn main() {
crate::deeplink::submit_deep_link(&url);
}
});
- #[cfg(target_os = "windows")]
- let runtime = windows_runtime();
- #[cfg(target_os = "windows")]
+ let runtime = app_runtime();
let runtime_handle = runtime.handle().clone();
app.run(move |cx: &mut App| {
- #[cfg(target_os = "windows")]
gpui_tokio::init_from_handle(cx, runtime_handle);
- #[cfg(not(target_os = "windows"))]
- gpui_tokio::init(cx);
gpui_tokio::Tokio::spawn(cx, cap_utils::operation_diagnostics::run_checkpoints()).detach();
// The dock icon: an unbundled dev binary shows the generic terminal
// document without it. The bytes are the shipping app's icon.png.
@@ -711,12 +715,15 @@ fn main() {
cx.activate(true);
}
});
- #[cfg(target_os = "windows")]
runtime.shutdown_background();
}
-#[cfg(all(test, target_os = "windows"))]
+#[cfg(all(
+ test,
+ any(target_os = "windows", target_os = "macos", target_os = "linux")
+))]
mod runtime_tests {
+ #[cfg(target_os = "windows")]
fn current_stack_size() -> usize {
let (mut low, mut high) = (0, 0);
unsafe {
@@ -725,20 +732,41 @@ mod runtime_tests {
high.saturating_sub(low)
}
+ #[cfg(target_os = "macos")]
+ fn current_stack_size() -> usize {
+ unsafe { libc::pthread_get_stacksize_np(libc::pthread_self()) }
+ }
+
+ #[cfg(target_os = "linux")]
+ fn current_stack_size() -> usize {
+ unsafe {
+ let mut attr = std::mem::zeroed::<libc::pthread_attr_t>();
+ assert_eq!(libc::pthread_getattr_np(libc::pthread_self(), &mut attr), 0);
+ let mut address = std::ptr::null_mut();
+ let mut size = 0;
+ assert_eq!(
+ libc::pthread_attr_getstack(&attr, &mut address, &mut size),
+ 0
+ );
+ libc::pthread_attr_destroy(&mut attr);
+ size
+ }
+ }
+
#[test]
- fn media_workers_and_blocking_tasks_have_large_windows_stacks() {
- let runtime = super::windows_runtime();
+ fn media_workers_and_blocking_tasks_have_large_stacks() {
+ let runtime = super::app_runtime();
runtime.block_on(async {
let worker_size = tokio::spawn(async { current_stack_size() }).await.unwrap();
let blocking_size = tokio::task::spawn_blocking(current_stack_size)
.await
.unwrap();
assert!(
- worker_size >= 16 * 1024 * 1024,
+ worker_size >= super::MEDIA_THREAD_STACK_SIZE,
"worker stack: {worker_size}"
);
assert!(
- blocking_size >= 16 * 1024 * 1024,
+ blocking_size >= super::MEDIA_THREAD_STACK_SIZE,
"blocking stack: {blocking_size}"
);
});
diff --git a/apps/desktop-gpui/src/upload.rs b/apps/desktop-gpui/src/upload.rs
index ceb845ba3..714f0bd02 100644
--- a/apps/desktop-gpui/src/upload.rs
+++ b/apps/desktop-gpui/src/upload.rs
@@ -307,6 +307,7 @@ fn start_instant_upload_with_ownership(
bridge = Some(
std::thread::Builder::new()
.name("gpui-instant-segments".to_string())
+ .stack_size(crate::MEDIA_THREAD_STACK_SIZE)
.spawn(move || {
while !bridge_cancel.load(Ordering::Acquire) && !events_tx.is_disconnected() {
match segment_rx.recv_timeout(Duration::from_millis(20)) {