Skip to content

Add 33 advanced pitfalls: asyncio, threading, interpreter internals, data systems - #2

Merged
EMRahman merged 2 commits into
mainfrom
docs/advanced-pitfalls
Oct 30, 2026
Merged

EMRahman merged 2 commits into
mainfrom
docs/advanced-pitfalls

Conversation

@EMRahman

@EMRahman EMRahman commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds Part II of the catalog: 33 advanced pitfalls on four new pages in docs/, plus an "In the wild" section on how Home Assistant runs into and guards against them. Total goes from 19 to 52, every one with a self-checking example run in CI.

Originally stacked on #1, which is now merged; this PR targets main.

New pages

Page # Highlights
Concurrency 20–30 Tasks garbage-collected mid-flight · call_soon from a thread stalls 500 ms · threading.local gives Alice's request to Bob · fork() child deadlocks on a copied lock
Object model & imports 31–37 A typo in a property reported as 'Device' object has no attribute 'temperature' · @lru_cache keeps every instance alive · mock.patch on the wrong module
Interpreter internals 38–42 python -O deletes validation · class-body comprehensions read the global · id() reuse returning stale cache entries · 4300-digit int/str limit
Data systems 43–52 hash() sharding differs per process · floats lose nanoseconds · datetime merges trades 500 ns apart · int(1.15 * 100) == 114 · DST arithmetic · struct padding · NaN in JSON

Real-world sources

  • Home Assistant: examples 48 and 49 reproduce home-assistant/core#188537 (alert notifications looping during DST fall-back) and #188836 (backups an hour off after DST) using only the standard library. #24 cites the Hikvision thread-safety fix (#160027), and the README describes HA's guard rails (task tracking, block_async_io.py, verify_event_loop_thread(), bugbear rules).
  • Databento: #44 and #47 reference DBN's fixed-point int64 prices (10⁻⁹ units) and the UNDEF_PRICE sentinel.

Determinism

Timing-sensitive pitfalls are forced to fail every time so CI can assert them: gc.collect() for task collection, time.sleep(0) to widen the race window, a held lock across fork(), subprocesses for -O, PYTHONHASHSEED and daemon threads. #29 and #50 skip on platforms without os.fork or time.tzset.

Test plan

  • All 52 examples pass locally on CPython 3.13.7 and 3.14
  • Every internal link and heading anchor resolves; external URLs return 200 and their anchors exist
  • CI passes on 3.10 to 3.14

🤖 Generated with Claude Code

…a systems

- docs/concurrency.md (20-30): asyncio task garbage collection,
  forgotten await, blocking the loop, swallowed cancellation,
  cross-thread loop calls, non-atomic +=, vanishing thread exceptions,
  Lock re-entry, threading.local under asyncio, fork() deadlocks,
  daemon threads.
- docs/object-model.md (31-37): property vs __getattr__, lru_cache
  leaks, __eq__ removing __hash__, __exit__ swallowing, weakrefs to
  bound methods, mock.patch targets, double-imported __main__.
- docs/interpreter.md (38-42): -O stripping asserts, class-body
  comprehension scope, locals() writes, id() reuse, int/str limit.
- docs/data-systems.md (43-52): hash() sharding, NaN, nanosecond
  timestamps, float prices, DST arithmetic, fixed UTC offsets,
  utcnow(), struct alignment, non-standard JSON.
- README: advanced contents table and an "In the wild" section on
  Home Assistant bug fixes and guard rails, each linked to the PR or
  source file. Examples 48 and 49 reproduce HA PRs #188537 and #188836.
- One self-checking example per pitfall; all 52 run in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T13:22:32.179482Z ff9ec95 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff9ec9583a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/concurrency.md Resolved
- examples/24: disable inherited asyncio debug mode before the stall
  demo. Under PYTHONASYNCIODEBUG=1 or -X dev, call_soon from a thread
  raised in the worker and the example hung forever. Waits now have a
  5s timeout, so a regression fails instead of hanging.
- docs/concurrency.md #21: -W error::RuntimeWarning does not fail on a
  never-awaited coroutine, because the warning fires during
  finalization and becomes an unraisable exception. Document the pytest
  filters that do fail (verified with pytest 9.2).
- CI: also run every example under -X dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EMRahman
EMRahman changed the base branch from docs/expand-pitfalls to main October 30, 2026 15:01
@EMRahman
EMRahman merged commit 06912e7 into main Oct 30, 2026
10 checks passed
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.

1 participant