Application segfaults when exiting without first closing windows
Is your issue REALLY a bug?
- [x] My issue is indeed a bug!
- [x] I am not crazy! I will not fill out this form just to ask a question or request a feature. Pinky promise.
Is there an existing issue for this?
- [x] I have searched the existing issues.
Is this issue related to iced?
- [x] My hardware is compatible and my graphics drivers are up-to-date.
What happened?
When using the iced::exit() Task with an active window open, the application closes the open window, hangs briefly, then segfaults.
However, if window::close is called first such that there are no open windows, the application exits normally.
As a byproduct of this behaviour, closing the window through the desktop environment/window manager leads to a successful exit as there are no windows left open when the program exits.
What is the expected behavior?
The application should close all open windows (as it does) and exit successfully (without segfaulting).
Version
master
Operating System
Linux
Do you have any log output?
# Using `iced::exit()`
$ cargo run 2>/dev/null
[1] 832223 segmentation fault (core dumped) cargo run 2> /dev/null
# Using `iced::window::get_latest().and_then(iced::window::close).chain(iced::exit())`
$ cargo run 2>/dev/null
(no output)
Do you have an example for this? The exit example in the examples dir works fine. Currious where your problem arises from?
Hello! I've made a super simple program that reproduces the issue:
fn main() -> iced::Result {
iced::run(update, view)
}
fn view(_: &usize) -> iced::Element<Message> {
iced::Element::new(
iced::widget::button("Exit")
.on_press(Message::Exit)
)
}
fn update(_: &mut usize, msg: Message) -> iced::Task<Message> {
match msg {
Message::Exit => iced::exit(),
}
}
#[derive(Debug, Clone)]
enum Message {
Exit,
}
Command-line:
$ cargo run 2>/dev/null
[1] 401840 segmentation fault (core dumped) cargo run 2> /dev/null
The reason that the exit example has no issue is because it does use the window::get_latest().and_then(window::close) as I specified above. See below:
https://github.com/iced-rs/iced/blob/89f480bdae951a31944302777b759661ec3c3e83/examples/exit/src/main.rs#L23
I kinda feel like exit is only meant for daemons that have already closed all of their windows
While this makes sense, the exit task does close the windows anyway (whether intentionally or not). While I'm not sure as to the complexity of implementation, I think it would make sense to automatically close all the windows before actually exiting. Alternatively, it could be documented on the exit task that it is intended to be called only when no windows are open (e.g., a daemon).
I think it's a matter of documentation. Maybe you could ask Hector on the DC server?
I think it's fair to expect a library to not segfault, or if the API requires closing all windows first, then maybe the exit task should return an error if so.
Anyway, I would like to help with the core here, but @Marlstar your example works for me! 😆 Maybe this is related to winit for your specific system? What linux distro are you running? Is is x86?
Hmm, that is interesting. I am running 64 bit fedora 42 on Wayland/hyprland. Device is a Dell XPS 15 9510. Using nouveau drivers for NVIDIA gpu. Using the wild linker (but it also happened with mold iirc). Latest rust stable unknown-linux-gnu toolchain.
I can't think of any other key details off the top of my head but let me know if there are any more important ones.
I also went ahead and tried your example on latest master (commit 89f480b). It did not error for me in any way, exiting normally with a return code of 0.
64 bit Arch /w X.org (dwm), default mesa drivers and an AMD RX 7700 XT. mold linker, x86_64-unknown-linux-gnu target on Rust 1.89.0-nightly (414482f6a 2025-05-13)
Just tried using nightly, on same latest master commit. My nightly toolchain is currently 1.88.0-nightly (cannot update it right now due to network issues). Same issue.
I'm also getting a segfault on my machine when executing the iced::exit() task. I'm on an AARCH64 processor running WSL2.
Getting a segfault too when closing the window. With the button in the corner, with iced::exit() and with return window::get_latest().and_then(window::close);.
iced = { version = "0.13.1", features = ["tokio", "wgpu"] }
Gnome 48, wayland, nvidia proprietary drivers. Removing wgpu from the features did not change the behavior, still crashes on close with SIGSEGV.
fn update(_: &mut (), _: ()) -> Task<()> { Task::none() }
fn view(_: &()) -> Element<()> { row![].into() }
application("Iced Hello World", update, view).run()
and then just close the window.
Try master, I think I fixed this in https://github.com/iced-rs/iced/commit/355f0e09218a8c9c1bed08b04201d32030a4150c.