⚡ Sangsakala Proxy 🏠 Home
Skip to content

Handle CSV writer callbacks safely - #9636

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix/csv-writer-reentrancy-20261103
Nov 4, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix/csv-writer-reentrancy-20261103

Conversation

@youknowdot

@youknowdot youknowdot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Use the existing callback-aware PyOperation for csv.writer.writerow instead of holding a non-reentrant unit mutex across iterator, field conversion, and write callbacks. A callback that invokes the same writer can otherwise wait on its own mutex. The writer's output buffer is local to each call, and its dialect and write target are immutable.

Obtain the row iterator through the normal iterator protocol and translate only iterator-acquisition TypeErrors to _csv.Error, as CPython does. Other acquisition errors and errors raised while advancing the iterator still propagate.

The row argument remains owned until after the operation section exits. This change introduces interpreter binding and operation bookkeeping; its runtime cost has not been benchmarked.

Scope

This addresses the lock/reentry behavior, not complete reentrant-output parity. CPython reuses an internal record buffer: a nested writerow from a field's __str__ can affect the outer record. RustPython constructs each record locally and already materializes input fields before string conversion, so its reentrant output and callback order can differ. Other iterator handling differences remain outside this change. Type names longer than 200 bytes are truncated at a UTF-8 boundary, which can differ from CPython at a multibyte cutoff.

This combines cleanly at file level with #9635; no merge was performed.

Validation

  • Compared CPython 3.15.0 critical-section and writer implementation; a CPython callback reproduction confirmed a nested write from __iter__ returns and writes both records. A nested __str__ reproduction confirmed the buffer difference described above.
  • CPython 3.15.0 reproductions covered TypeError and ValueError raised by __iter__, a non-iterator return, and TypeError from __next__.
  • Independent source review of both commits and its composition with Require callable write method for CSV writers #9635, normal commit hooks, and diff checks passed. The RustPython deadlock follows from holding its non-reentrant mutex across callbacks; it was not reproduced in a local RustPython execution.
  • RustPython was not built or executed locally. No tests were added or changed; hosted CI can run existing test_csv coverage.

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 41 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 18fc82b and 9cd138d.

Files selected for processing: 1
  • crates/stdlib/src/csv.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

Assisted-by: OpenAI Codex:model version not exposed
@youknowdot youknowdot changed the title Allow callbacks to reenter CSV writers Handle CSV writer callbacks safely Nov 3, 2026
@youknowone
youknowone marked this pull request as ready for review November 4, 2026 01:21
@youknowone
youknowone enabled auto-merge (squash) November 4, 2026 01:21
@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-04T01:24:33.341044Z 9cd138d 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.

@youknowone
youknowone merged commit 808f2ed into RustPython:main Nov 4, 2026
20 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.

2 participants