MagicStack / MagicStack/uvloop

_monitor_fs does not seem to hold reference to callback

オープン
#526 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Cython
スター
11.9k
フォーク
616
PR マージ指標
30日以内にマージされた PR はありません

説明

uvloop version: 0.17.0
python version: 3.8.10
platform: linux

The bug can be reproduced with the below script. I call _monitor_fs on the loop, passing in a function callback that itself has a reference to the file I'm reading as well as the handle to the _monitor_fs. I set a timer to later call gc.collect and when that happens, my object gets deleted and I stop getting read events.

This does seem to be a bug because the loop itself should have reference to my callback which has a reference to my object and that object has a reference to the file and handle, so GC should not be collecting them.

import functools
import gc
import sys
import tempfile
import typing

import uvloop
import os.path
import subprocess

class FileAndHandle:
    def __init__(self, f: typing.IO):
        self.f = f
        self.handle = None

    def __del__(self):
        print('goodbye cruel world')

class FileWatcher:
    _proc: typing.Optional[subprocess.Popen]

    def __init__(self):
        self._loop = uvloop.new_event_loop()
        self._proc = None
        self._last_read_event = 0

    def _do_read(self, file_and_handle: FileAndHandle, _os_fn: bytes, _ev_enum: int):
        self._last_read_event = self._loop.time()
        print(f'read: {repr(file_and_handle.f.read())}')

    def _check_read_timer(self):
        t = self._loop.time()
        if t - self._last_read_event > 3:
            print('stopped getting read events')
            self._proc.kill()
            self._loop.stop()
        else:
            self._loop.call_later(1, self._check_read_timer)

    def _start_watching(self, f_path: str) -> None:
        fah = FileAndHandle(open(f_path))
        fah.handle = self._loop._monitor_fs(f_path, functools.partial(self._do_read, fah))

    def main(self) -> None:
        with tempfile.TemporaryDirectory() as tmp_dir:
            pth = os.path.join(tmp_dir, "file.txt")
            open(pth, 'wt').close()
            self._proc = subprocess.Popen([sys.executable, '-c', f'''
import time
with open("{pth}", "wt") as f:
    while True:
        time.sleep(2)
        f.write("hello\\n")
        f.flush()
'''])
            self._start_watching(pth)
            self._loop.call_later(3, self._check_read_timer)
            self._loop.call_later(15, gc.collect)
            self._loop.run_forever()

if __name__ == '__main__':
    FileWatcher().main()

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供された reproducer を実行し、gc.collect の後にイベントが停止するか確認します。次に _monitor_fs のエントリポイントとその callback の処理を追跡します。callback と、それが参照する FileAndHandle が引き続き使用可能で、ガベージコレクション後も読み取りイベントが継続すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
linux, python
領域
backend, operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。