Repository navigation
fix(intercept): release waiting target approvals when the gate is switched off - #807
leninworld wants to merge 3 commits into
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
# Conflicts: # CHANGELOG.md
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>
|
Thanks, folded in.
Validation against a local PostgreSQL 14: |
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.awaitTargetnever 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 throughDecide→ResolveTargetRequestand created a pin, which takes effect again if the gate is turned back on.Behaviour change
awaitTargetchecks the scope'sEnabled()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,CheckTargetsreturns at once with the gate off, and the call runs as on any ungated task. A human approval that landed first still counts.TARGET_APPROVAL_PENDINGwith a reason asking it to retry. On the retry the ask goes through the normal path.Manager.UpdateTaskMetadatacalls the newInterceptor.CloseTargetRequestson an on→off change. It runsdb.CloseTargetRequests, which settles every pending target request of the task as a system denial in oneUPDATE … 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 getsErrAlreadyDecidedand 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.RequestTargetsafter 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, forWaitBound0) 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.WaitBound0. 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'stargetPasses, andDefaultTargetWaitBound. 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:TestTaskTargetGateSwitchOffClosesOpenRequestsgoes through the realPATCHand covers lateDecidewith no pin, and on→on / off→on.TestClaimLostEndsGateWaitnow creates a gated task. It calledRequestTargetson 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:interceptfail ("the call was not released within a few seconds") and the server test fails (the row is stillpendingafter the PATCH). TheCloseTargetRequeststests do not build on main.go test ./intercept/ ./db/ ./guard/passes. Ingo 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: anSQL_ASCIItest database (therecord_factandFix410tests), no127.0.0.2on macOS (threeHTTPToolsandbox tests),TestConnectWithoutPasswordRealPG,TestEnginePauseKillSwitchKillsEscapedDaemonsAndStopsEgressandTestManagerSOCKS5UDPOptInAndAssetBlockRuleDropsDatagram.gofmt -landgo vet ./intercept/ ./db/ ./server/ ./guard/are clean.Fixes #804