⚡ Sangsakala Proxy 🏠 Home
Skip to content

Fix asyncio callback ordering and reentrant completion - #9673

Open
youknowdot wants to merge 1 commit into
RustPython:mainfrom
youknowdot:asyncio-callback-lifecycle-20261103
Open

youknowdot wants to merge 1 commit into
RustPython:mainfrom
youknowdot:asyncio-callback-lifecycle-20261103

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

Share Future/Task callback registration, removal and scheduling instead of maintaining duplicate implementations.

  • Preserve callback order when the first callback has been removed.
  • Let the first call_soon observe and remove later callbacks before detaching their list.
  • Preserve removal counts and cleared state when equality callbacks complete the future.
  • Release removed callbacks/contexts outside locks, keeping surviving callbacks alive during reentrant finalization.
  • Only construct exception subclasses, recheck state after construction, and enable exception logging after successful scheduling.

Production Rust only: 153 additions and 292 deletions. Existing argument-binding structs are reused.

Reference

Matches CPython 3.15 callback scheduling, callback removal, and exception completion.

Validation

  • cargo check -p rustpython-vm -p rustpython-host_env -p rustpython-stdlib --features rustpython-stdlib/threading --locked --offline -j1
  • cargo clippy -p rustpython-vm -p rustpython-host_env -p rustpython-stdlib --features rustpython-stdlib/threading --locked --offline -j1 -- -D warnings
  • rustfmt, diff check, and normal commit hooks.
  • Nine external reference probes passed on CPython 3.15, including reentrant scheduling, constructor side effects and finalizer ordering. These establish expected behavior; they are not RustPython runtime tests.

Native RustPython runtime tests, full workspace/C-API tests and cross-platform validation were not run. This does not redesign Task stepping/initialization or claim complete parity for generic list finalization.

AI assistance: implementation, source comparison and review were assisted by OpenAI Codex; model version is not exposed.

Share Future and Task callback handling, preserve callback lifetimes across reentrant removal, and validate Future exception completion boundaries.

Assisted-by: OpenAI Codex:model version not exposed
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 17 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0b081b0 and afc385f.

Files selected for processing: 1
  • crates/stdlib/src/_asyncio.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@youknowone
youknowone marked this pull request as ready for review November 4, 2026 12:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 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-11-04T13:04:12.676417Z afc385f Draft marked ready
ℹ️ 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: afc385fd7e

ℹ️ 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 on lines +415 to +417
let cb0 = self.fut_callback0.read().clone();
if let Some(cb0) = cb0 {
let cmp = vm.identical_or_equal(&cb0, &func)?;
if cmp {
*zelf.fut_callback0.write() = None;
*zelf.fut_context0.write() = None;
let cmp = vm.identical_or_equal(&cb0, func);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve the still-valid lock-release explanation

AGENTS.md requires: "Do not delete or rewrite existing comments unless they are factually wrong or directly contradict the new code." This refactor removes // Clone to release lock before comparison (which may run Python code), although the shared helper still relies on precisely that sequence here. The comment documents why equality must run without a field lock, so retain it in the extracted implementation rather than deleting it with the duplicate code.

AGENTS.md reference: AGENTS.md:L165-L165

Useful? React with 👍 / 👎.

Comment on lines +423 to +424
drop(callback);
let context = self.fut_context0.write().take();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Detach the old context before finalizing callback0

If a pending future owns the last reference to its first callback, removing it via a distinct equal object can now run its __del__ at drop(callback), since the temporary cb0 reference was already dropped. If that finalizer calls add_done_callback(replacement, context=saved_context) and no callback list exists, registration repopulates both fut_callback0 and fut_context0. The following take() then clears the replacement's context, so completion runs the surviving callback in the completer's context instead of its registered context. The parent kept cb0 alive until both old fields were cleared. Take both old fields before dropping either reference so finalization cannot consume newly registered state.

Useful? React with 👍 / 👎.

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.

2 participants