[2.2.x] Fixed #30523 -- Fixed updating file modification times on seen files in auto-reloader when using StatReloader.

Previously we updated the file mtimes if the file has not been seen
before - i.e on the first iteration of the loop.

If the mtime has been changed we triggered the notify_file_changed()
method which in all cases except the translations will result in the
process being terminated. To be strictly correct we need to update the
mtime for either branch of the conditional.

Regression in 6754bffa2b.

Backport of 480492fe70 from master
This commit is contained in:
Tom Forbes 2019-05-27 12:50:05 +01:00 committed by Mariusz Felisiak
parent ace0bec804
commit 7089502b98
3 changed files with 15 additions and 1 deletions

View File

@ -333,9 +333,9 @@ class StatReloader(BaseReloader):
while True: while True:
for filepath, mtime in self.snapshot_files(): for filepath, mtime in self.snapshot_files():
old_time = mtimes.get(filepath) old_time = mtimes.get(filepath)
mtimes[filepath] = mtime
if old_time is None: if old_time is None:
logger.debug('File %s first seen with mtime %s', filepath, mtime) logger.debug('File %s first seen with mtime %s', filepath, mtime)
mtimes[filepath] = mtime
continue continue
elif mtime > old_time: elif mtime > old_time:
logger.debug('File %s previous mtime: %s, current mtime: %s', filepath, old_time, mtime) logger.debug('File %s previous mtime: %s, current mtime: %s', filepath, old_time, mtime)

View File

@ -28,3 +28,7 @@ Bugfixes
* Fixed a regression in Django 2.2 that caused a crash of auto-reloader when * Fixed a regression in Django 2.2 that caused a crash of auto-reloader when
an exception with custom signature is raised (:ticket:`30516`). an exception with custom signature is raised (:ticket:`30516`).
* Fixed a regression in Django 2.2.1 where auto-reloader unnecessarily reloads
translation files multiple times when using ``StatReloader``
(:ticket:`30523`).

View File

@ -656,6 +656,16 @@ class StatReloaderTests(ReloaderTests, IntegrationTests):
# Shorten the sleep time to speed up tests. # Shorten the sleep time to speed up tests.
self.reloader.SLEEP_TIME = 0.01 self.reloader.SLEEP_TIME = 0.01
@mock.patch('django.utils.autoreload.StatReloader.notify_file_changed')
def test_tick_does_not_trigger_twice(self, mock_notify_file_changed):
with mock.patch.object(self.reloader, 'watched_files', return_value=[self.existing_file]):
ticker = self.reloader.tick()
next(ticker)
self.increment_mtime(self.existing_file)
next(ticker)
next(ticker)
self.assertEqual(mock_notify_file_changed.call_count, 1)
def test_snapshot_files_ignores_missing_files(self): def test_snapshot_files_ignores_missing_files(self):
with mock.patch.object(self.reloader, 'watched_files', return_value=[self.nonexistent_file]): with mock.patch.object(self.reloader, 'watched_files', return_value=[self.nonexistent_file]):
self.assertEqual(dict(self.reloader.snapshot_files()), {}) self.assertEqual(dict(self.reloader.snapshot_files()), {})