Skip to content

Commit d984c83

Browse files
committed
Improve FS monitoring error handling in corner cases
1 parent 3c73d44 commit d984c83

5 files changed

Lines changed: 47 additions & 19 deletions

File tree

far/changelog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
--------------------------------------------------------------------------------
2+
drkns 2026-03-09 22:24:56+00:00 - build 6656
3+
4+
1. Improve FS monitoring error handling in corner cases.
5+
16
--------------------------------------------------------------------------------
27
drkns 2026-03-09 19:09:32+00:00 - build 6655
38

far/filelist.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,10 @@ bool FileList::ProcessKey(const Manager::Key& Key)
16691669
AnotherPanel->Redraw();
16701670
}
16711671
}
1672+
1673+
if (FSWatcher)
1674+
FSWatcher->restart_if_needed();
1675+
16721676
break;
16731677
}
16741678

far/filesystemwatcher.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,13 @@ static os::handle open(const string_view Directory)
211211
FileSystemWatcher::FileSystemWatcher(const string_view EventId, const string_view Directory, const bool WatchSubtree):
212212
m_EventId(EventId),
213213
m_Directory(nt_path(Directory)),
214-
m_WatchSubtree(WatchSubtree),
215-
m_DirectoryHandle(open(m_Directory))
214+
m_WatchSubtree(WatchSubtree)
216215
{
217-
if (!m_DirectoryHandle)
218-
{
219-
LOGWARNING(L"Skip monitoring of {}"sv, Directory);
220-
return;
221-
}
222-
223216
m_Overlapped.hEvent = m_Event.native_handle();
224-
225217
background_watcher::instance().add(this);
226218

227-
LOGDEBUG(L"Start monitoring {} {}"sv, m_Directory, WatchSubtree? L"tree"sv : L"directory"sv);
228-
read_async();
219+
if (open_directory())
220+
read_async();
229221
}
230222

231223
FileSystemWatcher::~FileSystemWatcher()
@@ -265,9 +257,28 @@ FileSystemWatcher::~FileSystemWatcher()
265257
}
266258
}
267259

260+
void FileSystemWatcher::restart_if_needed()
261+
{
262+
SCOPED_ACTION(std::scoped_lock)(m_CS);
263+
264+
if (m_DirectoryHandle)
265+
return;
266+
267+
LOGINFO(L"Attempting to restart monitoring of {}"sv, m_Directory);
268+
269+
if (open_directory())
270+
read_async();
271+
}
272+
273+
bool FileSystemWatcher::open_directory()
274+
{
275+
m_DirectoryHandle = open(m_Directory);
276+
return m_DirectoryHandle != nullptr;
277+
}
278+
268279
void FileSystemWatcher::read_async()
269280
{
270-
m_Event.reset();
281+
LOGDEBUG(L"Start monitoring {} {}"sv, m_Directory, m_WatchSubtree? L"tree"sv : L"directory"sv);
271282

272283
if (!ReadDirectoryChangesW(
273284
m_DirectoryHandle.native_handle(),
@@ -327,13 +338,18 @@ bool FileSystemWatcher::get_result() const
327338

328339
void FileSystemWatcher::callback_notify()
329340
{
330-
if (!m_DirectoryHandle)
331-
return;
332-
333-
if (!get_result())
334341
{
335-
LOGDEBUG(L"Stop monitoring {}"sv, m_Directory);
336-
return;
342+
SCOPE_EXIT{ m_Event.reset(); };
343+
344+
if (!m_DirectoryHandle)
345+
return;
346+
347+
if (!get_result())
348+
{
349+
LOGWARNING(L"Stop monitoring {}"sv, m_Directory);
350+
m_DirectoryHandle = {};
351+
return;
352+
}
337353
}
338354

339355
LOGDEBUG(L"Change event in {}"sv, m_Directory);

far/filesystemwatcher.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ class FileSystemWatcher
5454
FileSystemWatcher(string_view EventId, string_view Directory, bool WatchSubtree);
5555
~FileSystemWatcher();
5656

57+
void restart_if_needed();
58+
5759
private:
5860
friend class background_watcher;
5961

62+
bool open_directory();
6063
void read_async();
6164
bool get_result() const;
6265
void callback_notify();

far/vbuild.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6655
1+
6656

0 commit comments

Comments
 (0)