Some context behind this change: I’ve been meaning to look into this because I kept running into this crash when working on [other](https://github.com/zed-industries/zed/issues/61040) fixes. Everything worked fine in dev builds, right up until you tried to create a new Agent Thread using an ACP adapter. The crash reports in this instance were throwing me off: I was seeing different things each time. So, I had Fable look into this. It did so by binary-searching the minimum thread stack on which a probe replicating Zed’s exact handler chain can dispatch one message. Full methodology, probe source, and raw numbers can be found in [this gist](https://gist.github.com/yeskunall/2ac2b00f51389d9388d607974f6f8a04). The results of the probe are as follows: | SDK | Minimum stack (dev profile) | vs. 512 KiB GCD budget | |---|---|---| | 1.3.0 | 409,600–413,696 B | fit, ~100 KiB headroom | | 2.0.0 | 507,904–512,000 B | entire budget before runtime overhead | The oversized frames are monomorphized into `agent_servers`, not the SDK crate -- a `[profile.dev.package]` opt-level override on `agent-client-protocol` does **not** fix this (see gist), and optimized builds collapse the frames entirely, which is why only dev builds crashed. It found that the real signature is `fault_address == stack_pointer` on a `com.apple.root.default-qos` thread inside the ACP dispatch specialization, which I then had it verify across six local `.ips` reports. It seems in #61570, we pushed the dispatch chain past the GCD budget, explained further below: `AcpConnection::stdio` polled the ACP client connection future via `background_spawn`, which on macOS executes runnables on [GCD’s](https://developer.apple.com/documentation/DISPATCH) global-queue workers. Those threads have kernel-fixed, unconfigurable [512 KiB stacks](https://github.com/apple-oss-distributions/libpthread/blob/42d026df5b07825070f60134b980a1ec2552dfee/kern/kern_internal.h#L154). In unoptimized builds, the SDK’s chained-handler dispatch needs **~0.5 MiB of stack per inbound message** (again, see linked gist), so the first message overflows the guard page and takes the process down. Therefore, this 512 KiB constraint is **macOS-only**. Linux doesn’t use GCD -- GPUI [spawns its own `std::thread` workers](https://github.com/zed-industries/zed/blob/82878540b5410b288a2c92cb9ee5675533e4d807/crates/gpui_linux/src/linux/dispatcher.rs#L39) ([2 MiB Rust default](https://github.com/rust-lang/rust/blob/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/sys/thread/unix.rs#L26)). Windows uses [the OS thread pool](https://github.com/zed-industries/zed/blob/82878540b5410b288a2c92cb9ee5675533e4d807/crates/gpui_windows/src/dispatcher.rs#L68), which [inherits the executable’s stack reserve](https://github.com/MicrosoftDocs/sdk-api/blob/4502fff176b3b56beddb6a63c9f980377b11ba9b/sdk-api-src/content/threadpoolapiset/nf-threadpoolapiset-setthreadpoolstackinformation.md?plain=1#L58) -- [1 MB linker default](https://github.com/MicrosoftDocs/win32/blob/2eb6588c6703d31599285bbb06563c3d41b57590/desktop-src/ProcThread/thread-stack-size.md?plain=1#L17), but Zed already bumps it to 8 MiB in [crates/zed/build.rs:88](https://github.com/zed-industries/zed/blob/82878540b5410b288a2c92cb9ee5675533e4d807/crates/zed/build.rs#L88) (see [TODO comment](https://github.com/zed-industries/zed/blob/82878540b5410b288a2c92cb9ee5675533e4d807/crates/zed/build.rs#L87)). --- Release Notes: - N/A
Welcome to GPUI!
GPUI is a hybrid immediate and retained mode, GPU accelerated, UI framework for Rust, designed to support a wide variety of applications.
Getting Started
GPUI is still in active development as we work on the Zed code editor, and is still pre-1.0. There will often be breaking changes between versions. You'll also need to use the latest version of stable Rust. Add gpui, and optionally gpui_platform, to your Cargo.toml:
gpui = { version = "*" }
gpui_platform = { version = "*", features = ["font-kit", "wayland", "x11"] }
Everything in a standalone GPUI app starts with an Application. You can create one with gpui_platform::application(), which picks the windowing and text backends for the host OS, and kick off your application by passing a callback to Application::run(). Inside this callback, you can create a new window with App::open_window() and register your first root view.
use gpui::*;
fn main() {
gpui_platform::application().run(|cx: &mut App| {
// ..
});
}
gpui_platform
The features on gpui_platform are platform-specific, so the list above is a safe cross-platform default. If you build for a single platform, you can trim it:
-
macOS — Rendering uses Metal and is always available, but glyph rasterization needs
font-kit. Without it, GPUI falls back to a placeholder text system that lays text out but renders no glyphs.gpui_platform = { version = "*", features = ["font-kit"] } -
Linux / FreeBSD — enable at least one windowing backend for desktop windows:
wayland,x11, or both. These features also compile the renderer and text system, so no separate text feature is needed.gpui_platform = { version = "*", features = ["wayland", "x11"] } -
Windows — no features are required. Windowing uses Win32 and text uses DirectWrite.
font-kithas no effect here.
Additional Topics
Dependencies
GPUI has various system dependencies that it needs in order to work.
macOS
On macOS, GPUI uses Metal for rendering. In order to use Metal, you need to do the following:
- Install Xcode from the macOS App Store, or from the Apple Developer website. Note this requires a developer account.
Ensure you launch Xcode after installing, and install the macOS components, which is the default option.
-
Install Xcode command line tools
xcode-select --install -
Ensure that the Xcode command line tools are using your newly installed copy of Xcode:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
The Big Picture
GPUI offers three different registers depending on your needs:
-
State management and communication with
Entity's. Whenever you need to store application state that communicates between different parts of your application, you'll want to use GPUI's entities. Entities are owned by GPUI and are only accessible through an owned smart pointer similar to anRc. See theapp::contextmodule for more information. -
High level, declarative UI with views. All UI in GPUI starts with a view. A view is simply an
Entitythat can be rendered, by implementing theRendertrait. At the start of each frame, GPUI will call this render method on the root view of a given window. Views build a tree ofelements, lay them out and style them with a tailwind-style API, and then give them to GPUI to turn into pixels. See thedivelement for an all purpose swiss-army knife of rendering. -
Low level, imperative UI with Elements. Elements are the building blocks of UI in GPUI, and they provide a nice wrapper around an imperative API that provides as much flexibility and control as you need. Elements have total control over how they and their child elements are rendered and can be used for making efficient views into large lists, implement custom layouting for a code editor, and anything else you can think of. See the
elementmodule for more information.
Each of these registers has one or more corresponding contexts that can be accessed from all GPUI services. This context is your main interface to GPUI, and is used extensively throughout the framework.
Other Resources
In addition to the systems above, GPUI provides a range of smaller services that are useful for building complex applications:
-
Actions are user-defined structs that are used for converting keystrokes into logical operations in your UI. Use this for implementing keyboard shortcuts, such as cmd-q. See the
actionmodule for more information. -
Platform services, such as
quit the apporopen a URLare available as methods on theapp::App. -
An async executor that is integrated with the platform's event loop. See the
executormodule for more information., -
The
[gpui::test]macro provides a convenient way to write tests for your GPUI applications. Tests also have their own kind of context, aTestAppContextwhich provides ways of simulating common platform input. Seeapp::test_contextandtestmodules for more details.
Currently, the best way to learn about these APIs is to read the Zed source code or drop a question in the Zed Discord. We're working on improving the documentation, creating more examples, and will be publishing more guides to GPUI on our blog.