Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop_event weird behavior #329

Open
CNSeniorious000 opened this issue Feb 26, 2025 · 0 comments
Open

stop_event weird behavior #329

CNSeniorious000 opened this issue Feb 26, 2025 · 0 comments
Labels

Comments

@CNSeniorious000
Copy link

CNSeniorious000 commented Feb 26, 2025

Description

If watchfiles is imported inside a separated thread and stop_event is deleted after being set, it will be weirdly reset to False.

I can't clearly explain this, so I upload a minimal reproduction repo here:

https://github.com/CNSeniorious000/watchfiles-repro

Example Code

class Reloader:
    def start(self):
        from threading import Thread

        self.thread = Thread(target=self.run)
        self.thread.start()

    def stop(self):
        self._event.set()
        assert self._event.is_set()
        del self._event  # remove this line can solve the issue too
        self.thread.join()

    @cached_property
    def _event(self):
        return Event()

    def run(self):
        from watchfiles import watch  # move this line to the top of the file to solve the issue

        for _ in watch(".", stop_event=self._event):
            ...


reloader = Reloader()

reloader.start()
reloader.stop()

print("done")

In the snippet above, both threading.Event and asyncio.Event will fail.

Watchfiles Output

"done" will never show

Operating System & Architecture

Windows-11-10.0.26100-SP0
10.0.26100

Environment

the normal way

Python & Watchfiles Version

python: 3.13.2 (main, Feb 5 2025, 19:16:35) [MSC v.1942 64 bit (AMD64)], watchfiles: 1.0.4

Rust & Cargo Version

No response

CNSeniorious000 added a commit to promplate/pyth-on-line that referenced this issue Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant