_monitor_fs does not seem to hold reference to callback
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- linux, python
- Domain
- backend, operating-systems
Research direction
Run the supplied reproducer and observe whether events stop after gc.collect. Then trace the _monitor_fs entry point and its callback handling; done means the callback and its referenced FileAndHandle remain usable and read events continue after garbage collection.
Written by the indexing model from the issue text.
Description
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()
- Dominant language
- Cython
- Stars
- 11.9k
- Forks
- 615
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MagicStack/uvloop
-
License not clear Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#759 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#741 · 2 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#702 · 8 comments · 9 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
MagicStack/uvloop#766 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
MagicStack/uvloop#765 ·
All issues in MagicStack/uvloop
Similar issues
-
clawsweeper:linked-pr-open clawsweeper:no-new-fix-pr clawsweeper:source-repro impact:message-loss issue-rating: 🦞 diamond lobster maturity:stable P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
growthbook/growthbook#7100 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
punkpeye/mcp-remote#369 ·
-
Client customer-reported needs-team-attention question Service Attention WebPubSub
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Azure/azure-sdk-for-net#63292 · 3 comments · 1 reaction ·
-
bug p:3-mid pydanty:bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pydantic/pydantic-ai#8643 · 1 comment ·