[MG_Remote, MG_Test, scripts] (P7): DrainRing checks the session latch immediately before every pop, so a latch the control thread stores between two records stops the next one

This commit is contained in:
2026-09-23 17:04:13 -04:00
parent 1052b13a1a
commit 03bcf5e7fb
6 changed files with 145 additions and 36 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ namespace MobileGL::MG_Remote {
// SessionFail calls: the string must carry its `Fatal{Word` and the word must have a .def row.
void ArmSessionLatch();
bool SessionLatchArmed();
// True once the first fault has latched. One acquire load: DrainRing asks it per record.
// True once the first fault has latched. One acquire load: DrainRing asks it before every pop.
bool SessionLatched();
// The first latched fault - its family and its line, verbatim. Meaningful only once
// SessionLatched() is true; the line's storage lives for the process.
+34 -17
View File
@@ -657,25 +657,37 @@ namespace MobileGL::MG_Remote::Server {
// allocation; `applied == 0` then skips the retire block below entirely. Keep it that
// way: anything added here runs ~900 times a frame doing nothing.
//
// PH-1 (3), ID-P7-1: THE LATCH CHECK, TWICE, AND EACH ONE HAS A JOB THE OTHER CANNOT DO.
// Once a named fault has latched (an armed spawn / TCP session child only - unarmed this is
// one acquire load of a flag nobody sets), no further record is applied: the rest of the
// ring is declined unread, the apply thread leaves its loop, and RunSession closes the
// session by name.
// * HERE, at the top: a drain ENTERED latched pops nothing. That is the apply thread's
// exit-path drain after it left on the latch, and any drain after RunSession's thread
// latched a control op. ServerLoopTest's latched-batch case goes red without it (the
// exit-path drain applies the record behind the latched one).
// * AFTER EACH RECORD, at the bottom of the loop: a record that latched is the last one
// this drain pops, even when the peer published more behind it in the same batch
// (PeerLatchTest's two-record case and ServerLoopTest's latched-batch case).
// A check at the loop's head instead would only repeat this one on the first iteration.
if (SessionLatched()) return 0;
// PH-1 (3), ID-P7-1: THE LATCH CHECK, ONCE, IMMEDIATELY BEFORE EVERY POP. Once a named
// fault has latched (an armed spawn / TCP session child only - unarmed this is one acquire
// load of a flag nobody sets), no further record is applied: the rest of the ring is
// declined unread, the apply thread leaves its loop, and RunSession closes the session by
// name. The one check does three jobs, and each has a case that goes red without it:
// * a drain ENTERED latched pops nothing - the apply thread's exit-path drain after it
// left on the latch, and any drain after RunSession's thread latched a control op
// (ServerLoopTest's latched-batch case: the exit-path drain applies the record behind
// the latched one);
// * a record that latched is the last one this drain pops, even when the peer published
// more behind it in the same batch (PeerLatchTest's two-record case, ServerLoopTest's
// latched-batch case);
// * a latch ANOTHER thread stores between two records - RunSession's control thread
// latching a malformed SurfaceOp while this drain is mid-batch - stops the next pop
// (codex closeout finding 6; ServerLoopLatchTest's between-records case). The first
// version checked at the top of the function and after each record instead, and a
// latch that landed after the per-record check was still followed by one more pop and
// apply before anything looked again.
// What remains is the window between this load and the pop itself, and it is not a gap
// in the guarantee but its linearization point: a latch stored after this load is ordered
// after the record the load admitted, exactly as a latch stored while that record runs is.
// Closing it would take a lock the control thread shares with every pop.
//
// The idle poll's cost is unchanged: this load replaces the function-top check the first
// version made, and the empty-ring answer is still it plus Pop's cmdHead load.
ServerSession& session = *m_session;
Transport::SessionConsumer& consumer = session.Consumer();
PipeApplier& applier = session.Applier();
Uint64 applied = 0;
for (;;) {
if (SessionLatched()) break;
bool corrupt = false;
const bool popped = consumer.ApplyOne(
[this, &applier](const Transport::RingRecordView& record) {
@@ -708,12 +720,17 @@ namespace MobileGL::MG_Remote::Server {
}
if (!popped) break;
++applied;
if (SessionLatched()) break; // PH-1 (3): the per-record check the top of this function describes
// PH-6: the record whose event forfeited the reverse channel is the last one this
// session applies. Checked only AFTER a popped record, so the empty-ring answer above
// still costs its two loads and nothing else. (The latch check sits directly under
// `++applied` because ph_latch_sites.py's MECHANICS row pins exactly that shape.)
// still costs its two loads and nothing else. (A record that LATCHED is stopped by the
// loop-head check on the next iteration - ph_latch_sites.py's MECHANICS row pins that
// check as the first statement of this loop.)
if (session.ReverseChannelForfeited()) break;
// TEST-ONLY scheduling point BETWEEN two records: after this record's own checks and
// before the next pop's latch check, which is where a latch from RunSession's control
// thread used to slip one more record through. Only after a popped record, so the idle
// poll never pays for it; null outside ServerLoopLatchTest.
if (const auto hook = m_betweenRecordsHook.load(std::memory_order_acquire)) hook();
}
if (applied != 0) {
// THE TWO TALLIES MUST AGREE, AND THAT IS WHAT MAKES R-9's BATCHING BAN CHECKABLE.
+8
View File
@@ -310,6 +310,13 @@ namespace MobileGL::MG_Remote::Server {
void SetBeforeRetireHookForTesting(void (*hook)()) {
m_beforeRetireHook.store(hook, std::memory_order_release);
}
// Scheduling point only, same discipline: DrainRing runs the hook on the apply thread
// BETWEEN two records - after a popped record's own checks, before the next pop's latch
// check - and never on an empty-ring poll. ServerLoopLatchTest latches from a second
// thread there, which is the interleaving codex closeout finding 6 named.
void SetBetweenRecordsHookForTesting(void (*hook)()) {
m_betweenRecordsHook.store(hook, std::memory_order_release);
}
// C7 / ID-54 diagnostics, read by ServerLoopTest's C7 and N-3 controls. NativeBindCount is
// how many times ApplyMakeCurrent FORWARDED a bind to the backend (a tuple it did not
@@ -459,6 +466,7 @@ namespace MobileGL::MG_Remote::Server {
// that reached the blocking Park. Relaxed everywhere: it is a gauge.
std::atomic<Uint64> m_parkBlocks{0};
std::atomic<void (*)()> m_beforeRetireHook{nullptr};
std::atomic<void (*)()> m_betweenRecordsHook{nullptr};
// C7 / ID-54: the (dpy, draw, read, ctx) currently bound on the apply thread. Written and
// read ONLY on the apply thread inside ApplyMakeCurrent, so it needs no lock; the two