ed6bab847b
Existing implementation is an entangled mess which consists of shared code residing in the base class which in random places invokes a number of undocumented poorly named methods overloaded in OS specific subclasses. Some of these methods mutate static state. There are no clear lifetime guarantees for different parts of the system (including comments saying that some values might or might not be valid at certain points). The rewrite aims to clean most of this up - sharing everything that can be shared and moving OS specific logic to clearly documented methods. Furthermore, we change the code to ensure proper lifetime guarantees - so we no longer find ourself in situations where we don't know whether pathId is valid or not. This refactoring by itself fixes a number of issues, most specifically a bug where watcher would stop receiving events on Windows because DirectoryWatchHandle ends up allocated at precisely the same address as a previous destroyed one - which confuses Dart side to think that newly created handle is the same as the old one (due to a race between event handler thread and Dart thread). We fix Windows lifetime issue by a) not keeping pathId based mapping in the watcher anymore and b) keeping DirectoryWatchHandler alive until it is stoped by the Dart side - this is achieved by retaining it after it is created and releasing it once path is unwatched. This way Dart side is always sure that pathId values are valid until they are explicitly released via _unwatchPath - which makes code very uniform. To make sure that native objects created by _watchPath are released when surrounding isolate exists abruptly (e.g. via Isolate.exit - without letting Dart code to shutdown and call _unwatchPath naturally) we attach NativeFinalizer to them. This fixes the existing leak of file watchers on Mac OS X - as Node objects it created were not freed if surrounding isolate exited. Note that inotify descriptors did not leak in the same way because they were wrapped into sockets. Finally, this refactoring also make sure that the last subscriber cancelling subscription on filesystem event stream will get a proper cancellation future back and can wait for the watcher to shutdown. Previously implementation used broadcast streams which simply return an already completed future when subscriber cancels. New implementation uses Stream.multi instead which gives a better result. Now doing watch().listen().cancel() returns a future which will only complete once watcher is fully disposed (e.g. inotify descriptor is closed). Bad behavior was revealed by analysing standalone/regress_52715 - which revealed that repeatedly watching and cancelling might flakely cause us to hit fd limit depending on whether eventhandler thread can keep up closing file descriptors created by the main thread or not. Fixes https://github.com/dart-lang/sdk/issues/61378 TEST=standalone/{regress_61378,file_system_watcher_isolate_exit_leak} CoreLibraryReviewExempt: VM only changes. Change-Id: I6a6a69642b1f2673f2be78434bc64270846ad8c5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/450921 Reviewed-by: Lasse Nielsen <lrn@google.com>
156 lines
4.6 KiB
C++
156 lines
4.6 KiB
C++
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "platform/globals.h"
|
|
#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
|
|
|
|
#include "bin/file_system_watcher.h"
|
|
|
|
#include <errno.h> // NOLINT
|
|
#include <sys/inotify.h> // NOLINT
|
|
|
|
#include "bin/fdutils.h"
|
|
#include "bin/file.h"
|
|
#include "bin/socket.h"
|
|
#include "platform/signal_blocker.h"
|
|
|
|
namespace dart {
|
|
namespace bin {
|
|
|
|
void FileSystemWatcher::InitOnce() {}
|
|
|
|
void FileSystemWatcher::Cleanup() {}
|
|
|
|
bool FileSystemWatcher::IsSupported() {
|
|
return true;
|
|
}
|
|
|
|
intptr_t FileSystemWatcher::Init() {
|
|
int id = NO_RETRY_EXPECTED(inotify_init1(IN_CLOEXEC));
|
|
if (id < 0) {
|
|
return -1;
|
|
}
|
|
// Some systems don't support setting this as non-blocking. Since watching
|
|
// internals are kept away from the user, we know it's possible to continue,
|
|
// even if setting non-blocking fails.
|
|
FDUtils::SetNonBlocking(id);
|
|
return id;
|
|
}
|
|
|
|
intptr_t FileSystemWatcher::WatchPath(intptr_t id,
|
|
Namespace* namespc,
|
|
const char* path,
|
|
int events,
|
|
bool recursive) {
|
|
int list_events = IN_DELETE_SELF | IN_MOVE_SELF;
|
|
if ((events & kCreate) != 0) {
|
|
list_events |= IN_CREATE;
|
|
}
|
|
if ((events & kModifyContent) != 0) {
|
|
list_events |= IN_CLOSE_WRITE | IN_ATTRIB | IN_MODIFY;
|
|
}
|
|
if ((events & kDelete) != 0) {
|
|
list_events |= IN_DELETE;
|
|
}
|
|
if ((events & kMove) != 0) {
|
|
list_events |= IN_MOVE;
|
|
}
|
|
const char* resolved_path = File::GetCanonicalPath(namespc, path);
|
|
path = resolved_path != nullptr ? resolved_path : path;
|
|
int path_id = NO_RETRY_EXPECTED(inotify_add_watch(id, path, list_events));
|
|
if (path_id < 0) {
|
|
return -1;
|
|
}
|
|
return path_id;
|
|
}
|
|
|
|
void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) {
|
|
VOID_NO_RETRY_EXPECTED(inotify_rm_watch(id, path_id));
|
|
}
|
|
|
|
intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) {
|
|
USE(path_id);
|
|
return id;
|
|
}
|
|
|
|
static int InotifyEventToMask(struct inotify_event* e) {
|
|
int mask = 0;
|
|
if ((e->mask & IN_CLOSE_WRITE) != 0 || (e->mask & IN_MODIFY) != 0) {
|
|
mask |= FileSystemWatcher::kModifyContent;
|
|
}
|
|
if ((e->mask & IN_ATTRIB) != 0) {
|
|
mask |= FileSystemWatcher::kModifyAttribute;
|
|
}
|
|
if ((e->mask & IN_CREATE) != 0) {
|
|
mask |= FileSystemWatcher::kCreate;
|
|
}
|
|
if ((e->mask & IN_MOVE) != 0) {
|
|
mask |= FileSystemWatcher::kMove;
|
|
}
|
|
if ((e->mask & IN_MOVED_TO) != 0) {
|
|
mask |= FileSystemWatcher::kMovedTo;
|
|
}
|
|
if ((e->mask & IN_DELETE) != 0) {
|
|
mask |= FileSystemWatcher::kDelete;
|
|
}
|
|
if ((e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) != 0) {
|
|
mask |= FileSystemWatcher::kDeleteSelf;
|
|
}
|
|
if ((e->mask & IN_ISDIR) != 0) {
|
|
mask |= FileSystemWatcher::kIsDir;
|
|
}
|
|
return mask;
|
|
}
|
|
|
|
Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) {
|
|
USE(path_id);
|
|
const intptr_t kEventSize = sizeof(struct inotify_event);
|
|
const intptr_t kBufferSize = kEventSize + NAME_MAX + 1;
|
|
uint8_t buffer[kBufferSize];
|
|
intptr_t bytes =
|
|
SocketBase::Read(id, buffer, kBufferSize, SocketBase::kAsync);
|
|
if (bytes < 0) {
|
|
return DartUtils::NewDartOSError();
|
|
}
|
|
const intptr_t kMaxCount = bytes / kEventSize;
|
|
Dart_Handle events = Dart_NewList(kMaxCount);
|
|
intptr_t offset = 0;
|
|
intptr_t i = 0;
|
|
while (offset < bytes) {
|
|
struct inotify_event* e =
|
|
reinterpret_cast<struct inotify_event*>(buffer + offset);
|
|
if ((e->mask & IN_IGNORED) == 0) {
|
|
Dart_Handle event = Dart_NewList(kEventNumElements);
|
|
int mask = InotifyEventToMask(e);
|
|
Dart_ListSetAt(event, kEventFlagsIndex, Dart_NewInteger(mask));
|
|
Dart_ListSetAt(event, kEventCookieIndex, Dart_NewInteger(e->cookie));
|
|
if (e->len > 0) {
|
|
Dart_Handle name = Dart_NewStringFromUTF8(
|
|
reinterpret_cast<uint8_t*>(e->name), strlen(e->name));
|
|
if (Dart_IsError(name)) {
|
|
return name;
|
|
}
|
|
Dart_ListSetAt(event, kEventPathIndex, name);
|
|
} else {
|
|
Dart_ListSetAt(event, kEventPathIndex, Dart_Null());
|
|
}
|
|
Dart_ListSetAt(event, kEventPathIdIndex, Dart_NewInteger(e->wd));
|
|
Dart_ListSetAt(events, i, event);
|
|
i++;
|
|
}
|
|
offset += kEventSize + e->len;
|
|
}
|
|
ASSERT(offset == bytes);
|
|
if (i == 0) {
|
|
// No events in the chunk.
|
|
return Dart_NewList(0);
|
|
}
|
|
return events;
|
|
}
|
|
|
|
} // namespace bin
|
|
} // namespace dart
|
|
|
|
#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
|