From 34090097a1db5078a63303793eb391f272440f1f Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Fri, 2 Oct 2026 13:43:51 -0400 Subject: [PATCH] [Fix] (Client): make a readback on a lost device a no-op instead of a Fatal that kills the client --- MobileGL/MG_Remote/Client/EmitTables.cpp | 15 ++++++++-- .../MG_Remote/Client/TextureReadbackEmit.inc | 2 +- MobileGL/MG_Test/Wire/ServerSpawnTest.cpp | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/MobileGL/MG_Remote/Client/EmitTables.cpp b/MobileGL/MG_Remote/Client/EmitTables.cpp index 9e5cff23..f392e78b 100644 --- a/MobileGL/MG_Remote/Client/EmitTables.cpp +++ b/MobileGL/MG_Remote/Client/EmitTables.cpp @@ -900,7 +900,15 @@ namespace MobileGL::MG_Remote::Client { // The same predicate at the call site, with the named Fatal each failure mode owns. // status > expected cannot reach here: EmitAndWait already aborts Fatal{ReplyTooLarge} on // an oversize reply, so the only failures left are a wrong status or a SHORT one. - void RequireReadbackReplyComplete(Int32 status, Uint64 replySize, Uint64 expected) { + // + // A LOST DEVICE IS NOT ONE OF THOSE FAILURES. Once the session is device-lost every verb + // is declined by EmitAndWait itself, and the GL answer for a lost context is that its + // commands do nothing: the read writes no pixel and the application learns of the loss + // from glGetGraphicsResetStatus. Dying here instead turned a server-side GPU fault into a + // crash of the client (Chrome's in-process GPU thread took the browser with it). False: + // the caller leaves the destination untouched and returns. + Bool RequireReadbackReplyComplete(Int32 status, Uint64 replySize, Uint64 expected) { + if (status != Wire::ReplySink::kStatusOk && ClientSession::DeviceLost()) return false; if (status == Wire::ReplySink::kStatusError) { SessionFail(MGFatalFamily::ReplyError, "MGPipe: Fatal{ReplyError, \"ReadPixels\"} - the readback answered ERROR; " "the destination is left untouched rather than filled with stale bytes"); @@ -922,6 +930,7 @@ namespace MobileGL::MG_Remote::Client { static_cast(replySize), static_cast(expected)); } + return true; } #include "TextureReadbackEmit.inc" @@ -1068,7 +1077,7 @@ namespace MobileGL::MG_Remote::Client { // M2 / codex 11: an OK reply that arrived short, or a DECLINE/ERROR, must not // be handed back as pixels. The Fatal aborts before the application reads the // buffer, so the bytes EmitAndWait already copied are never observed. - RequireReadbackReplyComplete(status, replySize, bandBytes); + if (!RequireReadbackReplyComplete(status, replySize, bandBytes)) return; ApplyReadbackByteSwap(at, bandBytes, type, pack); return; } @@ -1078,7 +1087,7 @@ namespace MobileGL::MG_Remote::Client { 0, bounce.data(), bandBytes, &status, &replySize); // BEFORE THE SCATTER, so a short or non-OK reply never reaches the application's // pointer at all (the bounce is the only thing that held the partial bytes). - RequireReadbackReplyComplete(status, replySize, bandBytes); + if (!RequireReadbackReplyComplete(status, replySize, bandBytes)) return; ApplyReadbackByteSwap(bounce.data(), bandBytes, type, pack); if (pbo) { // pixels is an offset, never a host pointer. Upload only the requested diff --git a/MobileGL/MG_Remote/Client/TextureReadbackEmit.inc b/MobileGL/MG_Remote/Client/TextureReadbackEmit.inc index 05d0dab9..9bd06e55 100644 --- a/MobileGL/MG_Remote/Client/TextureReadbackEmit.inc +++ b/MobileGL/MG_Remote/Client/TextureReadbackEmit.inc @@ -140,7 +140,7 @@ Uint64 received = 0; session.EmitAndWait(MG_Pipe::MGPWireOp::GetTextureImage, &info, sizeof(info), nullptr, 0, bytes.data() + offset, info.DstSize, &status, &received); - RequireReadbackReplyComplete(status, received, info.DstSize); + if (!RequireReadbackReplyComplete(status, received, info.DstSize)) return; offset += info.DstSize; } ApplyReadbackByteSwap(bytes.data(), tight, type, pack); diff --git a/MobileGL/MG_Test/Wire/ServerSpawnTest.cpp b/MobileGL/MG_Test/Wire/ServerSpawnTest.cpp index 9a0ce13d..8bad1872 100644 --- a/MobileGL/MG_Test/Wire/ServerSpawnTest.cpp +++ b/MobileGL/MG_Test/Wire/ServerSpawnTest.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -1234,6 +1235,34 @@ TEST(ServerSpawnTest, AKilledServerLatchesDeviceLostFromTheHangupAndNotFromADead (void)Server::ReapServer(session.server, 5000, &exitCode); } +// A LOST DEVICE MAKES A READBACK A NO-OP, NOT A CRASH. Once the session is device-lost every verb +// is declined, and the read used to treat that DECLINE as Fatal{ReadbackDeclined} - so the first +// glReadPixels after a server-side GPU fault aborted the client (Chrome's in-process GPU thread +// took the whole browser with it). GL's answer for a lost context is that the read writes nothing +// and glGetGraphicsResetStatus reports the reset. Red once by dropping the device-lost arm of +// RequireReadbackReplyComplete: this case dies of SIGABRT. +TEST(ServerSpawnTest, AReadbackOnALostDeviceWritesNothingAndDoesNotAbort) { + Session session; + ASSERT_TRUE(Bring("lostread", &session)); + ASSERT_EQ(Handshake(session), MOBILEGL_OK); + Transport::Doorbell* bell = Client::ClientSessionInstance().SelfDoorbellForTest(); + ASSERT_NE(bell, nullptr); + ASSERT_EQ(::kill(session.server.pid, SIGKILL), 0); + for (int attempt = 0; attempt < 200 && !bell->PeerHungUp(); ++attempt) (void)bell->Park(10); + ASSERT_TRUE(bell->PeerHungUp()); + Client::ClientSessionInstance().LatchDeviceLost("the test killed the server"); + ASSERT_TRUE(Client::ClientSession::DeviceLost()); + + std::uint8_t pixels[4] = {0x5A, 0x5A, 0x5A, 0x5A}; + Client::RemoteEmitTable().GL.ReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + EXPECT_EQ(pixels[0], 0x5A); + EXPECT_EQ(pixels[3], 0x5A) << "a lost context's read wrote into the destination"; + + Client::ClientSessionInstance().Stop(); + int exitCode = -1; + (void)Server::ReapServer(session.server, 5000, &exitCode); +} + TEST(ServerSpawnTest, AnOrderlyStopIsNotADeviceLoss) { // The session-level control: a clean shutdown must leave the latch disarmed, or every exit // would report a context reset and the status would mean nothing.