python-trio / python-trio/trio

operations on wrap_file objects cannot be cancelled

Open
#1,350 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with trio.wrap_file and its use of trio.to_thread.run_sync, then review the linked cancellation-handling documentation and the reproducer in the issue. Determine whether cancellation behavior should change or be documented, and define completion around the agreed behavior for the one-second example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.