fix(gateway): fence the hosted preclaim await against FIFO advance (X4, reported by @Xipong)

_drain awaited check_remote_hosted_admission for head A, then claimed whichever
row was first in the FIFO. Cancelling A during that await let claim take B,
which then executed on A verdict without its own source-owner reauthorization.
Re-read the checked row after the await and restart the loop when it is no
longer queued, the same fence the native branch already has.
This commit is contained in:
teknium1
2026-09-12 16:56:32 -07:00
parent 51c06f5ecf
commit 8d6d486f4a
2 changed files with 45 additions and 0 deletions

View File

@@ -392,6 +392,11 @@ class SessionAuthority:
if service is None:
raise RuntimeStoreError('permission_denied')
await asyncio.to_thread(service.check_admission, ref, first)
# Cancellation may advance the FIFO while the source owner is awaited;
# the successor must earn its own reauthorization, not inherit this one.
current = get_session_admission(self.db, admission_id=first['admission_id'])
if current is None or current['status'] != 'queued':
continue
if 'local_automation_v1' in first['payload']:
from gateway.session_automation import check_local_automation
check_local_automation(self, ref, first)

View File

@@ -92,3 +92,43 @@ async def test_cancelling_the_head_of_a_paused_fifo_resumes_its_successor(tmp_pa
statuses = {r['request_id']: (r['status'], r['outcome'])
for r in list_session_admissions(db, session_id='s', pending_only=False)}
assert statuses == {'blocked': ('terminal', 'cancelled'), 'follower': ('terminal', 'completed')}
@pytest.mark.asyncio
async def test_hosted_head_cancelled_during_preclaim_does_not_let_successor_skip_its_check(tmp_path, monkeypatch):
import threading
from gateway import session_finite, session_hosted_transport, session_local_recovery
from gateway.config import Platform
db, authority = _authority(tmp_path, monkeypatch, platform=Platform.LOCAL)
monkeypatch.setattr(session_local_recovery, 'restore_local_session', lambda authority, sid: None)
validated, executed = [], []
head_checking, release_head = threading.Event(), threading.Event()
def check_remote_hosted_admission(authority, ref, row):
validated.append(row['request_id'])
if row['request_id'] == 'hosted:A':
head_checking.set()
release_head.wait(5)
return True
# B's own reauthorization is revoked; it must be asked, never inherit A's verdict.
raise RuntimeStoreError('permission_denied')
monkeypatch.setattr(session_hosted_transport, 'check_remote_hosted_admission', check_remote_hosted_admission)
async def execute(authority, ref, row):
executed.append(row['request_id'])
return 'done'
monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)
with db:
head = await _submit(authority, 'hosted:A')
await _submit(authority, 'hosted:B')
await asyncio.get_running_loop().run_in_executor(None, head_checking.wait, 5)
await authority.cancel_queued(ACTOR, REF, head.admission_id)
release_head.set()
await asyncio.wait_for(authority.sessions['s'].task, 5)
assert executed == [], 'the successor reached execution without its own hosted validation'
assert validated == ['hosted:A', 'hosted:B']
statuses = {r['request_id']: r['status'] for r in list_session_admissions(db, session_id='s', pending_only=False)}
assert statuses == {'hosted:A': 'terminal', 'hosted:B': 'queued'}