You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Watchfiles seems to be monitoring directories like node_modules and .mypy_cache, and crashes with an OSError.
I've only briefly dug into this, and django-watchfiles doesn't seem to be doing any additional includes/excludes from the StatReloader, so I would naively presume the StatReloader is also monitoring files in those kinds of directories.
But I haven't been able to determine if there's any configuration we can use to ignore directories.
I haven't been able to replicate on OSX yet, but another of our developers is seeing the issue on Linux. Here's an excerpt of the stacktrace:
for changes in watch(
File "/home/path-to-venv/venv-kraken-core/lib/python3.12/site-packages/watchfiles/main.py", line 126, in watch
with RustNotify(
^^^^^^^^^^^
OSError: OS file watch limit reached. about ["/home/theproject/src/project/path-to-static-app/node_modules/react-datepicker/node_modules/date-fns/getMilliseconds"] (Error { kind: MaxFilesWatch, paths: ["/home/theproject/src/project/path-to-static-app/node_modules/react-datepicker/node_modules/date-fns/getMilliseconds"] })
Ah, fun. Happy to add a docs section on this. Not sure there’s anything else we can do...
A system check could potentially detect a low value and recommend bumping it, but the "max" number to set seems project-specific. Y’all have a LOT more code than the median project.
Sorry @adamchainz this is one of the things still sitting in my TODO pile. I was finding that watchfiles wasn't detecting changes to certain paths, and I wanted to dig into the cause of that (and see if it was related to sysctl limits) or something else, and then moved onto other things.
From memory (reader, beware!) I think the main issue is that we wanted more control over which paths to be monitored and which to exclude, so we aren't trying to set watches on the endless node_modules etc. The max user watches was a workaround for the underlying problem.
I was finding that watchfiles wasn't detecting changes to certain paths, and I wanted to dig into the cause of that (and see if it was related to sysctl limits) or something else, and then moved onto other things.
That was probably the issue fixed in #91, so worth a re-try.
I think the main issue is that we wanted more control over which paths to be monitored and which to exclude, so we aren't trying to set watches on the endless node_modules etc.
We don't have an API here for watchfiles yet. I don't know how easy it would be, given watchfiles.watch() takes a list of paths to watch, with no way to exclude subdirectories there, only post-event.
I haven't dug into this too closely, but is it expected that watchfiles would watch everything in a local .venv directory? That makes hitting this limit not so uncommon on many systems. Just curious if there's a configuration-level workaround for that, or if it's by design?
A little reading up reveals setrlimit, which increases the limit for the number of open files. So it may be possible to fix the issue in some cases by bumping the limit with a call to that. Testing in IPython:
In [1]: import resource
In [2]: resource.getrlimit(resource.RLIMIT_NOFILE)
Out[2]: (4096, 9223372036854775807)
In [3]: resource.RLIM_INFINITY
Out[3]: 9223372036854775807
In [4]: resource.setrlimit(resource.RLIMIT_NOFILE, (1_000_000, resource.RLIM_INFINITY))
In [5]: resource.getrlimit(resource.RLIMIT_NOFILE)
Out[5]: (1000000, 9223372036854775807)
Can one of y'all with the issue try adding a call to setrlimit in your server, with appropriate try/except OSError wrapping, and see if that fixes the issue? If so, we might add some default limit-bumping here.
Thanks for the suggestion. I tried it out and it doesn't seem to work. I think it sets the limit on file descriptors but we need to set the inotify limit(?). Also, according to AIs at least, it is not possible to update that limit programatically (at least without root). So I think any library-level solution might need to limit the number of watched files rather than attempt to modify the limits.
Do we have any leeway to leverage / passthrough the ignore_dirs setting to watchfiles as a filter? I haven't dug into any of the implementation details, but on the surface it seems like exposing that would likely solve this.
Edit: I tried playing with filters, but that also didn't seem to help. I think the initial watch call still sets up an inotify instance on every file before it reaches the filters and then discards them. (I think you already knew this based on your previous comments, but I didn't till now). So it seems like maybe the only solution which probably is not worth it would be to construct the list of paths dynmically based on a more complex set of rules within this library, so we can call watch() with a subset of files / directories before reaching the notify calls. 😞
I think it sets the limit on file descriptors but we need to set the inotify limit(?). Also, according to AIs at least, it is not possible to update that limit programatically (at least without root).
Ah, okay, thanks for trying.
I think the initial watch call still sets up an inotify instance on every file before it reaches the filters and then discards them. (I think you already knew this based on your previous comments, but I didn't till now).
That was my mental model, yes, but I hadn't 100% confirmed it. Thanks for checking.
So it seems like maybe the only solution which probably is not worth it
It sounds like it would be worth it for a large enough project, for which I know @jarshwah works on one, and I have a client with another! So while I can't say I'll be working on it right now, there's hope…
Before then, maybe we can document how to change the limits?
Nice. Yeah a workaround like this: sphinx-doc/sphinx-autobuild#194 is what I had in mind as something that could be done as a wrapper. If you think you'd accept something like that as a patch (I would probably replace the "." prefix behavior with a configurable django setting to ignore dirs), I'd be game to take a crack at it
Activity
jarshwah commentedon Feb 26, 2025
The linux folks running into this issue were able to solve it by adjusting fs limits with sysctl:
fs.inotify.max_user_watches = 1048576adamchainz commentedon Feb 27, 2025
Ah, fun. Happy to add a docs section on this. Not sure there’s anything else we can do...
A system check could potentially detect a low value and recommend bumping it, but the "max" number to set seems project-specific. Y’all have a LOT more code than the median project.
adamchainz commentedon Sep 22, 2025
@jarshwah did you do anything in your project to help devs with this limit? Want to make any kind of docs change?
jarshwah commentedon Sep 23, 2025
Sorry @adamchainz this is one of the things still sitting in my TODO pile. I was finding that watchfiles wasn't detecting changes to certain paths, and I wanted to dig into the cause of that (and see if it was related to sysctl limits) or something else, and then moved onto other things.
From memory (reader, beware!) I think the main issue is that we wanted more control over which paths to be monitored and which to exclude, so we aren't trying to set watches on the endless node_modules etc. The max user watches was a workaround for the underlying problem.
adamchainz commentedon Sep 23, 2025
That was probably the issue fixed in #91, so worth a re-try.
Okay, that's something the Django docs warn about for the Watchman integration too: https://docs.djangoproject.com/en/5.2/ref/django-admin/#runserver:~:text=When%20using,this,-%2E
We don't have an API here for watchfiles yet. I don't know how easy it would be, given
watchfiles.watch()takes a list of paths to watch, with no way to exclude subdirectories there, only post-event.czue commentedon Oct 10, 2025
I haven't dug into this too closely, but is it expected that watchfiles would watch everything in a local
.venvdirectory? That makes hitting this limit not so uncommon on many systems. Just curious if there's a configuration-level workaround for that, or if it's by design?czue commentedon Oct 10, 2025
Ok sorry, I'm reading the thread more closely and see a similar discussion above and that there's currently not a solution.
(Sorry for multiple edits of this comment, I confused myself because the Django link above goes to a different project)
adamchainz commentedon Oct 11, 2025
Looking upstream, there's only one issue on watchfiles: https://github.com/samuelcolvin/watchfiles/issues?q=sort%3Aupdated-desc%20is%3Aissue%20state%3Aclosed%20%22Too%20many%20open%20files%22 . And two in notify-rs (what watchfiles uses): https://github.com/notify-rs/notify/issues?q=is%3Aissue%20state%3Aopen%20%22Too%20many%20open%20files%22 . All point to increasing the limits.
A little reading up reveals
setrlimit, which increases the limit for the number of open files. So it may be possible to fix the issue in some cases by bumping the limit with a call to that. Testing in IPython:Can one of y'all with the issue try adding a call to
setrlimitin your server, with appropriatetry/except OSErrorwrapping, and see if that fixes the issue? If so, we might add some default limit-bumping here.czue commentedon Oct 13, 2025
Thanks for the suggestion. I tried it out and it doesn't seem to work. I think it sets the limit on file descriptors but we need to set the inotify limit(?). Also, according to AIs at least, it is not possible to update that limit programatically (at least without root). So I think any library-level solution might need to limit the number of watched files rather than attempt to modify the limits.
Do we have any leeway to leverage / passthrough the
ignore_dirssetting to watchfiles as a filter? I haven't dug into any of the implementation details, but on the surface it seems like exposing that would likely solve this.Edit: I tried playing with filters, but that also didn't seem to help. I think the initial
watchcall still sets up aninotifyinstance on every file before it reaches the filters and then discards them. (I think you already knew this based on your previous comments, but I didn't till now). So it seems like maybe the only solution which probably is not worth it would be to construct the list of paths dynmically based on a more complex set of rules within this library, so we can callwatch()with a subset of files / directories before reaching the notify calls. 😞adamchainz commentedon Oct 14, 2025
Ah, okay, thanks for trying.
That was my mental model, yes, but I hadn't 100% confirmed it. Thanks for checking.
It sounds like it would be worth it for a large enough project, for which I know @jarshwah works on one, and I have a client with another! So while I can't say I'll be working on it right now, there's hope…
Before then, maybe we can document how to change the limits?
adamchainz commentedon Oct 14, 2025
Also, I found a similar issue upstream: samuelcolvin/watchfiles#341
czue commentedon Oct 15, 2025
Nice. Yeah a workaround like this: sphinx-doc/sphinx-autobuild#194 is what I had in mind as something that could be done as a wrapper. If you think you'd accept something like that as a patch (I would probably replace the "." prefix behavior with a configurable django setting to ignore dirs), I'd be game to take a crack at it