Skip to content

Batch 673: security notifier close is bounded and records every cut notice (BACKLOG #6832) - #3260

Open
wshallwshall wants to merge 9 commits into
mainfrom
b673/notify-aclose
Open

wshallwshall wants to merge 9 commits into
mainfrom
b673/notify-aclose

Conversation

@wshallwshall

Copy link
Copy Markdown
Collaborator

Batch 673 (pipeline). This is its own PR because it changes a security control: how the security-event notifier closes and what it records. It merges clean with current main (git merge-tree exit 0).

BACKLOG #6832 -- the security notifier's close is bounded and leaves no notice unrecorded

  • Source branch b673/notify-aclose, head bc3562fea4435927dde2048e43489a4f46ab7b8e.
  • Verdict: REAL. Two failing tests showed it. A cut close left queued notices with no row, and a cut while the close waited on a full queue left the drain running.
  • Fix, in SecurityEventNotifier.aclose(timeout):
    • The close stops waiting at the timeout or on a cancel, then cancels the drain.
    • It records the send it cut as interrupted, and each queued notice, including a send that had not started on a worker thread, as abandoned. A send that finished before the cut is read as it ended.
    • The records run as a held, shielded task, so a cut stops only the wait. A failed send's record is shielded too.
    • A cut logs an ERROR naming the queued count first (lock notices are not counted).
    • A second concurrent aclose() waits for the first, then returns.
    • A send not yet started at the cut never mails.
    • api/app.py passes b * _INNER_SHARE to the close.
  • Also fixed, predating the branch: _log_send_failed now scrubs the username and the event type through scrub_log_argument. On main, both were logged raw.
  • Docs: SECURITY.md (the close behaviour, both reasons, and the residual below), the __main__ counting-sink comments, the alert_sinks docstring, and changelog.d/6832.fixed.md. The precondition of one Re-race the coord race test when the only invalid reason is start lateness #3120 test now expects an empty queue.
  • Residual, stated in SECURITY.md: the close's records are written one at a time with no bound. Under a full queue, many can still meet the store's close.
  • Proposed banner: FIXED. A cut close left queued notices unrecorded, and a cut on a full queue left the drain running; failing tests showed both. aclose now stops waiting at part of the teardown step's bound, cancels the drain, and records the cut send (reason interrupted) and each queued or unstarted notice (reason abandoned) in a shielded pass, with an ERROR count first. Residual: those records are unbounded serial writes.

Checks (Builder): ruff, both mypy package legs. pytest on test_security_notice_undelivered, test_lock_notice_off_request_path, test_lifespan_teardown, test_provision_first_administrator, test_engine_stop_budget, test_mfa, test_alert_*, test_security_doc_rate_limits and test_initial_credential_reminder: all pass. Full suite and the tests mypy leg not run. Read the pytest, mypy and changelog legs.

QA: xhigh. Rounds: 2 + 4 unrepaired. Tag: xhigh effort -> 10 inline angles -> dedup (no verify) -> sweep -> <=15 findings (round-8a5ebb8d4f, round-5946a3f037, round-db7e0eed1f, round-908381f651, round-67477f48ec, round-95c38613cc). Each unrepaired round led to a Manager-directed repair of a regression or a false text claim, and the last round reviewed 53f5acf3. The head, bc3562fe, fixes the log level, the log scrub and the doc wording, and no review round ran on it.

Open findings, to file under R2:

  1. A second aclose(timeout) ignores its own timeout while it waits for the first.
  2. A send whose thread returned, but whose result has not reached the loop, is recorded as interrupted, so the feed shows "undelivered" for a mail that probably went out. The Manager decided to keep this: when delivery is unknown, telling the recipient it may not have arrived is the safe side.
  3. _consume_outcome swallows a late send failure, and a late success leaves its interrupted row in place.
  4. The drain is now cut at 0.8 of the bound, so a few notices that would have gone out are recorded as abandoned.
  5. The close's records are written serially and compete with engine.stop(); under a full queue, most can be lost.
  6. Cleanups: the hold pattern and the cut logic are duplicated (the base class should own them), there is a redundant callback, a record task is created on every clean close, and _record_cut reads _sending lazily.
  7. The test docstring at line 840 is stale.

When the shutdown's bounded step cut SecurityEventNotifier.aclose while a
send waited on a slow relay, the notices still queued got no audit row,
and on a full queue the close was cut while putting its stop sentinel,
so the drain ran on past the close into the closed store.

aclose now cancels a drain the cut left running, records the cut send
with reason "interrupted" (its thread may still deliver it) and each
notice still queued with reason "closing", before it returns. A WARNING
line counts the queued notices, lock notices left out (BACKLOG #1131).
SECURITY.md's residual bullet and the alert sink's aclose docstring are
corrected to match.

BACKLOG #6832

Proposed vault banner:
FIXED (batch 673, branch b673/notify-aclose). Adjudicated by failing
tests: a cut close left queued notices unrecorded, and a cut on a full
queue left the drain running. aclose now cancels the drain and records
the cut send (reason interrupted) and each queued notice (reason
closing) while the store is open. Residual: those writes run in the
teardown's 0.1 s cut grace, so one still running then can meet the
closed store.
The precondition of the BACKLOG #3120 restart test read the sentinel a
cut close left behind. The close now empties the queue, recording each
notice it took, so the precondition reads an empty queue; the property
assertions are unchanged.

BACKLOG #6832
The send is shielded and held on the notifier, so a cut close reads
how it ended rather than guessing: a send already finished counts as
delivered or failed, and only one still running is "interrupted". A
failed send's record is a held, shielded task, so a cut during that
write no longer cancels it. Queued notices are taken and counted in the
log before any await, so a second cancel cannot leave them untold, and
they are recorded as "abandoned", apart from a notice refused once the
close began ("closing"). Failed records at close are summed into one
WARNING line; no sink means no record claim. SECURITY.md, the changelog
fragment, __main__'s counting-sink comments and the BACKLOG #3120 test
docstring are corrected to match.

BACKLOG #6832
aclose takes a timeout, and the API teardown hands it _INNER_SHARE of
its step's bound, so the close cuts its own drain and starts recording
before the step is cut. A send cancelled with the loop no longer
raises out of the record pass; a cut send logs that its delivery is
unknown, with or without a sink; a drain that died is logged rather
than swallowed; the failed-record count is logged in a finally; _task
is reset whatever state the drain is in. SECURITY.md now states the
residual as it is: the writes are unbounded and run on beside
engine.stop() until the store closes. Comments on start(), the
recording set and _MAX_RECORDING are corrected.

BACKLOG #6832

Proposed vault banner:
FIXED (batch 673, branch b673/notify-aclose). Adjudicated by failing
tests: a cut close left queued notices unrecorded, and a cut on a full
queue left the drain running. aclose now stops waiting at part of the
teardown step's bound, cancels the drain, and records the cut send
(reason interrupted) and each queued notice (reason abandoned), with a
WARNING count first. Residual, in SECURITY.md: the writes are
unbounded, so under a full queue many can still meet the store's close.
The record pass of a cut close is a held, shielded task, so the
teardown's cut stops only the close's wait and the rows go on being
written until the store closes; SECURITY.md said so and the code did
not. The close's own "did not drain" WARNING is gone: with only a lock
notice in flight it would tell a logs:view reader a lock landed
(BACKLOG #1131). A TimeoutError not from the close's own deadline is
re-raised, and a drain failure is logged only on a cut, where nothing
else raises it. SECURITY.md no longer says an interrupted
provision-admin run always leaves no row.

BACKLOG #6832

Proposed vault banner:
FIXED (batch 673, branch b673/notify-aclose). Adjudicated by failing
tests: a cut close left queued notices unrecorded, and a cut on a full
queue left the drain running. aclose now stops waiting at part of the
teardown step's bound, cancels the drain, and records the cut send
(reason interrupted) and each queued notice (reason abandoned) in a
shielded pass, with a WARNING count first. Residual, in SECURITY.md:
the writes are unbounded, so under a full queue many can still meet
the store's close.
The final unrepaired review found the "send still running" WARNING,
skipped for a lock notice, let its absence tell a logs:view reader a
lock landed, since a cut with notices queued nearly always has a send
in flight (BACKLOG #1131). The line is gone; the row remains. The
close's docstring and the alert sink's no longer say every record is
written before the close returns, the _MAX_RECORDING comment says "at
least", and a drain failure is logged only on a real cut.

BACKLOG #6832
The paragraph said the engine writes the row for a cut close; the
residual below it says those writes can fail. It now says the engine
tries to write it, and points at the residual.

BACKLOG #6832
(a) A second aclose() waits for the first to finish or be cut, and
closes nothing itself, so it no longer awaits the drain the first set
to None. (b) Each send passes a gate its thread must take before it
mails; a cut close skips a send still waiting for a worker thread, so
it never mails after the close, and records it as abandoned. (c) A
close cut by its own timeout logs a WARNING on every such cut naming
the queued count (lock notices left out), standing in for the
teardown's cut line. __main__ says "at least an interrupt".

BACKLOG #6832
The close's timeout-cut line logs at ERROR, the level of the teardown
line it stands in for. The failed-send line passes the username through
scrub_log_argument and the kind through notice_kind_log_label, like the
closing line (the raw username predates this branch). SECURITY.md and
the changelog name the abandoned reason for an unstarted send, and the
start() comment no longer rests on a sentinel a close cannot leave.

BACKLOG #6832
@wshallwshall wshallwshall added the qa Builder QA record posted; not a merge gate label Nov 24, 2026
@wshallwshall

Copy link
Copy Markdown
Collaborator Author

QA: level xhigh. Rounds: 2 + 4 unrepaired. Tag: xhigh effort -> 10 inline angles -> dedup (no verify) -> sweep -> <=15 findings (last round round-95c38613cc, on 53f5acf). The head, bc3562f, fixes the log level, the log scrub and the doc wording, and was not reviewed. Lander: ready to merge. This PR changes a security control, so it is on its own. The batch 673 Manager opened it; see the body for the R2 findings.

This branch has not been deployed

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

Labels

qa Builder QA record posted; not a merge gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant