Skip to content

operations on wrap_file objects cannot be cancelled #1350

Open
@lithp

Description

@lithp

Here's an example program which "should" exit after one second, but instead it hangs forever.

#!/usr/bin/env python

import os
import trio

async def main():
    async with trio.open_nursery() as nursery:
        fake_in_read_fd, _ = os.pipe()
        fake_in_read = os.fdopen(fake_in_read_fd)

        wrapped = trio.wrap_file(fake_in_read)
        with trio.move_on_after(1):
            await wrapped.read(1)

if __name__ == '__main__':
    trio.run(main)

This is not necessarily a bug, wrap_file is implemented with trio.to_thread.run_sync which defaults to ignoring cancellation (as mentioned in the "Cancellation handling" section of that link). However, if it's not a bug it's certainly surprising behavior, nothing in the wrap_file documentation warned me this might happen! And even if it was documented, I would appreciate a cancellable flag on wrap_file which was passed into run_sync. That sounds dangerous, but I think I would be up to contributing a patch if you agree it makes sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions