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

Fix watchdog reload worker repeatedly if there are multiple changed files #1555

Closed
wants to merge 8 commits into from

Conversation

lexhung
Copy link
Contributor

@lexhung lexhung commented Apr 18, 2019

When there are multiple changed files (e.g. git pull, installing new version of a package, etc.), watchdog attempts to reload workers at first detected change and repeat for every changed file.

The patch allow gathering all changes into a single restart action.

@codecov
Copy link

codecov bot commented Apr 18, 2019

Codecov Report

Merging #1555 into master will increase coverage by 2.04%.
The diff coverage is 82.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1555      +/-   ##
==========================================
+ Coverage   92.17%   94.21%   +2.04%     
==========================================
  Files          23       22       -1     
  Lines        2312     2315       +3     
  Branches      424      426       +2     
==========================================
+ Hits         2131     2181      +50     
+ Misses        141       84      -57     
- Partials       40       50      +10     
Impacted Files Coverage Δ
sanic/app.py 92.06% <0.00%> (-0.20%) ⬇️
sanic/reloader_helpers.py 67.32% <88.88%> (+52.58%) ⬆️
sanic/__init__.py

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7c04c9a...1b64573. Read the comment docs.

Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Why not break out of the loop when need_reload is True, instead of continuing to look at every file?

@lexhung
Copy link
Contributor Author

lexhung commented Apr 18, 2019

Why not break out of the loop when need_reload is True, instead of continuing to look at every file?

In my case, when I update a dependency package there are about 30 files being touched at the same time which trigger the app to reload 30 times continuously (breaking the loop there will only update the mtime of a single file). This patch allow all modified files' mtime to be updated before trigger reload.

It should have no big performance impact since it have to scan all files in the ordinary case when no file is modified.

@ahopkins
Copy link
Member

ahopkins commented May 5, 2019

I guess... it does seem like it would potentially have a negative impact though. Especially for larger projects. I am hesitant to accept a change like this that could potentially have a large impact on a number of developers in their daily workflow.

I understand your issue and concern, but I would rather find a solution that does not also impact the existing operation.

@ahopkins
Copy link
Member

ahopkins commented May 5, 2019

Instead, what if we make watchdog a configurable param:

app.run(watchdog=my_custom_watchdog)

@stale
Copy link

stale bot commented Aug 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@stale stale bot added the stale label Aug 3, 2019
@Tronic
Copy link
Member

Tronic commented Aug 27, 2019

I might have a better look at the auto-reloader in the coming days; its killing of existing processes seems rather broken, and this patch is probably a step in the right direction.

@stale stale bot removed the stale label Aug 27, 2019
@Tronic
Copy link
Member

Tronic commented Sep 9, 2019

This needs a few changes. First of all, multiprocessing.Process is used to run subprocess ... meaning a chain of two processes. After fixing that so that subprocess is used directly, it can also be terminated reliably, and we no longer need those hacks to kill all child processes.

I worked on this in the Trio branch, but in principle the changes should apply to asyncio implementation too. It appears to be working with single as well as multiple workers, as long as the master server kills its worker processes when it receives SIGTERM (dunno how Windows-compatible that is, might need another solution there).

My current revision:
https://github.com/huge-success/sanic/blob/53f088e0cf3c5584ece8f61de5082970a9514b78/sanic/reloader_helpers.py

@lexhung could you give it a little bit of polish and testing on current Sanic master?

@lexhung
Copy link
Contributor Author

lexhung commented Sep 15, 2019

Hi @Tronic We may want to go through manual review for this to get merged.

I implemented the tests however it is quite tricky to get the patch coverage passed. These are parts that related to OS exceptions and it is not clear to me how to set that up without making the tests brittle.

@stale
Copy link

stale bot commented Dec 14, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@stale stale bot added the stale label Dec 14, 2019
@Tronic
Copy link
Member

Tronic commented Dec 15, 2019

@ahopkins Are you reviewing this?

@stale stale bot removed the stale label Dec 15, 2019
@ahopkins
Copy link
Member

ahopkins commented Dec 21, 2019

Yes. I am taking a look at it. I will respond with more detailed thoughts when I am back at my workstation (beginning of the week) and can fire up a few tests on this.

@ahopkins ahopkins self-requested a review December 21, 2019 17:46
@sjsadowski sjsadowski added the needs review the PR appears ready but requires a review label Feb 5, 2020
@stale
Copy link

stale bot commented Mar 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@stale stale bot added the stale label Mar 20, 2020
@Tronic
Copy link
Member

Tronic commented Mar 23, 2020

Looks like this is still stuck with no review. Even if the patch isn't quite the ideal solution we might have in our minds, I believe that it is definitely an improvement and it works on my machine, so I suggest for this to be merged as well.

@stale stale bot removed the stale label Mar 23, 2020
@Tronic Tronic mentioned this pull request Mar 30, 2020
@ahopkins
Copy link
Member

ahopkins commented Jun 3, 2020

I am thinking this should be closed once #1827 is finished. Am I mistaken?

@Tronic
Copy link
Member

Tronic commented Jun 3, 2020

@ahopkins #1827 is already finished but it was a bit too late & scary change for 20.3 (needs review now). Closing in favour of #1827 which contains these changes as well.

@Tronic Tronic closed this Jun 3, 2020
@ahopkins ahopkins removed the needs review the PR appears ready but requires a review label Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants