Skip to content

fix(intercept): release waiting target approvals when the gate is switched off - #807

Open
leninworld wants to merge 3 commits into
BaaRai:mainfrom
leninworld:fix/target-gate-off-releases-approvals-804
Open

leninworld wants to merge 3 commits into
BaaRai:mainfrom
leninworld:fix/target-gate-off-releases-approvals-804

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #804.

Problem

Switching a task's target gate off (PATCH /api/tasks/{id} with "target_gate": false) had no effect on a call that was already waiting for a target approval. awaitTarget never read the flag, so the call sat out its wait bound (300 s, up to three rounds in the guard). The open request also stayed approvable: a click after the switch-off still went through Decide → ResolveTargetRequest and created a pin, which takes effect again if the gate is turned back on.

Behaviour change

  • The waiter reads the live flag. awaitTarget checks the scope's Enabled() on each pass (the 2 s poll, or a wake-up). When the gate has been switched off, the call closes the requests it opened (system decision, denied, "the target gate was switched off while the call waited; nothing was pinned") and is no longer blocked. The guard then re-checks, CheckTargets returns at once with the gate off, and the call runs as on any ungated task. A human approval that landed first still counts.
  • Merged asks are not approved by the switch. If a rule or judge ask was merged into the request, that approval was never given, so the call returns TARGET_APPROVAL_PENDING with a reason asking it to retry. On the retry the ask goes through the normal path.
  • Switching the gate off closes the task's open requests. Manager.UpdateTaskMetadata calls the new Interceptor.CloseTargetRequests on an on→off change. It runs db.CloseTargetRequests, which settles every pending target request of the task as a system denial in one UPDATE … RETURNING id, and wakes the local waiters. Rows nobody waits on are closed too (non-waiting dial/redirect calls, calls that already left), so a late click gets ErrAlreadyDecided and pins nothing. Calls on other instances see the closed row on their next poll. Off→on and on→on close nothing. If the close fails, the PATCH still succeeds (the flag is already stored) and the error is logged.
  • Races around the switch: a call that reaches RequestTargets after the switch-off opens no request. A call that opens its rows while the switch happens re-reads the flag before it waits (or returns, for WaitBound 0) and closes them. A retry row that replaces an attached merged row is tracked with the call's other rows, so it is closed too.
  • Connections follow the same release. The pinned TCP dialer and the SOCKS5 UDP resolver ask with WaitBound 0. If the gate is switched off while they open a request, they now dial or resolve exactly as they would with the gate off (target_dial.go), and they no longer return an empty-coded dial error.

Not changed: conversations, whose flag is frozen per turn (server/conversations.go), the guard's targetPasses, and DefaultTargetWaitBound. With the release above, a gate-off call leaves the pass loop at once, so the 3 × 300 s stall in point 3 of the issue no longer applies to it.

Tests

New tests:

  • intercept: TestTargetGateSwitchOffReleasesPlainRequest, TestTargetGateSwitchOffClosesMergedRequest, TestTargetGateOffOpensNoRequest, TestTargetGateSwitchedOffWhileOpeningClosesRows, TestTargetGateSwitchOffReleasesAttachedRetry, TestHumanTargetApprovalWinsGateSwitchOff, TestCloseTargetRequestsWakesWaiter, TestPinnedDialGateSwitchedOffAfterRequestUsesBaseDialer, TestPinnedUDP4GateSwitchedOffAfterRequestResolvesPlain.
  • db: TestCloseTargetRequestsOnlyClosesPendingTargetsInScope. It checks that another task's rows, rows already decided and classic asks are left alone.
  • server: TestTaskTargetGateSwitchOffClosesOpenRequests goes through the real PATCH and covers late Decide with no pin, and on→on / off→on.

TestClaimLostEndsGateWait now creates a gated task. It called RequestTargets on a task whose gate is off, a path production never takes, and with the live flag read that call no longer opens a request.

Testing

Run against a local PostgreSQL 14 with ARTEX_PG_DSN:

  • On main, the two switch-off tests in intercept fail ("the call was not released within a few seconds") and the server test fails (the row is still pending after the PATCH). The CloseTargetRequests tests do not build on main.
  • With the change, go test ./intercept/ ./db/ ./guard/ passes. In go test ./server/ every test passes except 10 that fail the same way on main in this local environment and are not related to this change: an SQL_ASCII test database (the record_fact and Fix410 tests), no 127.0.0.2 on macOS (three HTTPTool sandbox tests), TestConnectWithoutPasswordRealPG, TestEnginePauseKillSwitchKillsEscapedDaemonsAndStopsEgress and TestManagerSOCKS5UDPOptInAndAssetBlockRuleDropsDatagram.
  • I disabled each new guard in turn, and its test failed every time. The guards are the check when the call starts, the re-read after the rows are opened, the retry-row tracking, and the TCP and UDP fallbacks.
  • gofmt -l and go vet ./intercept/ ./db/ ./server/ ./guard/ are clean.

Fixes #804

…tched off

Switching a task's target gate off left a gated call waiting out its bound
and the open request approvable, so a late click still pinned the target.

The waiter now reads the live flag and closes the call's requests as a
system decision; a plain call then runs as on an ungated task, a merged ask
is told to retry. Switching the gate off closes the task's open target
requests, so a late Decide gets ErrAlreadyDecided and pins nothing. TCP
dials and UDP resolves that open a request during the switch fall back to
the gate-off path.

Fixes BaaRai#804

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right base for #804: the simple call is released without a denial, CloseTargetRequests runs from UpdateTaskMetadata, and the dial/UDP paths re-read the gate. One race remains before merge.

UpdateTask and CloseTargetRequests are two operations. A click that lands between them (or while CloseTargetRequests fails, or on another instance) still reaches ResolveTargetRequest and can create a pin after target_gate=false. #808 has the missing piece: lock the task row (tasks ... FOR UPDATE, after reading task_id, then intercept_pending ... FOR UPDATE, same order as task deletion), read target_gate under that lock, and refuse the approval when it is off.

Please fold that lock and its test into this branch, with credit to the #808 author. One detail from that path: when a late click is converted to a system denial, intercept.Decide currently returns nil, wakes the waiter with allowed=true, and still writes approver/approver_addr on the row. It should return an already-decided error (or otherwise not report success) and not wake with allowed=true.

Switching the gate off and closing the task's open requests are two
operations, so an approval landing between them (or while the close
fails, or on another instance) could still pin the target.

ResolveTargetRequest now locks the task row before the pending row (the
order task deletion uses), reads target_gate under that lock and turns an
approval on a gate-off or deleted task into a system denial: no pin, no
approver fields, the same row shape CloseTargetRequests writes. Decide
reports it as already decided and wakes local waiters with false, and a
waiter seeing that system close releases the call even if its local gate
flag lags.

Tests that approve target requests now create gate-on tasks.

The task-row lock and its test come from BaaRai#808.

Co-authored-by: Jwalith <jwalith4418@gmail.com>
@leninworld

Copy link
Copy Markdown
Contributor Author

Thanks, folded in.

  • Race between UpdateTask and CloseTargetRequests: ResolveTargetRequest now reads the row's task_id, locks the task row (tasks ... FOR UPDATE), then the intercept_pending row (the same order task deletion uses), reads target_gate under that lock and refuses an approval when the gate is off or the task is gone. The lock and its test come from fix: release target approvals when the task gate is disabled #808 (credit to @jwalith4418-Mj, co-author on the commit). TestResolveTargetRequestRefusesApprovalAfterTaskGateDisabled covers the click between the two operations; TestResolveTargetRequestWaitsForTaskGateLock holds the task row lock, flips the gate, and checks the approval blocks and is then refused.
  • Late click converted to a system denial: the row is written by the same helper CloseTargetRequests uses (decided_by='system', no approver/approver_addr, target outcome denied). ResolveTargetRequest returns db.ErrTargetGateDisabled; Decide maps it to ErrAlreadyDecided (409 over HTTP) and wakes local waiters with false, never true. A waiter that sees that system close releases the call as gate-off, also when its local gate flag has not caught up yet (gate switched off on another instance). Tests: TestDecideLateApprovalUsesGateOffRelease, TestCloseTargetRequestsGateOffReasonBeatsLaggingFlag.
  • Tests that approve target requests on tasks now create those tasks with the gate on (agent/guard/server fixtures; no assertions changed).
  • Merged main to clear the CHANGELOG conflict.

Validation against a local PostgreSQL 14: go test ./db/ ./intercept/ ./agent/ ./guard/ pass; the new tests fail without the change. In ./server the only failures are the same ones the pre-change head shows in this environment (database encoding, 127.0.0.2, password-less auth, kill switch, SOCKS5 UDP). gofmt and go vet ./... are clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flipping target_gate off does not release a waiting approval, and a late click still pins the target

2 participants