Ui, main: a frame that fails no longer ends the process in std::terminate

The library throws at a Vulkan error, and a frame's — the device lost,
its memory exhausted — came out of the frame callback with nothing under
it to catch it: std::terminate. The instance was gone with its window,
and every later text imsd had stored waited there, not notified, until
somebody started the app again. A first frame that failed in the window
an Activate had asked for threw into the Activate handler instead and
left an instance with a window it never showed.

The frame path catches it now. The failure is logged and nothing counts
as on screen from there. From the frame callback the window closes as
at the compositor's word: an instance with a session bus runs on
without one, importing and notifying, and one without ends with exit 1.
At Show's first frame the window is taken down at once, and a start
left without a window ends with exit 1. A window asked for while a
failed one waits for its Hide is made after that Hide. The screenshot's
frame is a frame like the others, and a capture finds a window that
failed to be no window.

A failure named VK_ERROR_DEVICE_LOST — the library throws the failed
call's result by its name — means the device is lost. It stays lost,
and the app sets the device up once per process, so this process can
make no window again: the next request for one — at once when the
failure was a window's making or its first frame — ends the instance
in order with exit 1, and the start after it has a new device. That
covers a device lost while there was no window, where the window's
making fails and the instance used to stay, failing every later request
the same way. With the device not lost the next request makes a new
window.

The frame that fails is often the one a text asked for, and that text
was stored read, its conversation counting as on screen. A text made
read — by Sync at its import, by the app at Opened and Reload — is now
kept until the device has answered for the frame after it: the next
frame draws it, if its conversation is still the screen's, and the
library's Render begins by waiting for the frame that had its slot, the
frames in flight before it, so the app asks for that many more frames
and lets the text go when they are drawn. A window that fails before
then, or a device found lost at the next window's making, gives its
texts back: unread again, and notified through main's notifier, whose
bus main flushes before a start's early exit as before the loop's.

Only the frame path's errors are caught. One the library meets under its
own listeners — the swapchain it rebuilds at a resize — is under no
catch of the app's and still ends the process. And a resident instance
whose own start asks for the window (--new) and meets the lost device
there says it quits and runs on until the next request: that quit comes
before the main loop runs, and is lost.

Tested: on KWin, with a Vulkan layer of the bench's that answers
VK_ERROR_DEVICE_LOST from a chosen frame or moment on (the real device
is not lost: the waits a teardown makes fail too), this build beside the
one before. A conversation open and a text for it drawn, then the device
lost and another text for it: before, an abort with that text stored
read; now the first text stays read, the second is unread again and
notified, the next text is notified, an Activate ends the instance with
exit 1, and the start after it has its window. A text whose frame is
handed over and the device lost from the next frame on: unread again and
notified. A text drawn, its window closed before the frames that answer
for it, the device lost, and an Activate: unread again and notified,
exit 1. A plain start whose first frame fails, and a window without a
session bus: exit 1, before an abort. A resident instance whose first
frame fails at an Activate, and one whose device is lost while it has no
window: the Activate ends it with exit 1 and the start after it has its
window, where before the instance stayed and showed none. One frame
alone answering VK_ERROR_OUT_OF_DEVICE_MEMORY: the window goes, its text
is unread and notified, and the next Activate makes a new window, before
an abort; an Activate sent while that frame is still failing is served
after the Hide, and so is a link to another conversation. The
screenshot's frame meeting the lost device: no PNG, exit 1, and a text
waiting for its frames is unread again and notified, where before it
stayed read; a capture that comes due while a frame fails in its submit:
no window, exit 1, before an abort. A text made read as its window
became the active one, the store refusing that write's flush (the bench
fails its fdatasync), and the next frame on the lost device: unread
again and notified; the same text drawn first, its window left and
returned to, and then the loss: it stays read. And the limit: the device
lost and the window resized before a frame — an abort, as before.
This commit is contained in:
Jorijn van der Graaf 2026-11-17 06:26:37 +01:00
commit b8907867df
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 210 additions and 28 deletions

View file

@ -417,6 +417,7 @@ int main(int argc, char** argv) {
app->onQuit = [loop] { g_main_loop_quit(loop); };
app->onConversationOpened = [&notifier](const std::string& conversation) { notifier.Close(conversation); };
app->onConversationDeleted = [&notifier](const std::string& conversation) { notifier.Close(conversation); };
app->onUnseen = [&notifier](const Message& m) { notifier.Notify(m); };
app->onLog = Log;
app->verbose = o.headless; // a log line that names a correspondent is the smoke test's alone
// The compositor's connection broke, and the process cannot connect its
@ -458,10 +459,13 @@ int main(int argc, char** argv) {
// A start that asked for the window and has none (Ui's log said why)
// ends with exit 1, as one without its database does; a resident
// instance runs on without a window, and the next Activate or Open
// asks for one again (a device whose set-up failed fails it at once).
// asks for one again (a device whose set-up failed fails it at once;
// of a device that is lost the request ends the instance with exit 1,
// and the start after it has a new one).
if ((o.uri || !o.daemon) && !app->Visible()) {
client.Stop();
store->Flush();
if (session) g_dbus_connection_flush_sync(session, nullptr, nullptr); // what was asked of the bus on the way here — a notification for a text given back — goes out before the exit
g_main_loop_unref(loop);
return 1;
}

View file

@ -41,6 +41,7 @@ export namespace ImsdMessages::Ui {
// let go of the window, then the frame driver and the canvas go, and
// the display source last.
void Shutdown() {
showAfterHide = false;
Teardown();
source.reset();
}
@ -84,34 +85,41 @@ export namespace ImsdMessages::Ui {
hitKeys.clear();
hitMessages.clear();
textSinceFrame.clear();
frameFailed = false;
}
static gboolean OnHideIdle(gpointer data) {
App& self = *static_cast<App*>(data);
self.hideIdle = 0;
self.Hide();
if (std::exchange(self.showAfterHide, false)) self.Show(); // asked for while a failed window waited for this Hide
return G_SOURCE_REMOVE;
}
static gboolean OnScreenshot(gpointer data) {
App& self = *static_cast<App*>(data);
if (self.canvas) {
if (self.canvas && !self.frameFailed) {
try {
self.canvas->Window().CaptureNextFrame(self.screenshotPath);
self.canvas->Window().Update();
self.FrameDrawn();
self.Log(std::format("screenshot written to {} ({}x{} px)", self.screenshotPath.string(), self.canvas->Window().width, self.canvas->Window().height));
self.exitCode = 0; // a failure until written (main)
} catch (const std::exception& e) {
// The library throws when the capture fails (a Vulkan call,
// the PNG's write): the bench learns it from the log and the
// exit status.
// exit status. The capture's frame is a frame: one that
// threw is not known to have shown anything (FrameLost).
self.Log(std::format("screenshot not written to {}: {}", self.screenshotPath.string(), e.what()));
self.exitCode = 1;
self.FrameLost(e.what());
}
} else {
// No window to render it — none could be made, or it went
// between the request and the capture: logged, and the run
// ends with exit 1, so a bench learns it from both.
// No window to render it — none could be made, it went
// between the request and the capture, or it failed in a
// frame and waits for its Hide (another Update on it could
// wait for ever on a fence the failed frame reset): logged,
// and the run ends with exit 1, so a bench learns it from both.
self.Log(std::format("screenshot not written to {}: no window", self.screenshotPath.string()));
self.exitCode = 1;
}
@ -127,12 +135,13 @@ export namespace ImsdMessages::Ui {
bool closeQuits = false; // no session bus: a close ends the process, since nothing could show the window again
bool verbose = false; // --headless: log lines may name a correspondent
std::string appId = "net.catcrafts.ImsdMessages"; // the xdg app id; main sets it from the bus layer's constant
std::function<void()> onQuit; // the screenshot's run is over, or the window closed with no bus to come back on
std::function<void()> onQuit; // the screenshot's run is over, the window closed with no bus to come back on, or one was asked for of a lost device
std::function<void(const std::string&)> onConversationOpened; // the notification goes
std::function<void(const std::string&)> onConversationDeleted; // so does the notification
std::function<void(const Message&)> onUnseen; // a text made read that no frame is known to have shown (GiveBack): notified after all
std::function<void(std::string_view)> onLog;
std::function<void()> onDisplayLost; // the compositor's connection broke
std::int32_t exitCode = 0; // what main returns when nothing else decided: 1 for a screenshot not written
std::int32_t exitCode = 0; // what main returns when nothing else decided: 1 for a screenshot not written, and for an end over a window that failed or a device that is lost
bool Visible() const noexcept { return canvas.has_value(); }
@ -146,9 +155,11 @@ export namespace ImsdMessages::Ui {
}
// Cancels a close's deferred Hide: a Show or an Open before it runs
// keeps the window. True when there was one.
// keeps the window — not one that failed in a frame, whose Hide
// stands (Show serves the request after it). True when there was
// one.
bool KeepWindow() {
if (!hideIdle) return false;
if (!hideIdle || frameFailed) return false;
g_source_remove(hideIdle);
hideIdle = 0;
return true;
@ -159,8 +170,8 @@ export namespace ImsdMessages::Ui {
// window and has not suspended it (the library's Window::activated
// and suspended, xdg_toplevel's states). A window behind a lock
// screen or another application, minimised or with the display
// off, is up all the same.
bool OnScreen() const noexcept { return canvas.has_value() && hideIdle == 0 && canvas->Window().activated && !canvas->Window().suspended; }
// off, is up all the same; one that failed draws nothing more.
bool OnScreen() const noexcept { return canvas.has_value() && !frameFailed && hideIdle == 0 && canvas->Window().activated && !canvas->Window().suspended; }
// The compositor's word on the window changed. Out of sight, no
// conversation is on screen: a text for the open one is stored
@ -172,8 +183,22 @@ export namespace ImsdMessages::Ui {
else if (sync) sync->SetOpenConversation(std::nullopt);
}
// Creates the window; shows it again after a Hide.
// Creates the window; shows it again after a Hide. With the device
// lost (CheckDevice) there is none to make: the request ends the
// process instead (EndOverDevice).
void Show() {
if (deviceLost) {
EndOverDevice();
return;
}
if (canvas && frameFailed) {
// The window failed in a frame and goes with its Hide, which
// is not hurried from here: Show is reachable from that
// window's own listeners (a tap that opens). The request is
// served after the Hide (OnHideIdle).
showAfterHide = true;
return;
}
const bool kept = KeepWindow();
if (canvas) {
if (kept) Opened(); // a close taken back: the kept conversation is on screen again, if the window is seen
@ -190,10 +215,17 @@ export namespace ImsdMessages::Ui {
// once with the same reason when the device's set-up failed
// (it runs only once), and tries again past it (an asset
// missing): what the failed Create queued is drained here,
// as Teardown drains, since no frame will.
// as Teardown drains, since no frame will. A device that
// was set up and is lost since makes no window in this
// process again: the process ends (EndOverDevice).
canvas.reset();
Crafter::Device::DrainDeletions();
Log(std::format("no window: {}", e.what()));
CheckDevice(e.what());
if (deviceLost) {
GiveBack(); // what the window before this one drew last, the device may not have finished
EndOverDevice();
}
return;
}
canvas->theme = theme;
@ -205,7 +237,22 @@ export namespace ImsdMessages::Ui {
Log(std::format("vulkan: {}, descriptors by {}; window scale {}", Crafter::Device::deviceProperties.deviceName, Crafter::Device::descriptorHeapSupported ? "heap" : "sets", canvas->Window().scale));
}
frames.emplace(canvas->Window());
frames->onFrame = [this] { canvas->Window().Update(); };
frames->onFrame = [this] {
if (frameFailed) return; // until the close's Hide has taken it
try {
canvas->Window().Update();
FrameDrawn();
if (!readUnshown.empty()) frames->RequestRedraw(); // the frames that prove the one a text was drawn in
} catch (const std::exception& e) {
// Closed as at the compositor's word: the teardown is
// the loop's, since this runs inside the frame callback.
// Without a session bus the close ends the process, and
// not as a close the user asked for.
WindowFailed(e.what());
if (closeQuits) exitCode = 1;
Close();
}
};
day.emplace();
// The captions name the day ("Today", a clock time for today's
// text): drawn again when it changes.
@ -296,19 +343,113 @@ export namespace ImsdMessages::Ui {
Reload();
Opened(); // read once the new window counts as seen: now, or at the call that finds it so
if (fresh || (view && view->compose.focused)) ShowCaret(); // the blink a hide stopped
try {
w.Render();
FrameDrawn();
} catch (const std::exception& e) {
// The first frame failed as a later one can: no window,
// taken down here (nothing of the library's is on the
// stack). A start that asked for one ends with exit 1
// (main); a resident instance runs on, unless the device
// is lost.
WindowFailed(e.what());
Teardown();
if (deviceLost) EndOverDevice();
return;
}
frames->RequestRedraw();
}
// The window's close, the compositor's or the bench's (--hide-after,
// --hide-every). Closing hides: the process stays for the next text
// (the resident model); --quit or a signal ends it. The library runs
// onClose from inside its own listener, so the teardown is deferred
// to the loop; a Show before it runs keeps the window. The
// conversation is off the screen from here, not from the Hide: a
// text for it meanwhile is unread, and notified. Without a session
// bus nothing could show the window again, so there a close ends
// the process.
// A frame threw: the library does at a Vulkan error (the device
// lost, its memory exhausted). That costs the window and not the
// process, which would end in std::terminate with nothing to catch
// it under the frame callback: said once, nothing is on screen from
// here, and the window goes — an instance that can be asked for a
// window again runs on without one, importing and notifying. Only
// a frame's error comes by here: one the library meets under its
// own listeners (the swapchain it rebuilds at a resize) is under
// no catch of the app's.
void WindowFailed(std::string_view what) {
Log(std::format("the window failed: {}", what));
FrameLost(what);
}
// What a frame that threw leaves, the screenshot's too: nothing more
// is drawn on the window and nothing is on screen from here, the
// device may be lost, and the texts made read for a frame nobody is
// known to have seen are given back.
void FrameLost(std::string_view what) {
frameFailed = true;
CheckDevice(what);
if (sync) sync->SetOpenConversation(std::nullopt);
GiveBack();
}
// The texts made read that the device has not answered for
// (readUnshown; the frame that failed is often the one a text asked
// for): unread again, and notified, since nobody is known to have
// seen them. A read that throws leaves the rest as they are, and is
// said.
void GiveBack() {
std::uint32_t unseen = 0;
try {
for (const Unshown& u : readUnshown) {
std::optional<Message> m = store.Get(u.id);
if (!m || !m->read) continue; // deleted since, or not left read
store.SetRead(u.id, false); // in place: the row reads unread even when the store refuses the flush
m->read = false;
++unseen;
if (onUnseen) onUnseen(*m);
}
} catch (const std::exception& e) {
Log(std::format("the texts no frame showed were not all made unread: {}", e.what()));
}
readUnshown.clear();
if (unseen > 0) Log(std::format("{} text(s) no frame showed: unread again", unseen));
}
// A text was made read, its conversation counting as on screen: kept
// until the device has answered for the next frame (readUnshown).
void MadeRead(std::uint64_t id) { readUnshown.push_back({ id, framesDrawn + 1 + Canvas::framesInFlight }); }
// A frame was drawn without a throw. The library's Render begins by
// waiting for the frame that had its slot, the frames in flight
// before it, and went on to submit and present: the device's word
// that the earlier frame is finished.
void FrameDrawn() {
++framesDrawn;
std::erase_if(readUnshown, [this](const Unshown& u) { return u.shownBy <= framesDrawn; });
}
// Whether the device is lost, by the failure's own word: the library
// throws the failed call's result by its name
// (Device::CheckVkResult), and VK_ERROR_DEVICE_LOST is a lost
// device's. It stays lost, and the device is set up once per process
// (Canvas::Create): this process makes no window again.
void CheckDevice(std::string_view what) {
if (deviceLost || what != "VK_ERROR_DEVICE_LOST") return;
deviceLost = true;
Log("the Vulkan device is lost: this process can make no window again");
}
// A window was asked for and the device is lost: the process ends in
// order with exit 1, so that the next start has a new device.
void EndOverDevice() {
if (!endSaid) Log("a window was asked for and the device is lost: quitting, the next start has a new one");
endSaid = true;
exitCode = 1;
if (onQuit) onQuit();
}
// The window's close: the compositor's, the bench's (--hide-after,
// --hide-every), or the one a failed frame makes. Closing hides: the
// process stays for the next text (the resident model); --quit or a
// signal ends it. The library runs onClose from inside its own
// listener, so the teardown is deferred to the loop; a Show before
// it runs keeps the window. The conversation is off the screen from
// here, not from the Hide: a text for it meanwhile is unread, and
// notified. Without a session bus nothing could show the window
// again, so there a close ends the process.
void Close() {
if (!canvas) return;
if (closeQuits) {
@ -362,7 +503,7 @@ export namespace ImsdMessages::Ui {
view->messages = store.Conversation(key);
if (verbose) Log(std::format("opened {}", key));
else Log("opened a conversation");
if (canvas) Opened();
if (canvas && !frameFailed) Opened();
else Show();
ShowCaret();
}
@ -373,7 +514,7 @@ export namespace ImsdMessages::Ui {
// next Show calls again.
void Opened() {
if (!view || !OnScreen()) return;
if (store.MarkRead(view->key) > 0) Reload();
if (MarkOpenRead() > 0) Reload();
if (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key);
}
@ -548,6 +689,7 @@ export namespace ImsdMessages::Ui {
}
textSinceFrame.insert(m.conversation);
if (gestures.Pressed() && pressedOn && pressedOn->id == Hit::rowBase && pressedOn->key == m.conversation) pressGotText = true;
if (m.read) MadeRead(m.id); // stored read for the open conversation (Sync): not drawn yet
Reload();
}
@ -571,6 +713,23 @@ export namespace ImsdMessages::Ui {
if (view) drafts.Keep(view->key, view->compose, view->dialled, view->dialledPlan);
}
// Marks the open conversation's unread texts read, and keeps which
// they were until a frame has shown them (MadeRead): the ones the
// store has unread now. The view's copies are older, and a text
// made read since still reads unread in them.
std::uint32_t MarkOpenRead() {
std::vector<std::uint64_t> unread;
for (const Message& m : store.Conversation(view->key)) {
if (!m.read) unread.push_back(m.id);
}
const std::uint32_t marked = store.MarkRead(view->key);
// Kept whatever the count says: a MarkRead whose flush the store
// refused answers 0 with the rows read in place, and GiveBack
// passes over a text that was left unread.
for (const std::uint64_t id : unread) MadeRead(id);
return marked;
}
void Reload() {
convs = GroupConversations(store.All());
if (view) {
@ -580,7 +739,7 @@ export namespace ImsdMessages::Ui {
// Texts on screen are read; a conversation kept across a hide
// or in a window that is not seen is not on screen, and its
// new texts stay unread until it is.
if (OnScreen() && sync && store.MarkRead(view->key) > 0) convs = GroupConversations(store.All());
if (OnScreen() && sync && MarkOpenRead() > 0) convs = GroupConversations(store.All());
}
Dirty();
}
@ -1040,6 +1199,25 @@ export namespace ImsdMessages::Ui {
std::optional<Crafter::KeyCode> spentKey; // the key whose press changed the screen (Key)
guint blinkTimer = 0;
guint hideIdle = 0; // a close's Hide, deferred to the loop
bool frameFailed = false; // the window threw in a frame (WindowFailed): nothing more is drawn on it
bool deviceLost = false; // the device said so after a failure (CheckDevice): no window is made again
bool endSaid = false; // EndOverDevice's line, once
bool showAfterHide = false; // a window was asked for while a failed one waited for its Hide (Show)
// The texts made read — the open conversation counted as on screen
// — that the device has not answered for yet. The next frame draws
// such a text, if its conversation is still the screen's, and
// the frame that takes that frame's slot again, the frames in
// flight later, waits for it before it draws (the library's
// Render): the count of frames drawn that carries the answer is
// kept with each. A window that fails before then, or a device
// found lost at the next window's making, gives them back
// (GiveBack).
struct Unshown {
std::uint64_t id;
std::uint64_t shownBy;
};
std::vector<Unshown> readUnshown;
std::uint64_t framesDrawn = 0; // the frames drawn without a throw, over every window of the process
std::chrono::steady_clock::time_point lastFrame{};
bool overflowLogged = false;
FrameWait frameWait{ .patience = Canvas::framesInFlight + 1 }; // the frames owed to the atlas while glyphs wait for a cell