Skip to content

CBuffered*Test.test_flush_error_on_close test failure investigation #3826

Closed

Description

@deantvv

Issue

Reason for test: test_flush_error_on_close in CBufferedReaderTest, CBufferedWriterTest, CBufferedRandomTest

Finding

Buffered*.closed need to acquire lock to access raw

RustPython/vm/src/stdlib/io.rs

Lines 1498 to 1503 in 6ad0e54

fn closed(&self, vm: &VirtualMachine) -> PyResult {
self.lock(vm)?
.check_init(vm)?
.to_owned()
.get_attr("closed", vm)
}

which makes b.closed(L1156) failed to execute because lock already acquired on b.close(L1160)

RustPython/Lib/test/test_io.py

Lines 1155 to 1160 in 6ad0e54

def bad_flush():
closed[:] = [b.closed, raw.closed]
raise OSError()
raw.flush = bad_flush
b = self.tp(raw)
self.assertRaises(OSError, b.close) # exception not swallowed

In cpython, it doesn't acquire lock in b.closed which makes them passing this test.
https://github.com/python/cpython/blob/774ef28814d0d9d57ec813cb31b0a7af6c476127/Modules/_io/bufferedio.c#L449-L461

IMO, accessing raw should acquire a lock (then cpython is wrong?), and the test scenario doesn't seem so common in real world use case. Maybe we shouldn't care about this failure test that much. But since I've already spent several night digging on this issue, I would like to post a issue describing this issue in case someone else also falling down on this rabbit hole.

Activity

  1. youknowone commented on Dec 25, 2025

    @youknowone
    Member

    The tests are recently fixed. It was related to missing Drop impls for them. Thank you for the report!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions