Skip to content

Name the event and multidict when a watcher callback fails - #1585

Merged
asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:watcher-unraisable-message
Sep 25, 2026
Merged

asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:watcher-unraisable-message

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

A failing watcher callback is now reported the way CPython's _PyDict_SendEvent() reports a failing dict watcher. The message names the event and the multidict's type and address, for example Exception ignored in MultiDict_EVENT_ADDED watcher callback for <multidict._multidict.MultiDict object at 0x...>, and the multidict itself is no longer passed to sys.unraisablehook. Previously every event except DEALLOCATED passed the multidict, so the default hook would repr() it and could run arbitrary code (a subclass __repr__) while an error was being reported.

On 3.13+ this uses PyErr_FormatUnraisable(). On 3.10 to 3.12, which lack it, it uses _PyErr_WriteUnraisableMsg(), and the address is formatted with PRIxPTR because MSVC's %p omits the 0x. This only affects the C extension: watchers have no pure-Python counterpart.

Are there changes in behavior for the user?

Yes, for C API clients whose watcher callbacks fail: sys.unraisablehook now receives object=None and a descriptive err_msg.

Is it a substantial burden for the maintainers to support this?

No. It is one cold function with a version branch.

Related issue number

Follow-up to #1551.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
  • make doc-spelling passes and any new technical words are added to docs/spelling_wordlist.txt

Drafted with Claude Code (Claude Opus 5.5); to be reviewed by @asvetlov.

Agent run details (optional, for reviewers)

Each leg ran in its own venv and tree copy.

  • GIL 3.14.7: 2734 passed
  • Free-threaded 3.14.7t, MULTIDICT_DEBUG_BUILD=1, Cython harness required: 2866 passed
  • GIL 3.10.17 (covers the _PyErr_WriteUnraisableMsg() branch): 2734 passed
  • Pure Python 3.14.7, --no-c-extensions: 1286 passed
  • pre-commit on the changed files and make doc-spelling: pass

This touches _md_watch_call() in watch.h, as #1583 does, so whichever lands second needs a small rebase.

Report a failing callback the way CPython's _PyDict_SendEvent() does: a
message naming the event and the multidict's type and address, and no
object for sys.unraisablehook to repr(). Previously the multidict itself
was passed, so formatting the report could run arbitrary code.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 25, 2026
@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:watcher-unraisable-message (fa3e46c) with master (cd03110)

Open in CodSpeed

PyErr_FormatUnraisable() formats %p with the platform's printf and only
guarantees the 0x prefix, so Windows 3.13+ reports a zero-padded,
uppercase address that id() formatting does not match.
@asvetlov
asvetlov marked this pull request as ready for review September 25, 2026 14:13
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 25, 2026 14:13
@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Changes how watcher callback errors are reported to the hook.

No outstanding findings block merging.

Reviews (2) · Last reviewed commit: "Use the past tense in the news fragment"

Comment thread CHANGES/1585.feature.rst Resolved
@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3e0105a) to head (fa3e46c).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1585   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         7701      7760   +59     
  Branches       643       648    +5     
=========================================
+ Hits          7701      7760   +59     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asvetlov
asvetlov merged commit 59488ea into aio-libs:master Sep 25, 2026
62 checks passed
@asvetlov
asvetlov deleted the watcher-unraisable-message branch September 25, 2026 14:51
asvetlov added a commit that referenced this pull request Sep 25, 2026
<!-- Thank you for your contribution! -->

## What do these changes do?

`AGENTS.md` already asked for past-tense news fragments, but an agent
still wrote the #1585 fragment as "is now reported ... is now ``None``".
This spells out that every verb describing the change is past tense and
that the fragment leads with one, names "X is now Y", "X now does Y" and
"X will ..." as not past tense, and adds a bad and good example pair
taken from #1585. The existing `1310.bugfix.rst` style example also used
"is now fixed", so it was rewritten to lead with "Fixed". A matching
"Things not to do" bullet was added.

## Are there changes in behavior for the user?

No.

## Is it a substantial burden for the maintainers to support this?

No; documentation for contributors only.

## Related issue number

Related to #1585

## Checklist

- [x] I think the code is well written
- [ ] Unit tests for the changes exist: N/A, documentation only
- [x] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`: N/A
- [x] Add a new news fragment into the `CHANGES/` folder
- [x] `make doc-spelling` passes and any new technical words are added
to `docs/spelling_wordlist.txt`

Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.

<details>
<summary>Agent run details (optional, for reviewers)</summary>

Spelling: `make doc-spelling` succeeded.
Lint: `pre-commit run --files AGENTS.md CHANGES/1588.contrib.rst`
passed.
</details>
asvetlov added a commit that referenced this pull request Sep 26, 2026
<!-- Thank you for your contribution! -->

## What do these changes do?

Prepares the news fragments for 7.0.0.

The public C API (#1480) and the watchers API (#1551) are new in this
release, so the fragments describing later changes to them describe
states that never shipped. They are folded into the entries that added
the APIs: the visitor's identity and hash (#1552, #1561) into `1478`,
and the 32-watcher limit, the watcher registration race fix and the
failing-callback report (#1574, #1583, #1585) into `1551`. The
`MultiDict_SetDefault(NULL)` half of `1553` is dropped for the same
reason.

Intermediate header names that never shipped are removed as well: `1510`
no longer mentions `deferred_decref.h`, which #1530 merged into
`reflist.h`, and `1513` names `debug.h` directly, so `1576` goes.
`1572`, a fix to a tool added this cycle, is folded into `1529`. `1560`
and the last sentence of `1556` go too, since the release refresh of the
benchmark tables supersedes those intermediate ones.

Finally, the fragments that did not lead with a past-tense verb or
described the change as "now ..." are rewritten per `AGENTS.md`.

The benchmark table refresh from `RELEASE.md` step 2 is still to be
added to this branch before it leaves draft.

## Are there changes in behavior for the user?

No.

## Is it a substantial burden for the maintainers to support this?

No.

## Related issue number

N/A

## Checklist

- [x] I think the code is well written
- [ ] Unit tests for the changes exist (N/A, changelog only)
- [x] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt` (N/A)
- [ ] Add a new news fragment into the `CHANGES/` folder (N/A, edits
existing fragments; the benchmark refresh will add a `doc` fragment)
- [x] `make doc-spelling` passes and any new technical words are added
to `docs/spelling_wordlist.txt`

Drafted with Claude Code (Claude Opus 5.5); pending review by @asvetlov
before leaving draft.

<details>
<summary>Agent run details (optional, for reviewers)</summary>

`python -Im towncrier build --draft --version 7.0.0` renders cleanly.
The symlinked pairs `1512` -> `1492` and `1562` -> `1555`, and both
`1535` fragments, are intentional and left alone.
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant